if variable is nil, inside function

My coding mentality derives from a PHP background. And manipulating variables is much more forgiving with PHP.

I am passing a variable, as a function parameter. No worries there.

function checkMeOut(var)  
 if( var == "heaven" ) then  
 print("bring a chair");  
 end  
  
 local lastVarPassed = var;  
end  

Notice I declare a variable at the end of the function, saving the last string passed. I do this for a few reasons, but mainly to work with a condition inside the function. The condition is supposed to check if the lastVarPassed is the same as the next var being passed.

So here is what I would normally do, in a PHP environment:

function checkMeOut(var)  
  
 --check if last var matches the current var  
 if( lastVarPassed ~= var ) then  
 print("that's a new value than before!");  
 end  
  
 if( var == "heaven" ) then  
 print("bring a chair");  
 end  
  
 local lastVarPassed = var;  
end  

From what I can see, that condition is never detected. I may be missing something, like declaring the lastVarPassed in the wrong place.

Can anyone see if I’m doing something weird with that if statement? [import]uid: 154122 topic_id: 27343 reply_id: 327343[/import]

you will have to define the variable outside of the function if you want to save the last variable that was passed through the function. If you do it like you have it set up then lastVarPassed gets wiped every time the function is completed.

So something like this:

[lua]–Declare the variable outside of the function
local lastVarPassed = “empty”

function checkMeOut(var)

–check if last var matches the current var
if( lastVarPassed ~= var ) then
print(“that’s a new value than before!”);
lastVarPassed = var;
return true
end

if( var == “heaven” ) then
print(“bring a chair”);
end

lastVarPassed = var;
end

–Running this function with “heaven” should return the string “that’s a new value than before!” but when you run it a second time within the same session “heaven” will be saved as the last variable and should return “bring a chair”
checkMeOut (“heaven”)[/lua]

The only time you would want to define variables inside a function is if you do not need them outside of the function. In this case you will need it outside of the function because once the function is complete you will want to still have access to the variable. [import]uid: 126161 topic_id: 27343 reply_id: 111067[/import]