1

I'm a complete newbie to both Lua, PICO-8, and coding in general. I'm having trouble with a function I want to put in my first program. The text is all placeholder, I will change it once I get the code right and comprehend it.

Basically, before the _init() I have a function ow() defined where I press a button and the program displays the text "ow." I put the function name in _update() so that it will update 30x/second to see if the button is pressed; however, this is making the "ow" appear 30 times a second (or however long the button is pressed) instead of appearing once when I initially press the button. How do I fix this? Thank you for your tolerance of a new coder's question in advance. Here's my code:

function ow()


if btn((X))
then print "ow"
     --how do i make it do this
     --only once?

end

end

function _init()
print "hello."

print "i have been waiting for you."

end

function _update()

ow()

end


function _draw()

end
B. Hoyt
  • 15
  • 6

1 Answers1

2

You need a global variable to save previous status of the button.

function ow()
   if btn((X)) then
      if not button_was_pressed then 
         button_was_pressed = true
         print "ow"
      end
   else
      button_was_pressed = false
   end
end
Egor Skriptunoff
  • 906
  • 1
  • 8
  • 23
  • 1
    For future users: make sure you hold shift down when typing "x" into the btn function (or just put "5," which also corresponds to the x key in-game) so that it will register that you're specifying that button. – B. Hoyt Sep 15 '18 at 03:41
  • OR use BTNP(x). – B. Hoyt Nov 13 '18 at 14:55