Code: Select all
local random = math.random(2)
if random == 1 then
local xpos = party.x + 1 + math.random(3)
else
local xpos = party.x - 1 - math.random(3)
end
print(xpos)
Code: Select all
local random = math.random(2)
if random == 1 then
local xpos = party.x + 1 + math.random(3)
else
local xpos = party.x - 1 - math.random(3)
end
print(xpos)
Code: Select all
local random = math.random(2)
local xpos = 0
if random == 1 then
xpos = party.x + 1 + math.random(3)
else
xpos = party.x - 1 - math.random(3)
end
print(xpos)
Ah, ok. Guess using a local variable is pointless then as it will be 0 outside the if condition. Thought the only difference was that it's not written to the save game. Thanks!BeNNyBiLL wrote:your local variable xpos loses scope outside of the if/else loop. Outside of this loop, xpos is disposed of. This is the very definition of a local variable. It is not a problem, its just how you use it. The correct implementation would be
Code: Select all
local random = math.random(2) local xpos = 0 if random == 1 then xpos = party.x + 1 + math.random(3) else xpos = party.x - 1 - math.random(3) end print(xpos)
Ack...so non-local variables are not unique to scripts? If I declare a variable named 'counter' in script A and declare another one named 'counter' in script B, then I have effectively one variable named 'counter' only? So if script A sets the counter var to, say, 2 then it will automatically be 2 in script B as well? If that is the case then I guess I'll have to do a lot of renaming. But I guess the declaration block of a script runs only once when the script is triggered for the first time, right? EgKomag wrote:local things will be local to inside of whatever they're inside of. So if it's inside an "if" they will disappear after the "end" associated with that "if". If it's inside a "function" it will remain alive until the function is done with its "end"
They're useful because then you don't have to use a unique name compared to other scripts and functions you have going on.
Code: Select all
counter = 0
function blabla()
blabla
end
Code: Select all
function teletarget()
local xpos = math.random(4) + 26
local ypos = math.random(3) + 28
local face = math.random(4) - 1
catchtele1:setTeleportTarget(xpos,ypos,face,2)
catchtele2:setTeleportTarget(xpos,ypos,face,2)
catchtele3:setTeleportTarget(xpos,ypos,face,2)
end
Code: Select all
jump = 1
function getJump()
return jump
end
function setJump(value)
jump = value
end