Calling strings from global script

Talk about creating Grimrock 1 levels and mods here. Warning: forum contains spoilers!
Post Reply
User avatar
cromcrom
Posts: 549
Joined: Tue Sep 11, 2012 7:16 am
Location: Chateauroux in a socialist s#!$*&% formerly known as "France"

Calling strings from global script

Post 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...
A trip of a thousand leagues starts with a step.
User avatar
Crisim
Posts: 16
Joined: Wed Aug 29, 2012 2:28 pm

Re: Calling strings from global script

Post 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
User avatar
cromcrom
Posts: 549
Joined: Tue Sep 11, 2012 7:16 am
Location: Chateauroux in a socialist s#!$*&% formerly known as "France"

Re: Calling strings from global script

Post 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 :-)
A trip of a thousand leagues starts with a step.
User avatar
Crisim
Posts: 16
Joined: Wed Aug 29, 2012 2:28 pm

Re: Calling strings from global script

Post 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
User avatar
cromcrom
Posts: 549
Joined: Tue Sep 11, 2012 7:16 am
Location: Chateauroux in a socialist s#!$*&% formerly known as "France"

Re: Calling strings from global script

Post by cromcrom »

Thank you very much. Its already put to good use, and make my craft scripting so much more intuitive and easy :-)
A trip of a thousand leagues starts with a step.
Post Reply