PICO-8 making a button press show an output of text only once?

1.1k Views Asked by At

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
1

There are 1 best solutions below

2
On BEST ANSWER

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