Reputation: 1
I've searched everywhere for an answer on how to change a variable inside a function. I have tried many different solutions but none have worked so far.
My plan is to change a variable named 'stateRed' inside a function which is run when a button is pressed. When the function is run, the variables will change to 'READY'. I will then use this variable to run a separate chunk of code (the actual game). There are two of these buttons.
My code looks something like this:
function ReadyRed()
stateRed = 'READY'
stateBtnRed = widget.newButton
{
defaultFile = "ready_red.png",
overFile = "unready_red.png",
label = "Ready",
emboss = true,
onPress = unreadyStateRed,
}
stateBtnRed.rotation = 90; stateBtnRed.width = 90; stateBtnRed.height = 90; stateBtnRed.x = display.contentWidth / 2 - 170; stateBtnRed.y = display.contentHeight - 270
return stateRed
end
function UnreadyRed()
local stateRed = 'UNREADY'
stateBtnRed = widget.newButton
{
defaultFile = "unready_red.png",
overFile = "ready_red.png",
label = "Unready",
emboss = true,
onPress = readyStateRed,
}
stateBtnRed.rotation = 90; stateBtnRed.width = 90; stateBtnRed.height = 90; stateBtnRed.x = display.contentWidth / 2 - 170; stateBtnRed.y = display.contentHeight - 270
return stateRed
end
The code that starts the game looks like this:
if stateRed == 'READY' and stateBlue == 'Ready' then
Upvotes: 0
Views: 106
Reputation: 1702
If variable stateRed
is global then remove local
keyword stand before variable name i.e.
function UnreadyRed()
stateRed = 'UNREADY'
...
end
Note:
stateRed
in ReadyRed
and UnreadyRed
functions since stateRed
is global variable.Upvotes: 2