Question

This is more of a conceptual question, to help me understand how things work.

In my simple game engine I have a game object class that has all the usual variables, like: position, orientation, scale, velocity, model, texture, and so on.

And now I want to be able to define behaviors of these objects with scripting. I understand how I would do that for existing variables. For example, if velocity != 0, change the position.

But how do I define new attributes? (e.g. Health, Ammo)

I can't create new variables in the game object class, since the class is already compiled, and I can't add the variables in the script since it would be reset the next time the behavior script runs.

How are these things usually done in games?

Was it helpful?

Solution

As you said, your C++ class is already comppiled and can't be changed. Well, that's not entirely true. From a pure C++ standpoint,

  1. it is possible to add attributes to a class: define a couple of std::maps for different object types, and if you need a new attribute, you add it to the appropriate map, then you can always retrieve it. However, you would need separate map for each type of attribute (int, bool, string) and each visibility (private, protected, public).

  2. Methods cannot be changed, i.e. you can't change the code of existing methods to use the new attributes. You can fake adding methods, by making your class store function pointers as data members, but those functions would not have access to the private or protected parts of your class.

  3. You can use OO polymorphism, whereby to add attributes, change existing methods, and add methods, you would create a new class that derives from base.

This is all good with regards to C++ side of the system, but the problem as you stated is that you can't create a class on the fly: it must be compiled. So now you bring in Lua. You can't make Lua generate derived classes at the C++ level.

If your C++ classes have virtual methods, create a class that derives from your base, such that it can be instantiated from Lua and forwards to the correct Lua functions of your Lua instance. With this a Lua script can extend the a C++ class, and register the object in C++, and when C++ calls a virtual method on that object, it will call the associated Lua method that extends the class. I have not done that manually myself, but SWIG (www.swig.org) is a tool that makes that very easy to do via its "director" feature. Many people do their own Lua-C++ binding, or use luabind, or SWIG, or other. I have created my own, lua-icxx, which works in tandem with SWIG, so it's a different approach. In any case, this doesn't allow you to create completely new behavior at the C++ level, unless you make everything virtual.

Conclusion:

  1. you'll have to make your C++ classes extendable, and your design able to use derived classes.
  2. you can fake class derivation from the Lua side; SWIG makes this easy to do, assuming #1 done properly.
  3. you should also be able to then give your Lua instances that extend C++ classes to C++ for later use, and when your C++ code calls a virtual method on those instance, the corresponding Lua "methods" will get called.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top