Read function outside of code while run-time?

Hi it is my first post on this community. So if i was rude or something please let me know :)

So I just started learning game development alone. It's been about 1 year since I begin to learn c and c++. Recently I started planning to make a game for my own entertainment and future career.

I'm trying to make the game as mod-friendly as possible. So I thought that I should make abstract classes that have most of the functions for working on run-time. Then when the game starts, the system reads mod files for filling out class information and stores that base object in some data storage made with a singleton(for easy access). And, then use a factory pattern(I hope I'm using it right) to copy the base object that I need, fill it with basic information and store it in the object storage.

The problem is, I can't figure out how I can make it so each object from each mod file acts with its own behavior.

For example: Core mod(main game) has an abstract class named 'living entity' 'Living entity' has some information like bone structure type, mesh name, entity size, etc... And a mod file from the core mod makes a 'human' class that has information for 'human bone type','basic human','human size' etc The human class does not need any special functions because it only uses basic functions that the game provides.

But modder A wants to make a Vampire entity. Vampire will have information like a human. So modder A fills up or copies the 'human' file and edits the class id value with which the system identifies objects and uses as a name. Vampire needs new skills, health effects and custom death(leaves an ash pile instead of a corpse) The game also has skills and health effects as a kind of component or class. So modder A fills up information but one of the skills has a unique projectile effect or one of the health effects has a unique behavior that the game does not provide So modder A wants to override projectile or health functions only for the vampire's unique skill or health

In this case how would I support them to override functions for their own needs? One of my idea was to read the dll and store function pointers in data storage and put the code on a base abstract object and then call it if the object has a custom function. But I am not sure this works and that it is how people do it. So if there is some kind of information or documentation for that please let me know I'm arguing with this problem about month D;

• Isn't this exactly what abstract classes and virtual functions do? – tkausl Nov 3 '18 at 22:41
• you're right. but i thought people can not edit the compiled file. so i guessed there should be some kind of system that read the class or function from external file during run-time – HM Ham Nov 4 '18 at 7:27

If you want to allow modders to not just inject data but also code into your game, then there are two appoaches:

• Have your game load dynamically linked libraries (.dll on Windows, .so on UNIX) at runtime and execute functions with certain names in them. This allows modders to create mods in C++ or any other programming language which can be used to build libraries. However, there are two downsides. First, these libraries must be compiled for the users target architecture, so mods won't be cross-platform unless people recompile them for every platform your game supports (but that's of course only a problem if your game is cross-platform in the first place). But a second and even greater problem is security. These libraries can do anything you can do in C++, so it would be possible for malicious people to spread mods which contain malware.
• Add an interpreter for a scripting language to your game. That way mods can contain scripts which are executed by your game and can be used to implement custom behavior. The advantage over the library method is that scripts can be properly sandboxed. You can decide what functionality you want to expose to the scripting engine and what functionality you would rather not want mods to meddle with. There are language bindings for lots and lots of scripting languages available for C++. Lua is quite popular for game development, but which one you want to use is eventually a matter of personal taste. You might even consider to create your own scripting language specifically tailored to the needs of your game, but I would advise against this. Developing an own programming language and a good interpreter for it is far more difficult than it seems at first glance. Using an implementation of an existing language which is already tested and documented will save you a lot of work.

One simple way would be to give your modders access to the class definitions, such as the "LivingEntity" you described, that way they can simply derive from the base class, and you dont need to have special handling for modified objects or anything else as their code would be compiled as an object with the right interface for your system.

To then integrate these new objects into your game you would need a way for them to 'register' the new objects if you wanted to use them as part of your base system(for example picking random entities to spawn). A fairly simple way to do this would be to have a central object that deals with holding all the entity types, like the factory method you described but with the option to register new types.

I wont go into detailed descriptions of the systems since you asked a higher level question, so I'll leave it up to you to attempt it if you pick this method then come back with more specific implementation questions

• so the modders can add classes or functions even the file already compiled? i thought i need to open project file and recompile to do that. than i just gonna do my work and they can edit whatever they want to do? :o – HM Ham Nov 4 '18 at 7:19
• They would need to compile to a DLL on windows. Thats basically a program that is compiled in a way that another program can load it and use its code, much like you were describing. The use of DLLs would take up much more than one post, but googling about how to use DLLs would be a good place to start in that area :3 – TurtleKwitty Nov 4 '18 at 18:01