0

I'm making a Entity Component System for a game. I implemented my components as structs with member variables:

struct Position
{
   float x;
   float y;
}

struct Render
{
   unsigned int texture;
}

That's all fine, and as simple as it can get, and if I want to add a new component type I would have to create a new struct and add it to the code.

But here's a problem: I want to enable players to create mods that add new component types after the game is already compiled?

For example, let's say that the modder supplies a file which defines a component, the file contains something like this:

position
float x
float y

One way I could do that is to make every component have a map that maps a string (variable name) with its value. I would have one map for ints, one map for floats, and so on. Then the component would look something like this:

struct Component
{
   string type;
   map<string, int> ints;
   map<string, float> floats;
}

Then the game would read a file that defines a component and add the variables to the maps. Now my questions are:

  • Is this a good idea?
  • Since I access these variables every frame, would the access get any slower because of the maps?
  • What would be the best solution to this problem?

EDIT: I should point out that the files define entirely new type of struct, so instead of adding manually something like the following in the code:

struct Health
{
   float max_hp;
   float hp;
}

I would write a file that contains:

health
float max_hp
float hp

and have the game read the class definition from that.

10
  • where are the variable names in you example mod file ? Commented Jan 5, 2014 at 14:38
  • 1
    You may wish to look into scripting languages. Most games typically provide extension by exposing an API in a scripting language and then allow users to provide their own custom scripts to run. Commented Jan 5, 2014 at 14:39
  • @StephaneRolland the names in this example would be x and y Commented Jan 5, 2014 at 14:40
  • I'm not sure if I understand the problem completely. Why can't you just parse the file into your normal struct? Commented Jan 5, 2014 at 14:41
  • @MikeBantegui, I was thinking of implementing Lua or something similar, but I'm unsure if scripting languages can create new structs in already compiled program like that? Commented Jan 5, 2014 at 14:42

1 Answer 1

1

Your objects could have a set of primitive attributes and custom attributes. Your engine must rely on the existence of primitive attributes and, usually, would not access custom ones. For example it makes no sense to define the "position" attribute in the mods, because the engine must rely on the existence of that attribute and will use it in a very peculiar way. On the other hand custom attribute might be used in the scripting language used in the mods. If organized in this way you don't have speed issues. The low level attributes are accessed with maximum speed, while custom attribute are accessed by name but the small loss of speed is negligible compared to the interpretation of the scripting language.

Sign up to request clarification or add additional context in comments.

3 Comments

That makes sense. Having, for this example, Position and Render structs defined in the code but also having a struct with maps to use for mods.
How would I decide what attributes should be in the code? Should the attributes that are used with something within the engine (e.g. Rendering) be in the code and other attributes that define gameplay and logic be in scripts? Or should I just implement everything in code and leave the script only for modders?
Yes: every attribute used by the engine should be made primitive.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.