Hey guys!
Most of my previous posts are already questions on the plugin-system I'm trying to implement in my current game. Thanks to this community I've now set on using Lua for plugins and C++ for the game(-engine). The simplest thing to implement, and on what I'm already pretty clear about, is some kind of event-system. It is simple:
- C++ keeps track of listeners for each event
- An event is simply some kind of ID
- You can hook into an event trough Lua and C++, by some simple function (Someting like hookInto(id, callback))
- Events can be triggered through Lua and C++, by some simple function (Something like triggerEvent(id, arguments))
- If triggered, all listeners of this event are called: A listener simply is a callback function.
- Maybe I could make it a bit more complex and add some kind of constraints, but it seems pretty ok as it is now.
(It is basically just some callback system)
So, what will a standard plugin do? Simply hook into some events, and do stuff in the callbacks (With the exposed variables. Seems simple enough. I was also told I shouldn't directly expose structs and variables and just implement a method for each thing I want to change.)
The thing is, I want each entity, respectively each entity type, be implemented in Lua. But... How?! I thought quite a bit about it now, and still can't wrap my head around it. I've had some little ideas here and there, but not enough to really put something together. Let's put together what I already have:
Each entity consists of:
- A drawable
- A physics object (+ hitbox)
- Position
- An Init-Function or Constructor or something
Plus
- Local variables only used by a specific type of entity
- Local functions only used by a specific type of entity
(By the way, local variables are all only used in the local functions (for example flags or something), and local functions are nothing more than a callback of some event or a function used in a callback of some event. Please keep this in mind for the rest of the post.)
Only Lua needs to know about those last two.
My problems are, that I don't know how to link this all together. Of course, I don't want to:
- Create or load the drawable in Lua for performance reasons. So I'll need to expose the C++ method, which will load the drawable on C++ side
- Render the entity in Lua for performance reasons. So I'll need to use the previously loaded drawable in C++ too, which doesnt seem like a problem.
- Let C++ know about all those local variables and functions. They should stay where they are, in Lua (Unless I would somehow convert the whole Lua script into something C++-ish on load-time, but I guess that is way too complex.)
I also want to:
- Update the position accordingly to the physics object. I guess I'll just let it be a part of the physics object, problem solved.
The thing is, how do I keep tracks of entities on the C++ Side?
I actually don't want to make dozens of copies of the same entity-type where everything stays the same but the values of the variables.
But I do need to keep entities in some kind of list. So I do need to let the drawable, physic object, and init-function-reference to be stored on C++ Side. And I also need to map all that stuff to one type of entity, maybe an entity ID. But the problems stays: How do I actually create entities now?
I don't want to load the same Lua script multiple times or something. I somehow have to copy some variables while letting other variables just be references, and everything coming from the same base entity... I know this reads confusing, but this is exactly the point where I get confused.
(Another small problem is that, since all local functions are event-system callbacks, I need some kinds of constraints. For example if I have a collide event, I don't want to trigger the callback in entity X for every collision in the game. Only for collisions involving Entity X.)
I hope you guys can understand what I mean and maybe help me :)
Kind regards,
Flyverse