Author Topic: Item is not saved  (Read 2709 times)

wizard_wizzle (aka ZeldaHistorian)

  • Sr. Member
  • ****
  • Posts: 263
    • View Profile
Item is not saved
« on: June 09, 2015, 03:03:56 am »
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?

Christopho

  • Administrator
  • Hero Member
  • *****
  • Posts: 1183
    • View Profile
Re: Item is not saved
« Reply #1 on: June 09, 2015, 09:12:24 am »
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.)

wizard_wizzle (aka ZeldaHistorian)

  • Sr. Member
  • ****
  • Posts: 263
    • View Profile
Re: Item is not saved
« Reply #2 on: June 09, 2015, 02:03:32 pm »
I believe I already am. In fact, these scripts should be similar if not still identical to ZSDX.

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

Arrow:
Code: [Select]
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

Christopho

  • Administrator
  • Hero Member
  • *****
  • Posts: 1183
    • View Profile
Re: Item is not saved
« Reply #3 on: June 10, 2015, 10:57:24 am »
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.

wizard_wizzle (aka ZeldaHistorian)

  • Sr. Member
  • ****
  • Posts: 263
    • View Profile
Re: Item is not saved
« Reply #4 on: June 10, 2015, 01:58:23 pm »
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.

Christopho

  • Administrator
  • Hero Member
  • *****
  • Posts: 1183
    • View Profile
Re: Item is not saved
« Reply #5 on: June 10, 2015, 02:25:37 pm »
I have no idea why this kind of error can appear randomly.