Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Isolate Lua bindings glue code from the rest of my game engine

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:

  • Add a function call to the events in the C++ base entity, which triggers an event in the Lua script

Bad bad bad! That's Lua binding stuff mixed right into the base entity class!

  • Add some sort of event system to my engine, where the entity events trigger an event which can also be catched by the Lua binding module and handled appropriately.

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?

like image 769
Jarx Avatar asked Feb 28 '11 19:02

Jarx


People also ask

Can you make a game engine in Lua?

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.


1 Answers

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.

like image 97
Zac Howland Avatar answered Sep 20 '22 05:09

Zac Howland