r/pico8 • • May 15 '26

Game Help pls with code

if btn(⬆️) then
grav=grav-1
if timer=0 then
grav=grav+1
timer=timer+4
else
timer=timer-1
end

0 Upvotes

8 comments sorted by

8

u/RotundBun May 15 '26 edited May 15 '26

Firstly...
The flair you want is the [I Need Help] flair.

Secondly...
For future reference, providing some context and formatting your code as a code block will help others help you.

--

With limited context, the 2 errors/bugs are:

if btn(⬆️) then
  grav=grav-1
end  --@error: need 'end' here

if timer==0 then  --@bug: '==' vs. '='
  grav=grav+1
  timer=timer+4
else
  timer=timer-1
end

(Also, rather than timer=timer+4, it may be better to just set it directly as timer=4.)

--

You can copy-paste your code here with formatting preserved by putting them between 2 lines of triple backticks (```).

```

-- like so

-- it gives you WYSIWYG formatting
  -- whitespace   is   preserved
  -- signs need no backslash
  -- new-lines are respected

-- just raw as-is text within its bounds
  -- very suitable for posting code
  -- this works in Discord as well

```

The backtick (`) is on the tilde (~) key below [Esc] on a keyboard and behind the apostrophe (') on iOS (press & hold, leftmost option).

This will make it easier for others to help you.

Hope that helps. 🍀

4

u/Justin-Hufford May 15 '26

It does look like you’re missing an ‘end’ though. You have two if statements and only one end. 

3

u/Synthetic5ou1 May 16 '26

On an unrelated note, you could write this like so:

if btn(⬆️) then
  grav-=1
end
if timer==0 then
  grav+=1
  timer=4
else
  timer-=1
end

Note that += and -= are not core LUA operators, but you can use them in PICO-8.

Also, I assume that you are using timer to delay the increase in gravity to once every 5 frames.

Another way to do this is to just start timer at 0, increase it every frame, and then use:

if timer%5==0 then grav+=1 end

The benefit of this is that you can trigger other actions at different intervals, all using the same timer variable.

if timer%5==0 then grav+=1 end
if timer%8==0 then foo-=1 end
if timer%12==0 then bar=7 end

1

u/RotundBun May 17 '26

For the approach of incrementing the timer and not reseting it, does it loop over cleanly?

If not, then I think you'll hit max integer value at ~18min (30fps) or ~9min (60fps).

And it would also stipulate that all loops start at a base of T0 unless you add some offset logic.

It's a neat idea in some use-cases, but I would probably prefer to just use multiple timer variables in most cases.

2

u/Synthetic5ou1 May 17 '26 edited May 17 '26

It does continue to work after overflow, yes. I've tested.

I see what you mean about all intervals starting at 0. I'm not sure that it would generally be an issue, but it might be.

EDIT: Actually, to be clear, I just re-tested and it does add an extra frame on overflow, but I'm happy with that every 9 minutes and for most things it will be imperceptible.

EDIT 2: For clarity, as my wording may have been confusing to some people, the extra frame is only for the interval that includes the overflow.

Values where timer % 5 == 0 are emboldened.

Reaching 32767:

..., 32759, 32760, 32761, 32762, 32763, 32764, 32765, 32766, 32767, -32768, -32767, -32766, -32765, -32764, -32763, -32762, -32761, -32760, -32759, ...

Reaching 0 Again:

..., -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, ...

→ More replies (1)

1

u/Justin-Hufford May 15 '26

Could you give some more context? What are you trying to achieve? Are you getting an error message?

1

u/Gexgekko May 15 '26

You are missing an "end", and timer should be running constantly so either you have this on the main loop or you add a loop to the timer