Reduce the number of Lua states used
crankycyclops opened this issue · comments
Right now, I'm creating a separate Lua state for each entity in the game. After doing some research, I've realized this is wasteful and unnecessary, so instead, I'm going to have only one Lua state per game. I'm going to tackle this in the following steps:
1. I need to remove the LuaState member from the Entity class and refactor the instantiator so that it loads all scripts into the Game's Lua state. I also need to refactor the way I instantiate LuaEventTrigger so that it uses the Game's state. DONE.
2. For organizational purposes, I need to change the way scripts are added to game.xml so that they are detached from specific things. Instead, <script> tags should be global. In fact, while other things in the xml file are in sections, <script> tags can just exist in global scope of xml file without a section, kind of like how it works with html. I can still bind events to Lua functions the way I'm doing now, and Entities can still have their own EventListeners like they already do. DONE.
3. I need to start using Lua environments. I'm thinking that an environment can be per script instead of per entity. This makes sense, as you would expect a standalone script to not have access to things outside of it. But I don't have to do this right away. EDIT: I'm not going to do this, at least for right now. It's reasonable for all external scripts to share a single global state.
Done and tested.