local tempVal = ItemPrice
if tempVal == 0 then
tempVal = "free"
elseif tempVal <= 1 then
tempVal = tempVal
else
warn("not a number (somehow)")
end
currentButton.Button.ppr.ActionText = string.format(currentButton.Button.ppr.ActionText.." for %s$", tostring(tempVal))
I have a tempVal to use temporarily, but I am worried that I might need multiple of those in a single script, but I do not want to change the names of every single (future) temporary variables.
I tried searching in this forum and may others but they all just said that Lua has Garbage Collection, but did not specify if it has or not.
In your code, if tempVal is only used within the local block, then it will automatically be deallocated at the end of that block. You don’t need to explicitly drop the variable.
If you want to reuse the same variable name in multiple portions of your code, you can simply declare it with the local keyword again in the new block, and Lua will create a new instance of the variable within the new scope. The old instance of the variable will be deallocated automatically by Lua.
And if it’s not inside of a block already, you can also make a block:
do
local tempVal = ItemPrice
if tempVal == 0 then
tempVal = "free"
elseif tempVal <= 1 then
tempVal = tempVal
else
warn("not a number (somehow)")
end
currentButton.Button.ppr.ActionText = string.format(currentButton.Button.ppr.ActionText.." for %s$", tostring(tempVal))
end
However, based on the currentButton bit and the indentation, I assume this is already inside of a block.
You shouldn’t name your variable a tempVal in this context, it’s a variable like any other so you should just give it a real name so you and other people know what it’s used for.
If you’re wondering about the variable taking up memory: If the variable isn’t referenced anymore after you used it, any memory that was taken by that variable will be freed later on. The garbage collector does this automatically. This doesn’t apply for instances references that are held by that variable, they need to be destroyed and dereferenced to be garbage collected.
warn(“not a number (somehow)”)
Since you just used the lower than operator on tempVal, it will always be a number. If it wasn’t it would have errored unless its a metatable with the __lt metamethod in it, but I don’t think that would ever happen if you don’t use any of that in your code.