Ask for help about creating mods and scripts for Grimrock 2 or share your tips, scripts, tools and assets with other modders here. Warning: forum contains spoilers!
function echoFunc()
workdamnit("green")
end
function workdamnit(yyz)
local yyzType = type(yyz)
local a;
if yyzType == "string" then
print("it is a string")
a = {}
table.insert(a, yyz)
end
print(type(a))
end
function echoFunc()
workdamnit("green")
end
function workdamnit(yyz)
if yyz ~= nil then
print("making headway")
local yyzType = type(yyz)
local a;
if yyzType == "string" then
print("it is a string")
a = {}
table.insert(a, yyz)
end
end
print(type(a))
end
######## RESULTS ##########
making headway
it is a string nil
I can really use some expertise on this, please.
Last edited by NutJob on Wed Oct 29, 2014 5:35 am, edited 1 time in total.
Since a is a local variable, it only exists inside its block. In the second example, you leave the block before calling type(a); since a is now out of scope, it is nil. To fix it, just move the declaration up:
function workdamnit(yyz)
local a
if yyz ~= nil then
print("making headway")
local yyzType = type(yyz)
if yyzType == "string" then
print("it is a string")
a = {}
table.insert(a, yyz)
end
end
print(type(a))
end
This whole time I assumed the local keyword confined it to the function block and it didn't matter if it was inside a conditional block. Never once crossed my mind.