Solarus-Games English Forum

Solarus => Development => Topic started by: dpro_games on August 02, 2016, 11:51:01 PM

Title: I have not understand the save of items (small_key)
Post by: dpro_games on August 02, 2016, 11:51:01 PM
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...
Title: Re: Pas compris sauvegarde des items (small_key)
Post by: Diarandor on August 03, 2016, 12:07:54 AM
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.
Title: Re: I have not understand the save of items (small_key)
Post by: dpro_games on August 03, 2016, 12:14:18 AM
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)
Title: Re: I have not understand the save of items (small_key)
Post by: Diarandor on August 03, 2016, 12:27:25 AM
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).
Title: Re: I have not understand the save of items (small_key)
Post by: dpro_games on August 03, 2016, 12:31:32 AM
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
Title: Re: I have not understand the save of items (small_key)
Post by: Diarandor on August 03, 2016, 12:37:08 AM
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.
Title: Re: I have not understand the save of items (small_key)
Post by: dpro_games on August 03, 2016, 12:46:43 AM
I have made juste one map with a door !

I give you my project...

http://www.mediafire.com/download/e4i1ad61k3enfio/The+legend+of+zelda+and+the+solitary+hero.zip
Title: Re: I have not understand the save of items (small_key)
Post by: Diarandor on August 03, 2016, 01:11:02 AM
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. :)
Title: Re: I have not understand the save of items (small_key)
Post by: dpro_games on August 03, 2016, 01:12:49 AM
Ok, thanks a lot !!!  ;)
Title: Re: I have not understand the save of items (small_key)
Post by: dpro_games on August 03, 2016, 01:22:30 AM
It is possible to increment this variable when we take a other key ?
Title: Re: I have not understand the save of items (small_key)
Post by: MetalZelda on August 03, 2016, 01:30:02 AM
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
Title: Re: I have not understand the save of items (small_key)
Post by: 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
Title: Re: I have not understand the save of items (small_key)
Post by: Diarandor on August 03, 2016, 01:42:32 AM
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.
Title: Re: I have not understand the save of items (small_key)
Post by: Diarandor on August 03, 2016, 01:46:41 AM
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)"
Title: Re: I have not understand the save of items (small_key)
Post by: Diarandor on August 03, 2016, 01:49:14 AM
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").
Title: Re: I have not understand the save of items (small_key)
Post by: MetalZelda on August 03, 2016, 01:55:06 AM
Yes, use another savegame variable for the amounts, otherwise the engine would be confused
Title: Re: I have not understand the save of items (small_key)
Post by: dpro_games on August 03, 2016, 02:01:47 AM
With this code, if I take two keys, I just can open one door after...  :(

How can I and where I need to inizialize a other variable for the amouts ?

I really sorry because I am a real beginners   :'(...
Title: Re: I have not understand the save of items (small_key)
Post by: MetalZelda on August 03, 2016, 02:05:02 AM
I don't remember if you need to use item:add_amount(item:get_amount() + 1) in on_obtaining() or if the engine does it automatically. I'm sure that this would fix your problem.
Title: Re: I have not understand the save of items (small_key)
Post by: Diarandor on August 03, 2016, 02:07:45 AM
I don't know because I have never used the key-door system. But what MetalZelda says is surely the correct thing to do.
Title: Re: I have not understand the save of items (small_key)
Post by: dpro_games on August 03, 2016, 02:10:10 AM
Oh thank !!! It works !!! It's the first time than I do something which works with solarus ! Thank you !  ;)
Title: Re: I have not understand the save of items (small_key)
Post by: MetalZelda on August 03, 2016, 02:10:42 AM
Also if you are a true begginer (we all passed this step), I recommand you to watch all of Solarus Tutorial videoes by Christopho

https://www.youtube.com/watch?v=DKLrGyMqvaU

These are a bit old and would be replaced by the new ones, but the engine is a bit similar and it would help you to understand the basics of the engine and the map editor.

If you want to make a game with Solarus (which I guess is what you're here for), I recommand you to learn the basics of lua
https://www.lua.org/pil/contents.html#P1
Title: Re: I have not understand the save of items (small_key)
Post by: Diarandor on August 03, 2016, 02:12:05 AM
You can also use the function "item:add_amount(amount)",
which should be equivalent to "item:add_amount(item:get_amount() + 1)".
Title: Re: I have not understand the save of items (small_key)
Post by: MetalZelda on August 03, 2016, 02:15:28 AM
Quote from: Diarandor on August 03, 2016, 02:12:05 AM
You can also use the function "item:add_amount(amount)",
which should be equivalent to "item:add_amount(item:get_amount() + 1)".

Yes that works too, I just didn't remember how it worked  ;D (It's been a long time since I didn't look at items)
Title: Re: I have not understand the save of items (small_key)
Post by: dpro_games on August 03, 2016, 02:17:22 AM
But there is an eroor in the consol with that : Error: In on_obtaining: [string "items/small_key.lua"]:16: bad argument #1 to add_amount (integer expected, got nil))
Title: Re: I have not understand the save of items (small_key)
Post by: Diarandor on August 03, 2016, 02:21:23 AM
As the error says "integer expected, got nil",
so write something like "item:add_amount(1)".

EDIT: when I wrote above:
QuoteYou can also use the function "item:add_amount(amount)",
which should be equivalent to "item:add_amount(item:get_amount() + 1)".
I wanted to say:
You can also use the function "item:add_amount(amount)",
which should be equivalent to "item:set_amount(item:get_amount() + 1)".

Sorry for the mistakes, it's quite late and we are all tired...  :-\
Title: Re: I have not understand the save of items (small_key)
Post by: MetalZelda on August 03, 2016, 02:21:47 AM
Quote from: dpro_games on August 03, 2016, 02:17:22 AM
But there is an eroor in the consol with that : Error: In on_obtaining: [string "items/small_key.lua"]:16: bad argument #1 to add_amount (integer expected, got nil))

integer expected means that eigher tou enterred a letter and not a number in add_amount()

In Diarandor's example, what he means is

Code (lua) Select
item:add_amount(amount)

amount can be replaced by a number, 1

So, it is

Code (lua) Select
item:add_amount(1)

Title: Re: I have not understand the save of items (small_key)
Post by: dpro_games on August 03, 2016, 02:28:12 AM
Realy thanks ! I can sleeping well now ! Bye ! Maybe later... to help me pass the beginner steps !  :D