Referencing global timers

Started by wizard_wizzle (aka ZeldaHistorian), May 18, 2014, 09:07:11 PM

Previous topic - Next topic
If I have a timer that I've declared in the "game" context, and my character moves to a different map while the timer is still active, I can't figure out how to reference that timer.

race_timer = sol.timer.start(game, 140000, end_race_lost)

Timer declared as above. I know the timer is still active when the map changes because the sound still plays. I also have the time displayed on screen using race_timer:get_remaining_time(). When the hero changes maps, the timer display disappears.

  if race_timer ~= nil then
    function map:on_draw(dst_surface)
      local timer_icon = sol.surface.create("hud/timer.png")
      local timer_time = race_timer:get_remaining_time() / 1000
      local timer_text = sol.text_surface.create{
        font = "white_digits",
        horizontal_alignment = "left",
        vertical_alignment = "top",
      timer_icon:draw(dst_surface, 20, 55)
      timer_text:draw(dst_surface, 40, 60)

The display is disappearing because the timer reference goes to nil, which I'm intentionally checking for. If I comment out the check, I get an error message of "attempt to index global 'race_timer' (a nil value), but the value shouldn't be nil since the timer is in a "game" context, right? Should I be referencing it a different way, or is this a bug in the engine?

May 18, 2014, 09:44:22 PM #1 Last Edit: May 18, 2014, 10:03:10 PM by Christopho

There seems to be a bug in the engine: when you declare a global variable in a map script, and try to access it from another map script, it is nil. So this is not a timer problem.
I never found this bug before because I don't use global variables :)

I am going to fix it, but in the meantime, what you can do is declaring this variable in the game object. This is also better because you will avoid a global variable.

game.race_timer = sol.timer.start(game, 140000, end_race_lost)

if game.race_timer ~= nil then

EDIT: fixed!
Thanks for the report.

May 18, 2014, 11:30:54 PM #2 Last Edit: May 19, 2014, 03:52:02 AM by wrightmat
Wow, that was fast! Thanks Christopho!

EDIT: Even with the new code in place, the callback of the timer (in the case above, "end_race_lost") won't run on another map. Erro is "bad argument #3 to '__index' (This map is not running).

May 19, 2014, 09:22:26 AM #3 Last Edit: May 19, 2014, 09:26:48 AM by Christopho
In your function end_race_lost, you are probably looking up names that refer to map entities from the previous map. Since that map is not running anymore, this does not work.

For example, assuming that you have a map entity called my_npc in the map where the timer is created:
local function end_race_lost"too_late")

It does not make sense to access my_npc when its map is no longer the current map.
But here is what happens exactly: the name my_npc was not declared before: therefore, it is resolved lazily when the function end_race_lost is executed. This generates the cryptic error that you get. In 1.3, it will get a nil value instead of this weird error message.

There is a way to access it anyway:

local some_npc_safe = some_npc  -- Resolve the entity while the map is still running.
local function end_race_lost"too_late")

or if you prefer:

local some_npc = map:get_entity("some_npc")  -- Resolve the entity while the map is still running.
local function end_race_lost"too_late")

Anyway, in your case, I don't think you really want to access entities from the previous map. So instead, you should probably just check if the map is the current one, with something like:

local function end_race_lost"too_late")
  if map == game:get_map() then  -- If this is still the current map.

Hmm... I think I understand what you're saying. My case is a tricky one though. It's basically a race that takes place over two different maps. So regardless of which map you're on, when the timer runs out, you lose the race. This is why I was hoping the timer callback would be persistent. I had even declared that end_race_lost function in each map, and it still didn't appear to be working. For reference, here's the function (those torch entities exist on both maps):"wrong")
game:set_value("i1028", 4);
game.race_timer = nil

Do you know of any way to make this work?

The timer callback is persistent indeed. However, the callback you pass to the timer is a function value, not a string. Its name has no importance: the timer stores its code. (All functions are anonymous in Lua, but you can assign a function to a variable). So the end_race_lost function that you declare on the second map has no effect. The one of the first map is the one called, even if you are in the second map. This is why you have the bug.

So, if I understand correctly, torch_1, torch_2 and torch_3 exist on both maps? And you want to unlight them? If yes, here is what you can do:"wrong")
game:set_value("i1028", 4)
local map = game:get_map()
game.race_timer = nil

This callback should work for both maps :)

PS: "i1028" is a poor savegame variable name, you can use more meaningful names for your savegame variables. ZSDX use this kind of names for historical reasons (in very old Solarus versions, savegame variables were identified by numbers only).

Works perfectly now - you're amazing! Thanks!

As far as the savegame variables - I fall into the same category as ZSDX on this one. I've been following the engine from the beginning and developing very early on, so I already had a large number of savegame variables established and it was easier to stick with the numbers. I have a system in place now and I actually like them! :-)

Quote from: wrightmat on May 20, 2014, 03:19:33 AM
I've been following the engine from the beginning and developing very early on
Wow! That's right, I found an e-mail from you of June 2010. The engine and the API have changed a lot since then! I'm glad to realize that you successfully upgraded to each new version and always carried on :)

Still chugging along! Thanks your all your great work on this engine!