Situation:
I am working on an Entity-Component system, and I am using LuaBridge as a Lua binder.
There is only one Lua State.
Currently when I update the game objects, I just check whether it has a script defined or not. If there is, I call
luaL_dostring()on it, and I call itsupdate()function.I have a script, where I declare a global variable in its
start()(it is called when the script runs for the first time), and increment it at everyupdate()then print it out
Question:
I would like to achieve separated inner state saving for my scripts.
For example If I add the same script to two different game objects, currently the printed out numbers will be 1,2,3,4,5,6 instead of 1,1,2,2,3,3, etc.
This is because they use the same Lua State (correct me if I am wrong)
But as I heard it's not a good practice to have a lua state for every script because If I just have 5 scripts, and I add these to 3 different game objects, there would be already 15 lua states.
How should I achieve my goal?
lua_pcallon that value - otherwise you're paying the price of reparsing the script every time. I'd also recommending pcall'ing the initial script and putting its core logic into functions, so you only pay the cost for initializing the script's data once as well. \$\endgroup\$