Item is not saved

Started by wizard_wizzle (aka ZeldaHistorian), June 09, 2015, 03:03:56 AM

Previous topic - Next topic
Occasionally  I've gotten error messages like the following while running my game:

Error: In on_started: [string "items/arrow.lua"]:13: Item 'bow' is not saved
Error: In on_started: [string "hud/rupees.lua"]:26: Item 'rupee_bag' is not saved

It doesn't seem to happen all of the time, and sometimes the "Item" mentioned is different. These items are calling self:set_savegame_variable() in their on_created() functions. Is there something else I'm doing wrong?

I guess the bow item is not saved yet at the moment you are in the "on_started" event of the arrow item.
This is because the arrow script is loaded before the bow script.

To solve this kind of issue, you can make two passes thanks to the existence of both events on_started() and on_created().

See the remark of http://www.solarus-games.org/doc/latest/lua_api_item.html#lua_api_item_on_created

So a solution is to initialize the bow in its on_created() event and the arrow in its on_started() event.

(Actually, a more simple solution solution is to initialize the bow outside of any event, and the arrow in its on_started() event. I realize that it was maybe useless to make two events in the API.)

I believe I already am. In fact, these scripts should be similar if not still identical to ZSDX.

Bow:
function item:on_created()
  self:set_savegame_variable("i1801")
  self:set_amount_savegame_variable("i1802")
  self:set_assignable(true)
end


Arrow:
function item:on_created()
  self:set_shadow("small")
  self:set_can_disappear(true)
  self:set_brandish_when_picked(false)
end

function item:on_started()
  -- Disable pickable arrows if the player has no bow.
  -- We cannot do this from on_created() because we don't know if the bow
  -- is already created there.
  self:set_obtainable(self:get_game():has_item("bow"))
end

This is very weird then. Is this a new problem of 1.4? I am pretty sure it works well in ZSDX with 1.4.

I believe it is new to 1.4. Like I said, it doesn't seem to happen all the time. I'll do some further testing and see what other information I can provide.

I have no idea why this kind of error can appear randomly.