Hello gamedev.net community!
I have a question about implementing a proper event listener. It shall be located in C++ but shall also get new event-listenings via Lua.
An event would be for example:
- Player walks into a set collision area.
- Player inputs a-button and is in reach of object x.
- Variable y inside the JSON-file reaches value 5.
and so on...
First of all, there are multiple level.lua-files that have a momentum where they describe those callback events.
I would like to store these event triggers in the C++-part of my engine, to avoid running Lua every iteration.
I thought about using an array of functions, storing functions that check if event conditions are met.
For example Lua calls a C++-function and passes arguments to it. The C++-function would then read out the arguements.
One arguement would determine what kind of function(-template) shall be selected and passes the other variables to it. These would be like which button to be pressed or the coordinates of that collision field.
An example on the Lua-side:
register_on_hit("name of this callback", x-coordinate, y-coordinate)
And somewhere in the map-file would be a function with the same name as the registered callback. This will be called by C++ once the function inside the function array with the id of "name of this callback" returns true.
Once all callbacks would be registered in the C++-array, the map-file closes and will be opened again when C++ realises that a function has been called.
When a new map-file will be loaded, the array will be cleaned of all registered potential events (except not map dependant events).
Is this a proper way of having an event listener? Are function arrays not recommended? Is there something I should do differently?