Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Structure for objects in a 2D game

Tags:

c#

First, let me apologize for asking a question that may seem a bit vague (or badly formalized), but I lack experience to ask anything more specific.

I'm building an engine for playing 2D adventure games (the you-click-on-an-object-and-something-happens sort) in C# and I'm considering the best structure for it. As you can imagine, different things can happen when you interact with an object: if it's a door, you expect to enter another room, if it's a person, you expect to start talking to them, etc. My idea is to achieve this behaviour with delegates, like this:

public abstract class GameObject {
    public delegate void onAction();
    public onAction Click;
}

public class Door : GameObject {
    public Door() {
        Click = new onAction(ChangeRoom);
    }
    private void ChangeRoom() {
        //code to change room here
    }
}

public class Person : GameObject {
    public Person() {
        Click = new onAction(StartTalking);
    }
    private void StartTalking() {
        //code to display dialogue here
    }
}

I find this solution quite elegant, because if I want to create a special object with some behaviour not covered by its class, I can simply do this:

specialObject.Click += new onAction(SpecialMethod);

But this is also where things get complicated. I want my engine to be capable of playing different games simply by loading different data, without changes to the engine itself, so hard-coding the SpecialMethod somewhere inside the engine is not an option, it must be part of the data. For normal object, it can all be done with (de)serialization, but from what I've read, this is problematic with delegates. Can you suggest a way to do it?

P.S.: Since I'm still on the concept level and the code above is not yet implemented, you are free to suggest whatever solution you find best, even one that avoids delegates completely.

like image 371
Maja Remic Avatar asked Sep 22 '11 16:09

Maja Remic


2 Answers

What you need is scripting.

Essentially a script is an external resource (i.e. An external file) which contain a source code, which describe actions.

C# has different option for this.

You can emit assemblies from .NET languages. Or you can integrate another language (python, for example) and executin the script at runtime. Essentially the script is loaded, interpreted or compiled, and executed at the right time. If the script is able to modify the application state (the data structures), you can define objects behavior with external data (data-driven development).

There is a lot literature about this issue, but I feel to suggest GPU Programming Gems 6 (section 4). However, the effort required to integrate a great scripting engine is very much. That way I tried to figure out a simple logic based on XML files (here is my question about, joining another question for defining the XML contents); sadly, I found that almost depends on the flexibility you need.

like image 120
Luca Avatar answered Sep 30 '22 00:09

Luca


First of all writing engine when you lack experience may be harder then you think. I'll suggest that you write you first game without engine and hardcode as much as you want and then learn from the experience and write your engine... I know that's not what you want :D

If you haven't checked yet look at http://www.adventuregamestudio.co.uk/ play with the editor and think of the object model underneath. It will help.

Since you want your games to be data driven you have to be able dynamically load and execute code. "It doesn't have to be a scripting language." It's against my nature to suggest adding scripting language and learning it at the same time when you learn how to build an engine. .NET Framework has a great reflection system that allows you to load C# code and compile it at runtime. I have to point out that this is kind of scripting in C# but there is a lack of proper definition of scripting language.

Since you don't have a lot of experience in this my suggestion would be to use XML files that define your rooms and have a basic sequence of nodes (commands) that define what should happened when you click something. You should implement all the commands in your "engine" but you would be able to easily create new rooms therefore similar games. XML will be also very expandable approach since because of it's hierarchical structure it will allow you to add conditions, loops, etc. that will expand your engine's "scripting" capabilities.

like image 28
Aleks Avatar answered Sep 30 '22 02:09

Aleks