0
\$\begingroup\$

I am in the process of creating a game engine written in C++, using the C/C++ SQLite interface to achieve a 'type object' pattern. The process is largely similar to what is outlined here (Thank you Bob Nystrom for the great resource!).

I have a generally defined Entity class that when a new object is created, data is taken from a SQLite database and then is pushed back into a pointer vector, which is then iterated through, calling update() for each object. All the ints, floats, strings are loaded in fine, but the script() member of Entity is proving an issue. It's not much fun having a bunch of stationary objects laying around my gameworld.

The only solutions I've come up with so far are:

  1. Create a monolithic EntityScript class with member functions encompassing all game AI and then calling the corresponding script when iterating through the Entity vector. (Not ideal)
  2. Create bindings between C++ and a scripting language.
    • This would seem to get the job done, but it feels like implementing this (given the potential memory overhead) and learning a new language is overkill for a small team (2-3 people) that know the entirety of the existing game engine.

Can you suggest any possible alternatives? My ideal situation would be that to add content to the game, one would simply add a script file to the appropriate directory and append the SQLite database with all the object data. All that is required is to have a variety of integers and floats passed between both the engine and the script file.

\$\endgroup\$
  • \$\begingroup\$ Implementing a lua binding in c++ can be done very easily, and will give you much more flexibility, I recommend looking into it. \$\endgroup\$ – akaltar Dec 27 '13 at 1:32
1
\$\begingroup\$

Make a series of Behaviour* subclasses that implement each of your behaviours. Register them by name in a table. Store/load the name of the behaviour in the database.

 struct IBehavior { void think(); }
 struct VillagerBehavior : IBehavior;
 struct MonkeyBehavior : IBehavior;
 struct OgreBehavior : IBehavior;

 std::pair<string, IFactory*> behaviors[] = {
   { "villager", CreateFactory<VillagerBehavior>() },
   { "monkey", CreateFactory<MonkeyBehavior>() },
   { "ogre", CreateFactory<OgreBehavior>() },
 };

 IFactory* GetBehavior(string name) {
   return behaviors[name];
 }

Note that you can even extend this to allow multiple behaviors per entity using a priority list or action list AI.

\$\endgroup\$
  • \$\begingroup\$ Thank you for your solution. My only problem with this is that it doesn't allow for much extensibility. It would be nice for the game to be moddable by users without needing to recompile the whole source. \$\endgroup\$ – Erik Sep 26 '13 at 19:07
  • \$\begingroup\$ Then use a scripting. That's what almost every game big or small does for such needs. \$\endgroup\$ – Sean Middleditch Sep 26 '13 at 19:14
  • \$\begingroup\$ Almost every? Do you know of any games that achieve a moddable environment by using a different system? A plug-in system of compiled objects of sorts perhaps. \$\endgroup\$ – Erik Sep 26 '13 at 20:11
  • \$\begingroup\$ Minecraft, for instance, is often just decompiled and new Java code inserted and compiled into a new JAR file. A number of other Java and C# games have similar stories. There are some notable mods in the world that patch C++ code, too, such as some of the extensions to Bethesda's games. Typically such efforts are only undertaken for very popular games by very dedicated, hardcore fans. Many other games have much simpler mods comprising almost solely of data file changes (new models/textures, item alterations, etc.). \$\endgroup\$ – Sean Middleditch Sep 26 '13 at 20:44
  • \$\begingroup\$ Thank you very much for all the information. Mentioning how the those alternations are undertaken by "very dedicated, hardcore fans" helped put things in perspective. I neglected the enthusiastic, but inexperienced (at programming) gamers out there. \$\endgroup\$ – Erik Sep 26 '13 at 22:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.