Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this question
    
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. –  Seth Battin Feb 22 at 21:23
    
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. –  Mbastias Feb 22 at 22:32
1  
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. –  Seth Battin Feb 22 at 23:01
    
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. –  Mbastias Feb 22 at 23:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.