I've been working with Lua for sometime and there is a problem that I could never solve it. First, I'm using a single lua state to have a connection between my scripts so they could transfer data via global table _G
.
My scripts have a template like this:
-- Vars
local somevar = 0
-- Update
function Update()
-- stuff
end
I call Update()
function from my application every game step, but since there are other scripts loaded in current lua state with the same template only the Update()
function from the last chunk in lua is getting called and there is no way I can call all of Update()
functions in my scripts.
If I load my scripts every time I want to call the functions, It'll work the way I want, but this way my local variables and every change that I have made to the scripts would get dumped because the chunk is replaced with a new one.
I've seen a message on Lua's site itself which talks about setting an environment for each chunk that is going to get loaded, but that's not what I want because it forces each script to reside in a unique table and there is no way to communicate with global table since we're going to override the global table for our unique tables.
I'd really appreciate if someone give me some advice because there isn't really much on the internet about these kinds of stuff.
UPDATE
I know the answer to first part of my question. Right now I'm using a table for each of my scripts, something like this:
code1.lua
code1 = {}
function code1.Update()
-- Update
end
This makes every script accessible via the global table with their table name. The problem is if I load this script for another object in my game, then it'll conflict with the previously loaded script because they're both using the same source and therefore the same names and I can only access the last loaded. As you can see, I've stated this about environments in first part of my question.