Hey guys!
In my current project (A platformer), the modding API has to be really well done, since I want every object, entity, etc, to be a mod itself (I talked a bit about that in my other threads). Basically, here is my simplified architecture:
Let's assume I have a mod "PlatformX". The restrictions for the mod, speaking of code, are simple; If it is an entity, a drawable thing needs to be loaded, a hitbox needs to be created, and it needs a list of functions for its behaviour (More about that later). So it would look like this:
//Each function is allowed to access specific variables of my game code.
init(): //Function called when loading the mod
this.texture = load it //Instantiate the texture of this entity
this.hitbox = load it //Instantiate the hitbox/collision of this entity
hookInto(EventType::Update, entityUpdate) //Hook into the update event. explained later.
hookInto(EventType::Collide, randomfunctionname_for_entityCollide) //Same here
entityUpdate(float delta):
doSomething
randomfunctionname_for_collidesWith(Entity A):
doSomething
Now some (bad) examples: (Will edit later)
map<EventType, vector<FunctionPointer>> events
gameUpdate():
for each FunctionPointer mapped to EventType::Update, execute the function
otherStuff
physicsColliding(Entity A, entity B):
if(A.hookedInto(EventType::Collide))
somehow_get_A's_collision_method(B)
So, what is this event system all about? Basically, I want to be able (And already am) to fire an event for a specific ID (Such as EventType::Update), and pass it arguments. Every method hooked into this event will then be called with those arguments. This means, that every entity that hooked, for example, into the update event, will have its update function called every 30ms. My current problem is how I'll load and execute the plugins. I found two solutions, but both are unconvenient, so not real solutions.
First one: Use a python interpreter. I would have to write some interfaces for the python code, but having all the code running in the python interpreter would be a good way to allow plugins to only access specific variables, and each "class" could also define it's own local variables. Unfortunately, there are two major problems:
1. Performance. Python isn't C++, and letting such crucial things as update-code run through a python interpreter (+ The overhead generated by needing to call these methods) would probably be extremely slow.
2. I somehow need to link each entity to its own methods. Since I can't create a C++ class at runtime, I would have to find a way to (for example) call the randomfunctionname_for_collidesWith(Entity A) method only if the entity owning this method collided with something. I don't think this is a big problem though.
Second one: C++ DLLs. This actually was my first idea. But I read somewhere that the compiler version absolutly has to be the same as the one the main program was written in, and that there are many other constraints.
The big pro's would be that I could just define classes for my new entities, and store them in a list or something.
What do you guys think I should do? I'm sorry that I did not explain that well, but I somehow forgot what I wanted to say (Started writing this post a week ago and forgot to continue since then)
Kind regards