Log In  


So I recently was trying out PICO, messing to see what was possible, and was attempting to make a metal gear-styled game.

I wanted to try and limit how often PICO would read a button press. To do this I used the following code:

function framecount()
frame = 0
frame += 1
if frame > 30 then frame = 0 end
end

function controls()
if btn(0) then player.x-=1 end
if btn(1) then player.x+=1 end
if btn(2) then player.y-=1 end
if btn(3) then player.y+=1 end
end

function _update()
framecount()
if frame = 30 then
controls()
end
end

The result ends with the Lua parser not seeing the

then

placed after

if frame = 30

I know btnp can be used for this (though it would detect press every 4 frames, not every 30), but there are other things I want to tie into a frame delay and this was a way to test if the hack worked.



Equality test is ==. If you have further problems with the parser, try surrounding the expression with brackets.


I feel incredibly silly, suddenly.

Thank you.


I fall badly for the old assignment instead of equality trick about 3 times a year.

(yes, I read compiler warnings)


I'm reasonably sure there isn't a programmer in the world who hasn't fallen for that at least once.


Yeah, why do they even allow for that? I mean no reasonable person would try to assign value in if's condition (or any condition, like loop condition) so it should always assume = in if's condition is comparison.


@darkhog: It was useful in situations where you wanted to update a value in a loop and didn't want to rely on the compiler to be smart enough to cache the value. Ie:

while (ch = getNextCharacter()) do --[[ process ch ]] end


Side note, though, assignments are not allowed inside of expressions in lua. I don't know how this was even an issue.


Also worth pointing out (maybe) that you're resetting frame to 0 every time you call framecount(), you should put that "frame=0" line outside of the function so it only gets run once. eg:

frame=0
function framecount()
  frame += 1
  if frame > 30 then frame = 0 end
end

Or if you feel like being fancy you can put it in the _init() function, which is nearly the same, behaviorally:

function _init()
  frame=0
end


[Please log in to post a comment]