1
\$\begingroup\$

I'm trying to implement a dialogue system for a game in Unity(c#). I'm going to use xml cause I want the dialogues to be editable/added from outside. I have a practical problem.

Let's say I got this line in XML:

<Action character="MC" name="giveItem" item="Potion" quantity="2"/>

This should tell to the inventory system of the main character that it has to give him 2 potions.

As example, not actual implementation:

MainCharacter.Inventory.giveItem("Potion",2)

The question is: With the name and parameters of the method from the XML, how do I call it? And how I call other methods that i'll need in my dialogue scripts? So far I've seen reflection and maybe a dictionary of delegates, but I'dont know is there is a better way and if those are bad practices.

\$\endgroup\$
4
  • \$\begingroup\$ What have you already tried? It sounds like you're asking for a complete solution to implementing dialog, which would be too broad a question to be answerable. \$\endgroup\$ Commented Feb 22, 2015 at 21:23
  • \$\begingroup\$ No, I'm loading my dialogs and making them conditional to characters stats already... my question is how to associate or link the attributes I read from XML to objects methods including the method name. Like, MainCharacter.Inventory.giveItem("Potion",2), But if I want to for example affect another character not in his inventory, I'll will need to access to another object method. It's more a programming question than a desing one. I'll edit. \$\endgroup\$ Commented Feb 22, 2015 at 22:32
  • 1
    \$\begingroup\$ The edit helps; that makes more sense. Regarding whether or not a given solution is "bad practice", don't worry about that. If it works it is good enough. At some point you'll have to associate some bit of data, like a string, with some code, like a delegate. So a Dictionary<string, delegateType> seems like a perfectly reasonable solution. I have to ask again though, have you tried to implement it, and have you had any specific problem that needs improving? If you're only speculating, you should definitely try a solution and see if it works. \$\endgroup\$ Commented Feb 22, 2015 at 23:01
  • \$\begingroup\$ I was doing some research before trying, and asked to see if I was missing something (probably I would have ended up trying), cause I don't have experience with somethig like this. Using strings to call functions is new for me. And I couldn't find any direction on doing something like this relating it to games. \$\endgroup\$ Commented Feb 22, 2015 at 23:10

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.