I’m kind of absolutely new to Defold, and I’m just trying to figure out how to get a button to only register that it has been pressed once without continuing to do more things afterwards. Also, strange things happen as I try to get the input to change a variable. It stays one value and just spams it in the debug. I’ve tried to make a boolean for the button but it’s not working…?
This is the code I’m working with:
if curstate == "title" then
if action_id == hash("up") and antibutton == false then
if action.pressed then
if curselect == "start" then
curselect = "quit"
end
if curselect == "quit" then
curselect = "option"
end
if curselect == "option" then
curselect = "start"
end
end
end
if action_id == hash("down") and antibutton == false then
if action.pressed then
if curselect == "start" then
curselect = "option"
end
if curselect == "option" then
curselect = "quit"
end
if curselect == "quit" then
curselect = "start"
end
end
end
-- if action_id == hash("up") and action.repeated or action_id == hash("down") and action.repeated then
-- antibutton = true
-- end
-- if action_id == hash("up") and action.pressed or action_id == hash("down") and action.pressed then
-- antibutton = true
-- end
-- if action_id == hash("up") and action.released and action_id == hash("down") and action.released then
-- antibutton = false
-- end
print(curselect)
Just one input, not trying to get an input every single frame. It’s supposed to be only one button press, then it has to wait until release and then it could go for another press.
One problem I noticed in your code is this section:
if curselect == "start" then
curselect = "option"
end
if curselect == "option" then
curselect = "quit"
end
if curselect == "quit" then
curselect = "start"
end
Let’s say curselect is currently set to "start". The top condition will be true, so curselect will then be set to "option". Now, the second condition is true, so curselect gets set to "quit". Then, the third condition is true, so curselect gets set back to "start". To fix that, you use elseifs like so:
if curselect == "start" then
curselect = "option"
elseif curselect == "option" then
curselect = "quit"
elseif curselect == "quit" then
curselect = "start"
end
I went ahead and fixed that, as well as simplifying the rest of the code, which should work now:
if curstate == "title" then
if action_id == hash("up") and action.pressed then
if curselect == "start" then
curselect = "quit"
elseif curselect == "quit" then
curselect = "option"
elseif curselect == "option" then
curselect = "start"
end
elseif action_id == hash("down") and action.pressed then
if curselect == "start" then
curselect = "option"
elseif curselect == "option" then
curselect = "quit"
elseif curselect == "quit" then
curselect = "start"
end
end
end
print(curselect)
P.S. Please take the time to format your code when asking questions, it makes it way easier for others to understand it and spot errors. You can add the fancy highlighting by wrapping it in backticks:
```
code goes here
```