I have not understand the save of items (small_key)

Started by dpro_games, August 02, 2016, 11:51:01 PM

Previous topic - Next topic
August 02, 2016, 11:51:01 PM Last Edit: August 03, 2016, 12:06:39 AM by dpro_games
I have a problem to initialize the "small_key" save. :-\

I want to create a door in a dongeon which can be open with a small key but solarus display an error in the consol...I still read the documentation but i don't understand how can I do to solarus doesn't display that: : Error: In create_door: bad argument #1 to ? (Bad field 'opening_condition' (equipment item 'small_key' is not saved)).
:'( Thank to help me !!!

Sorry for the bad translation, I'm french...

August 03, 2016, 12:07:54 AM #1 Last Edit: August 03, 2016, 12:09:44 AM by Diarandor
Pouvez-vous coller ici une copie de tout le code de votre script où la fonction "create_door" se trouve ? Ça nous aidera trouver le problème.
Translation: could you paste a copy here of all code of the script where the function "create_door" is defined? That will help us to find the problem.

PS: il serait mieux si vouz écrivez en anglais (si vous pouvez) parce que la plupart des gens d'ici ne parlent pas le français, et plus de gens pourrait vous aider.
Translation: PS: it would be better if you write in English (if you can) because most people here do not speak French, and more people would be able to help you.
"If you make people think they're thinking, they'll love you. But if you really make them think, they'll hate you."

Code of the small_key:
local item = ...

function item:on_created()

   
    item:set_shadow("small")
    item:set_can_disappear(false)
    item:set_brandish_when_picked(true)
    item:set_sound_when_picked("picked_small_key")
end

function item:on_obtaining()
 
end


And the door is create with solarus with the option : hero need an item to open the door (small_key)

Hi again. This is not the script I was asking for (but it may help too).
We need to see the script where the function "create_door" is defined (it's probably your map script).
"If you make people think they're thinking, they'll love you. But if you really make them think, they'll hate you."

This is my map script :
-- Lua script of map Outdoor_hero_house.
-- This script is executed every time the hero enters this map.

-- Feel free to modify the code below.
-- You can add more events and remove the ones you don't need.

-- See the Solarus Lua API documentation:
-- http://www.solarus-games.org/doc/latest

local map = ...
local game = map:get_game()

-- Event called at initialization time, as soon as this map becomes is loaded.
function map:on_started()

  -- You can initialize the movement and sprites of various
  -- map entities here.
end

-- Event called after the opening transition effect of the map,
-- that is, when the player takes control of the hero.
function map:on_opening_transition_finished()

end

Nope, I was wrong, hehe, it is not in your map script, or at least not in this one.
Since you got this error:
Error: In create_door: bad argument #1 to ? (Bad field 'opening_condition' (equipment item 'small_key' is not saved))
we need to find the script where the function create_door is being used, because the error is happening there.
Does the error indicate in which file the error is it happening? (If so, that will be the script that we need to check.)

Edit: maybe the script we need is the script of another map, and not that one.
"If you make people think they're thinking, they'll love you. But if you really make them think, they'll hate you."


August 03, 2016, 01:11:02 AM #7 Last Edit: August 03, 2016, 01:13:11 AM by Diarandor
Ok, I found the problem (I have never used a built-in door since I use my custom ones, so I did not know about this thing).

The problem is that the amount of keys you have has to be saved in some variable, and you have not indicated to the engine in which variable (this has to be done for any type of item, so you may get the same problem for other items you create).
You just need to add something like this
item:set_savegame_variable("id_for_the_amount_of_keys")
inside the function "item:on_created()".
(you can use there any string you want (so change the string to a shorter one if you want), and that variable will be used for all keys of your game).

You will have something like this:
function item:on_created()

  item:set_savegame_variable("id_for_the_amount_of_keys")   
  item:set_shadow("small")
  item:set_can_disappear(false)
  item:set_brandish_when_picked(true)
  item:set_sound_when_picked("picked_small_key")
end


You can now delete the previous link to your data in mediafire.

PS: you can also ask on the IRC chat whenever you want, and don't hesitate to ask if you have any problem. :)
"If you make people think they're thinking, they'll love you. But if you really make them think, they'll hate you."


It is possible to increment this variable when we take a other key ?

August 03, 2016, 01:30:02 AM #10 Last Edit: August 03, 2016, 01:31:43 AM by MetalZelda
Quote from: dpro_games on August 03, 2016, 01:22:30 AM
It is possible to increment this variable when we take a other key ?

Just add in item:on_created()

Code (lua) Select
self:set_amount_savegame_variable("your_variable")

http://www.solarus-games.org/doc/1.5/lua_api_item.html#lua_api_item_set_amount_savegame_variable

There is a lot of way to do it, you should take a look at how Mystery of Solarus DX manage small keys, this is the best way to do small keys

It does not work...
Where I need to add this ligne ?

local item = ...

function item:on_created()

  item:set_savegame_variable("small_key_save")
  self:set_amount_savegame_variable("small_key_save")
  item:set_shadow("small")
  item:set_can_disappear(false)
  item:set_brandish_when_picked(true)
  item:set_sound_when_picked("picked_small_key")

end

August 03, 2016, 01:42:32 AM #12 Last Edit: August 03, 2016, 01:50:06 AM by Diarandor
Yes. In that variable the engine saves the amount of keys you have.
You should take a look to the functions in the Lua API:
Code (Lua) Select
item:get_amount_savegame_variable()
item:set_amount_savegame_variable()

here
http://www.solarus-games.org/doc/1.5/lua_api_item.html#lua_api_item_methods

By the way, @Christopho: there is something wrong in the API, for the function "item:set_amount_savegame_variable()", where it says:
"Returns the name of the integer savegame value that stores the amount of this item."
I think that line should be removed, the last paragraph
"Return value (string): The savegame variable that should store the possessed amount of this item, or nil to make this item have no associated amount."
seems to be wrong too, and the function should appear as
"item:set_amount_savegame_variable(savegame_variable)",
if I am not wrong.

EDIT: whoops, I posted too late. You already read the API.
"If you make people think they're thinking, they'll love you. But if you really make them think, they'll hate you."

I was wrong in what I said before:
"item:set_savegame_variable(savegame_variable)" is used for the possesion, not the amount. For the amount the engine uses:
"item:set_amount_savegame_variable(savegame_variable)"
"If you make people think they're thinking, they'll love you. But if you really make them think, they'll hate you."

Quote from: dpro_games on August 03, 2016, 01:38:46 AM
It does not work...
Where I need to add this ligne ?

local item = ...

function item:on_created()

  item:set_savegame_variable("small_key_save")
  self:set_amount_savegame_variable("small_key_save")
  item:set_shadow("small")
  item:set_can_disappear(false)
  item:set_brandish_when_picked(true)
  item:set_sound_when_picked("picked_small_key")

end


You should use a different variable for the amount, so you should not use the same variable for both things (the variable "small_key_save").
"If you make people think they're thinking, they'll love you. But if you really make them think, they'll hate you."