Solarus-Games English Forum

Solarus => Development => Topic started by: oHInsane on January 19, 2017, 02:04:46 AM

Title: How to assign an item dynamically
Post by: oHInsane on January 19, 2017, 02:04:46 AM
Hi,

I'm getting my hands on solarus and i'm currently reading a lot of documentation and tryng out other projets to understand how far can solarus go.

1) So my first question is: i want to give directly the boomerang at the start of my quest.

I have tried this code

  local item = game:get_item("boomerang")
  game:set_item_assigned(1, item)


But i got the error: "Cannot assign item 'boomerang' because the player does not have it".

So my guess is that i need to instantiate the boomerang first but how?



Thank you.
Title: Re: How to assign an item dynamically
Post by: Christopho on January 19, 2017, 08:57:47 AM
Use the item API like this:
Code (lua) Select
game:get_item("boomerang"):set_variant(1)

The code you tried associates the boomerang item with the first command slot, which is only possible for items the player has.
Title: Re: How to assign an item dynamically
Post by: oHInsane on January 19, 2017, 04:56:29 PM
Hi thanks for your anwser.

So my code is now!

function game_manager:start_game(file_name)

  local exists = sol.game.exists(file_name)
  local game = sol.game.load(file_name)
  if not exists then
    -- Initialize a new savegame.
    initial_game:initialize_new_savegame(game)
  end
  game:start()
  --local item = game:get_item("boomerang")
  --game:set_item_assigned(1, item)
game:get_item("boomerang"):set_variant(1)

end


The error is gone but when I press X, nothing is happening, do i have to bind a key like with
set_command_keyboard_binding() ?

I'm quite lost with the item management in general  (equipement), is there a game array where all the equipements that i have is stored or do i have to code it? How do i add en item to the equipment (and remove it if needed)?

Thank you


Title: Re: How to assign an item dynamically
Post by: oHInsane on January 20, 2017, 01:46:29 AM
Thanks, I understand better now.

I modified my code to :

game:get_item("boomerang"):set_variant(1)
game:set_item_assigned(1,game:get_item("boomerang"))


And it words fine with the default commande X.

Thank you, I've also sent you a PM christopho about the engine as well.