I want to add full support for Lua scripting to my game engine. One of the biggest goals I set myself is to retain full modularity. That means, I don't want the rest of my engine, except for the Lua binding part, to use any Lua-specific functionality.
This has proven to be not any hard until now. The pure C++ to Lua functions binding part - like binding the Vector, Color and Entity objects, and modules such as the Draw library or Physics library are easily isolated from the rest of the engine.
Now, my problem lies in events. I need to provide a way for scripters to respond to game events. This involves calling Lua functions from the engine.
Examples: Call OnCreate
, OnUpdate
, OnCollide
on entities from C++ in Lua.
These events occur in my CEntity base class, and the Lua module has no way of knowing about when and how they happen.
Possible solutions:
Bad bad bad! That's Lua binding stuff mixed right into the base entity class!
I unfortunately don't know too much about this. I'm sure it would be quite difficult to implement and also bring some minor logical and performance problems.
- How can I trigger events (call a hook function) in Lua from within my C++ engine without having any Lua binding code outside the Lua binding module of my program?
There are tens of game engines using Lua with many developers using the language in these game engines. Here is why the Lua programming language is very popularly used in most game engines.
Game Code Complete (3rd Ed.) has a solution for that exact problem. Basically, he sets up the engine to have a generic event system and then has a scripting module that registers itself for the events it wants to respond to. The event system knows nothing about Lua, and the Lua wrapper only has to know the names of the events it wants to handle, so the coupling is very minimal.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With