Log In  

changelog says that time() has been removed

but I can still use it?

though it does seem to behave a bit oddly by eventually returning negative time?

any ideas?

P#21247 2016-05-24 10:12 ( Edited 2016-07-14 19:39)

It still works as of 0.1.6. But since Pico-8 numbers only go up to 32767, it's going to roll around to a negative number after the console has been running for a little over 9 hours, I think. You could write a wrapper around time() to handle this.

P#21265 2016-05-24 18:26 ( Edited 2016-05-25 11:05)

thanks for the info.

i see negative numbers much sooner than that.

anyway, i ended up rolling my own tick counter

P#21268 2016-05-24 19:15 ( Edited 2016-05-24 23:15)

Yes it's actually a tick counter itself I think. So it should roll around to a negative after 32767 miliseconds. Which are almost 33 seconds. Which isn't that great! But handling it yourself is definitely a good way to go!!

P#21271 2016-05-24 20:31 ( Edited 2016-05-25 00:31)

time() isn't a tick counter -- do print(time()) in the console and you can see it prints seconds from console start. (On my system, at least.)

P#21293 2016-05-24 23:27 ( Edited 2016-05-25 04:01)

From what I understand, time() isn't going to be your best bet for a lot of things because of that stupid negative number issue. Instead, you can do a number of make-shift things to effectively create your own estimation that's more suited to your game's particular needs.

For instance, there is a game type where the player gains incremental income based on purchased point-earning items. The player receives point income per second based on how many they own and the point value associated with them. My included cart shows how one can use the _update function as a counter and correctly count up to a second. Since _update gets called 30 times per second, incrementing a global value to 30, performing the point increment, and returning the global variable back down to zero then allows for the 'tick' effect without needing to know the time.

-- time fudging test
-- by vlek

points = 0
time_tracker = 0

function _update()
  if time_tracker < 29 then
    time_tracker += 1
    time_tracker = 0
    points += 1

function _draw()
P#21418 2016-05-26 18:20 ( Edited 2016-05-26 22:23)

Hey guys, if the number limitation is 32767... how do your tick counter compensate it?
I mean, if you're at tick #32765 and have something to do in 5 ticks, how do you manage the fact that your counter will either go to -32767 in 2 ticks or 0 if you decided to reset it?


P#25295 2016-07-14 05:49 ( Edited 2016-07-14 09:49)

All variables wrap at 32767. If you want to fire off an event in a few ticks, just set its tick to what the tick number will actually be in a few ticks, wrapping included.

Like, in your case, if cur_tick is 32765 and you do "event_tick=cur_tick+5", you'll find that event_tick wraps and ends up being -32766, which is what cur_tick will be in 5 ticks.

You do have to make sure you compare for equality and not less-than/greater-than. That means you have to process your event queue every tick so you never miss the tick where cur_tick==event_tick.


If you really want more numbers before your game's tick count wraps, you can count with the smallest fractional increment ( 0x.0001):

cur_tick = 0

function _update60()
  if btnp(0) and not event then
    event = {
      tick = cur_tick + 0x.0005,
      damage = 100

  if event and event.tick == cur_tick then
    event = nil

  cur_tick += 0x.0001   -- smallest possible increment

function _draw()
  -- switch between blue and black about once per second
  if band(cur_tick, 0x.0040) == 0 then
P#25297 2016-07-14 07:22 ( Edited 2016-07-14 11:23)

Thanks a lot Felice!

P#25298 2016-07-14 07:31 ( Edited 2016-07-14 11:31)

You could also do event_tick-cur_tick <= 0 and get an inexact comparison, yes? As long as the difference you're looking for is smaller than 32765 ticks. The wraparound preserves the difference.

P#25309 2016-07-14 11:56 ( Edited 2016-07-14 15:56)

True, dddaaannn, you can always look at the delta for short-term delays, since it'll handle wraps correctly. I just don't like to use 16-bit frame counters that way since I've been burned by them in the past on games that had very long delays on things like spell cooldowns.

P#25310 2016-07-14 15:39 ( Edited 2016-07-14 19:39)

You can also have an infinite ticker by using a sprite as a metronome.

function tictac()

tic = false

metronomo.x += 4

if metronomo.x > 128 then
   metronomo.x = 0
   tic = true;

return tic


If you add tictac() to the _update you'll have a working metronome. You can decide wether you want to show it on screen or not.

P#61036 2019-01-20 18:01

[Please log in to post a comment]