Discord
Login
Community
DARK THEME

Why won't the action from my button work?

I made a list for the buttons in my startscreen, but the action won't work

Here's my init for the button

startButtons = []
  startButtons.push(
    play = object
      x = 0
      y = -2
      width = 80
      height = 26
      size = 10
      text = "Play"
      sprite = "sprite39"
      color = "rgb(255,161,113)"
      action = function() mode = "play" end
      scale = 1
    end
    ) 

And the update code

for button in startButtons 
    button.scale = 1
    if checkRectMouseHover(button.x,button.y,button.width,button.height) then
      button.color = "rgb(255,161,113)"
      if mouse.press then
        button.action()
      end
      if mouse.pressed then
        button.scale = 1.1
      end
    else 
      button.color = "rgb(197,124,87)"
    end
  end

And then the way I called those, and the mode in the general init-function

init = function()
  mode = "start"
  initStartscreen()
end

I've also got this bit in the general update-function if it's relevant

if mode == "start" then
    updateStartscreen()
  
  elsif mode == "play" then
   ...

But the action still won't work, does anyone know why?

The rest of the code does work? The display, the hover, and the click itself?

Yes, all the other code works

Because of microScript's scoping, it's setting a property of the button object (play.mode) instead of a global variable. Use global.mode in the action instead.

Thank you, it works now

Post a reply

Progress

Status

Preview
Cancel
Post
Validate your e-mail address to participate in the community