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.

P#14393 2015-09-20 00:25 ( Edited 2015-09-23 22:21)

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

P#14394 2015-09-20 00:27 ( Edited 2015-09-20 04:27)

I feel incredibly silly, suddenly.

Thank you.

P#14395 2015-09-20 00:29 ( Edited 2015-09-20 04:29)

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

(yes, I read compiler warnings)

P#14420 2015-09-20 13:16 ( Edited 2015-09-20 17:16)

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

P#14421 2015-09-20 13:31 ( Edited 2015-09-20 17:31)

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.

P#14424 2015-09-20 13:58 ( Edited 2015-09-20 17:58)

@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

P#14467 2015-09-21 14:49 ( Edited 2015-09-21 18:49)

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

P#14468 2015-09-21 14:50 ( Edited 2015-09-21 18:50)

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
P#14541 2015-09-23 18:21 ( Edited 2015-09-23 22:21)

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-04-18 22:37:48 | 0.011s | Q:25