2
\$\begingroup\$

Pico8 has a function time() that when called displays the current time from start of program.
i.e. print(time(),0,0,14) --prints time at (0,0) with colour 14

However the function doesn't stop and keeps drawing the time each frame. I'm trying to figure out how I would draw the time without it increasing/changing. So If I printed this 5 seconds from the start of the program I'd want it to display 5, but not change from that 5 value.

https://pico-8.fandom.com/wiki/Time I don't know how to store a static value of this time as a variable. Although according to the wiki, assigning var=time() will cause time at 0 to be stored.

Another way of phrasing this is...how would I display the time of 10 seconds when the following print is triggered? So after 10 seconds "time's up" is displayed. How would I display the current time 10, as well, as a static? Maybe I don't know what the time is when the event is triggered, so how can I count using time()?

function _init()
  last = time()
end

function _update()
  -- (empty update to use game loop)
end

function _draw()
  cls()
  if (time() - last) > 10 then
    print("time's up!", 44, 60, 7)
  end
end
\$\endgroup\$
1
  • \$\begingroup\$ I think you should be able to initialize a variable at 0, then on the callback you can check if it is 0, and if it is, set the time. I am not entirely familiar with Lua. \$\endgroup\$
    – Theraot
    Jul 22, 2019 at 23:46

2 Answers 2

1
\$\begingroup\$

You are doing it correctly by setting time() at a certain point as your own static variable.

Things you should know:

  • time() can be abbreviated to t()
  • time() is recorded as a float (a decimal) of seconds to ten-thousandths of a second.

To print what time your timer went off, first re-set your LAST variable to be the current time within your timer IF statement.

Then outside and after your timer IF statement, print the LAST variable.

function _init()
  last = time()
end

function _update()
  -- (empty update to use game loop)
end

function _draw()
  cls()
  if (time() - last) > 10 then
    print("time's up!", 44, 60, 7)
    last=time()
  end
  print(last, 44, 70)
end

If you don't want to print the time as a decimal and instead show only the seconds, then you can use the function flr() to round down like this:

  print(flr(last), 44, 70)

NOTICE: The only problem with this code, is that "TIME'S UP!" will only flash on the screen because I reset last to the current time. So the timer resets and flashes every 10 seconds and updates the static time variable.

\$\endgroup\$
0
\$\begingroup\$

I'm not sure what exactly are you trying to achieve, but maybe something like this?

local start
local limit = 10
local finish

function _init()
  start = time()
end

function _draw()
  cls()
  local now = time()
  local delta = now - start
  if delta >= limit then
    -- time's up
    print("time's up!", 44, 60, 7)
    -- saving timestamp
    if finish == nil then
      finish = now
    end
  else
    -- still ticking
    print(delta, 44, 60, 7)
  end
end
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .