Page 1 of 1
Calling strings from global script
Posted: Sat Sep 22, 2012 4:30 pm
by cromcrom
Hi all, The lost continent is coming along great, with a lot of new stuffs. However, in order to ease my scripts (crafting scripts and so on), I need to be able to store and retrieve strings from a global script entity.
So I did that:
1st SE :
"crom_variables"
Code: Select all
function setVar(var,number)
var = number
end
function addToVar(var,numbertoadd)
var = var + numbertoadd
end
function subFromVar(var,numbertosub)
var = var - numbertosub
end
try_1 = 0
2nd is whatever, called from a button
Code: Select all
function check()
crom_variables.setVar(try_1,1)
print(crom_variables.try_1)
end
but it returns 0 while I expect 1...
Re: Calling strings from global script
Posted: Sat Sep 22, 2012 5:02 pm
by Crisim
hi cromcrom
I think you may change the first script to something like this
Code: Select all
t = {}
function setVar(name, value)
t[name] = value
end
function getVar(name)
return t[name]
end
function addToVar(name, addValue)
t[name] = t[name] + addValue
end
function subFromVar(name, subValue)
t[name] = t[name] - subValue
end
then rewrite the check function like this
Code: Select all
function check()
crom_variables.setVar("try_1",1)
print(crom_variables.getVar("try_1"))
end
i've tested it with three lua entities, a lever to subtract and another to add
hope this help
crisim
Re: Calling strings from global script
Posted: Sat Sep 22, 2012 5:18 pm
by cromcrom
You bet it helps, its amazing, thank you so much. Is there a way to know what variable names are in use, like a list or something, without having to do it myself, because I don't even have to declare the variable with this script.
And thanks again a lot, its gonna make my scripts so much cleaner now

Re: Calling strings from global script
Posted: Sat Sep 22, 2012 7:46 pm
by Crisim
to check if a variable name is in use you can do
Code: Select all
function isVarDefined(name)
return (t[name] ~= nil)
end
for a list of defined variable names (stolen from
http://lua-users.org/wiki/TablesTutorial)
Code: Select all
function listVar()
for k,v in pairs(t) do
print(k,v) --k is the variable name, v is the value
end
end
Re: Calling strings from global script
Posted: Sat Sep 22, 2012 8:14 pm
by cromcrom
Thank you very much. Its already put to good use, and make my craft scripting so much more intuitive and easy
