How to assign an item dynamically

Started by oHInsane, January 19, 2017, 02:04:46 AM

Previous topic - Next topic
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.

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.

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



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.