Log In  

I can't wrap my head around this issue, because it really seems like there is none, and I feel like either I am going schizo or PICO-8 is.

I'm working on a prototype platformer to learn a bit.

Basically, the logic of the animation I came up with is making a timer that could count up to 10 by 0.5 per frame in the update() function (so, basically, a timer that went up by 1 every two frames).

I then set up my commands function so that every time i press either left or right, the player not only moves in that direction, but also changes sprites in its spr() function (the player sprite is saved in the PL table under the SPT variable).

This is done by calling a custom function, anim() in the if statements of the commands() function. anim() is a custom function that basically changes the PL.SPT (the player sprites) based on the value of the timer I spoke about earlier.

So far so good. Surely not the most efficient way to pull this off, but still, glad I worked it out. Except I didn't, because even though it works with the left button, I CANNOT get it to work with the right button. I just cannot find a reason for the left button to work and for the right button not to, but it just doesn't and tbh I'm going crazy because I really can't see an issue with my code (except extremely high inefficiency :D).

I know this is not the best way to pull off the animation, but now its beyond my point. I REALLY wanna know what's wrong with this code above all else.

ALSO the AND X<10 in the BTN if Statements are useless, I already realized that.

Cart #feguzakuhe-0 | 2023-07-29 | Code ▽ | Embed ▽ | No License

P#132542 2023-07-29 22:05 ( Edited 2023-07-29 22:13)

1
    if (btn(➡️)) and fps<10 pl.x+=pl.spd pl.flp=false anim() else pl.spt=66
    if (btn(⬅️)) and fps<10 pl.x-=pl.spd pl.flp=true anim() else pl.spt=66

The else in at least one of these lines is always going to happen. The reason moving left works is that the top else happens beforehand so the bug ends up not mattering.

The fastest fix is to just check for neither separately like so:

    if (btn(➡️)) and fps<10 pl.x+=pl.spd pl.flp=false anim()
    if (btn(⬅️)) and fps<10 pl.x-=pl.spd pl.flp=true anim()
    if (not(btn(➡️) or btn(⬅️))) pl.spt=66
P#132543 2023-07-30 00:15

Thank you, I've tried this solution and it fixes it.

Though I still didn't understand the reason behind that else thingy. Could you elaborate on that? I feel like I'm missing a big part of how if statements work and I don't even know. Are there good resources for that?

P#132545 2023-07-30 01:20
1

It's easier to explain when not using shorthand, so first, here's what your code would look like when using normal Lua.

    if (btn(➡️)) and fps<10 then 
      pl.x=pl.x+pl.spd pl.flp=false anim() 
    else 
      pl.spt=66
    end
    if (btn(⬅️)) and fps<10 then
      pl.x=pl.x-pl.spd pl.flp=true anim() 
    else 
      pl.spt=66
    end

The 1-line version just skips the words then and end, by figuring out where they would be based on the parentheses and the end of the line.

If the expression between the if and the then evaluates to true, then the code between then and the next keyword is run. If it evaluates to either false or nil, then code between else and end is run, as long as else is there.

There were four relevant situations that your code was handling: 1. pressing right, 2. pressing left, 3. pressing both and 4. pressing neither. When pressing right, the animation would update properly in the first line, but then be reset when the second line reached the else. When pressing left, the animation would be reset by the first line, but then be updated properly in the second line. When pressing both, the animation would be updated twice. When pressing neither, the animation would be reset twice.

P#132551 2023-07-30 04:57

Here's the thing, if the IF statement evaluate to true, shouldn't the code between else and end just be ignored?

My understanding is that the software would run the part starting from pl.x onward, and then ignore the else as long as I'm pressing the button, but as sson as I stop pressing it, the code would get back to the else part of the statement, thus resetting the player sprite to the idle sprite.

Sorry if I'm being stubborn on that but it's just that my fundamental understanding of If statements in PICO-8 clashes with the way the software actually acts.

You mention that the code after else is run as long as the If conditions turn out to be false, which is the same way I understood it when I first studied it. But shouldn't pressing the right button be giving a True value back, therefore ignoring the else code?

Sorry if I'm bring dense and slow-witted and thanks for your time, your help was already more than enough and kindly appreciated, is just that I can't wrap my head around the logic

P#132562 2023-07-30 11:11

What you described is what the code is doing. The problem is that your original version had 2 separate if statements, both of which had their own else. The first one checked the right button, then branched appropriately. Then, after that, the second one checked the left button and branched appropriately.

Since you normally don't press both right and left at the same time, at least one of those branches was running an else.

P#132563 2023-07-30 11:29

Ooooh, ok maybe I got it

If the right button If returned a TRUE value, then the code would execute the THEN value but the ELSE value of the left button, as the left button wasn't pressed and constatly returned a FALSE, therefore leading to the ELSE statement.

On the left button IF statement, though, the ELSE of the right button's If statement was "overshadowed" by the left button's IF, returning a TRUE value and therefore executing the THEN statement. So, the issue didn't present itself on the left button being the left button's ELSE statement also the last ELSE statement in the functiin

Probably didn't make it clear in this post lmao, but yeah I think I got it now.

Thank you so much! Your help was massive

P#132602 2023-07-31 14:51 ( Edited 2023-07-31 14:53)

[Please log in to post a comment]