3
\$\begingroup\$

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 its update() 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 every update() 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?

\$\endgroup\$
  • \$\begingroup\$ "I call luaL_dostring() on it" - you really want to precompile that, cache the resulting function using the Lua registry, and use lua_pcall on 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\$ – Sean Middleditch May 22 '17 at 22:22
  • \$\begingroup\$ Check eliasdaler.wordpress.com/2015/08/10/… \$\endgroup\$ – Ian Young May 24 '17 at 13:11
  • \$\begingroup\$ I couldn't really fix my issue :( \$\endgroup\$ – Tudvari Jul 4 '17 at 21:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.