Author Topic: Check Item  (Read 2988 times)

Yruama

  • Newbie
  • *
  • Posts: 24
    • View Profile
Check Item
« on: December 03, 2014, 12:00:45 am »
Hi,

I'm sure it's not difficult but i don't find what i want ...

How can i check if the player have a X item ?
For example, a NPC block the path if the player have not the lamp.

Thanks

Renkineko

  • Jr. Member
  • **
  • Posts: 81
    • View Profile
Re: Check Item
« Reply #1 on: December 03, 2014, 07:57:21 am »
With game:get_item():get_variant() you can check the possession value of the item :)

http://www.solarus-games.org/doc/latest/lua_api_item.html#lua_api_item_get_variant

Christopho

  • Administrator
  • Hero Member
  • *****
  • Posts: 1186
    • View Profile
Re: Check Item
« Reply #2 on: December 03, 2014, 08:42:17 am »
Don't forget the item name parameter in game:get_item(). So it would be:
Code: [Select]
game:get_item("bow"):get_variant() == 0or a shorter equivalent:
Code: [Select]
game:get_item("bow"):has_variant()

Yruama

  • Newbie
  • *
  • Posts: 24
    • View Profile
Re: Check Item
« Reply #3 on: December 03, 2014, 02:37:47 pm »
I didn't find where is "get_item()" in the documentation.

so i try like you said :

Code: [Select]
local map = ...

function map:on_started()
if map:get_item("lamp"):get_variant() == 1 then
_13_NPC00:set_enabled(false)
end
end

but it does not work :

Code: [Select]
ror: In on_started: [string "maps/Map13/13.lua"]:4: attempt to call method 'get_variant' (a nil value)

Christopho

  • Administrator
  • Hero Member
  • *****
  • Posts: 1186
    • View Profile
Re: Check Item
« Reply #4 on: December 03, 2014, 08:07:03 pm »
get_item() is a function of game, not map.
See http://www.solarus-games.org/doc/latest/lua_api_game.html#lua_api_game_get_item

And now that I see the doc, there is an even shorter solution:
Code: [Select]
game:has_item("lamp")

Renkineko

  • Jr. Member
  • **
  • Posts: 81
    • View Profile
Re: Check Item
« Reply #5 on: December 04, 2014, 07:30:10 am »
Sorry for the name of the item, I posted the answer yesterday just before going to work aaaaaaaand.... I did not double check my post :) I wanted to indicate also where the get_item was on the doc, but good point for the "has_item" : I knew there was a shortcut, but didn't remember the location (and I didn't search a lot yesterday :p).

Yruama

  • Newbie
  • *
  • Posts: 24
    • View Profile
Re: Check Item
« Reply #6 on: December 04, 2014, 06:51:40 pm »
I try this :

Code: [Select]
local map = ...

function map:on_started()
if map:get_game():has_item("lamp") == true then
_13_NPC00:set_enabled(false)
end
end

But i never see my NPC like if i always have the lamp :(

EDIT : I'm just a retard who put a fail line in the code ....
« Last Edit: December 09, 2014, 09:05:18 pm by Yruama »