I am new in the game development and now I am at the stage when you need to fill the game content. I want to create the events that occur with the player (like in FTL). Now I make these events with the help of hardcoding, but I understand that this is a very bad style. I know there is another way, use data in XML files. Here is an example:

 <event name="SIMPLE_EVENT">
  <text>Event text</text>
  <battle name="MONSTERS_WEAK"/>

This event show a dialog window and start battle with a random weak monster. I understand how to make this XML files, but I not so experienced with C# to write code that will convert data from XML to my C# classes. How to convert this xml to C# methods invocation? Something like: ShowDialogWindow("Event text"); StartBattle("MONSTERS_WEAK"); Another example is:

<event name="ADD_FUEL">
 <text>Adding fuel</text>
     <item type="fuel" min="2" max="2"/>

This event show dialog too and add some fuel to the player.

I think its a some deserialization task? Anyway similar as FTL I need one class to represent all events in the game, using different XML data. I ask some advice with this, maybe tutorials, pattens or examples? Or maybe another solution of my task?


1 Answer 1


For the question of how to deserialize an xml file in c#, this question on stack overflow explains it pretty well: How to Deserialize XML document

The gist of it is, you need to create a class for each type you are going to represent in the xml files, in the example you gave it would be event, item, item_modify and possibly battle, and define for each object properties corresponding to its content in the xml, for example every event had text.

From then on is a question of what you want to do with these events. Separate interpreting the xml files themselves from what you want the events to do. You should first define the scope of what events the game should have. If you are making a game, this would be rather simple because the event will simply have to serve the mechanics of the game (A simple retro RPG game would have things like random encounters events, gaining items or effects like poison and sleep). If you are making a game engine it might be more complicated as you need to design your events around an unspecific general goal.

The next question you should ask is what part of your game should handle these events. You can't simply send the events to whatever part of your game that is supposed to handle each event (for example, an inventory should handle all item events) because the question of what class should handle what event can only be answered after reading the event, so an intermediate must decide what part should get what event. The solution to this problem can vary wildly depending on your implementation so far but the simplest solution is this: whenever you read a new event, ALL classes that can handle events receive it, and each one handles it on it's own discretion.

You can implement such a system by having a single class (EventReader) with a method that receives a file name, and with a C# event that receives an instance of the class representing game events (Event).

public class EventReader{
    public delegate void GameEventHandler(Event e);
    public event GameEventHandler GameEventReceived;

    public void readEvent(String path){
        XmlSerializer serializer = new XmlSerializer(typeof(Event));

        using(StreamReader reader = new StreamReader(path)){
             event = (Event)serializer.Deserialize(reader);
             if(GameEventReceived != null)

Every class that wishes to handle game event can simply add the game event handling method to the game-event event on this class.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .