Solarus-Games English Forum

Solarus => Development => Topic started by: ponderitus on September 06, 2019, 05:35:02 PM

Title: :on_interaction_item(item_used)
Post by: ponderitus on September 06, 2019, 05:35:02 PM
I was wondering if you can make this item specific? it seems to activate if any item is used..eg.

function custom_sensor_1:on_interaction_item(boomerang)
sol.audio.play_sound("secret")
end

function npc_1:on_interaction_item(boomerang)
sol.audio.play_sound("secret")
end


this would be activated if i used a bow or any item at all.

is there a different way to have something happen when an certain item is used in a specific place? maybe im looking in the wrong place?
Title: Re: :on_interaction_item(item_used)
Post by: Starlock on September 06, 2019, 05:44:21 PM
I haven't tested this so not sure if it'll work with what you're trying to do but maybe this could work.

function npc_1:on_interaction_item(item_used)
  if item_used == "boomerang" then
    sol.audio.play_sound("secret")
  end
end
Title: Re: :on_interaction_item(item_used)
Post by: ponderitus on September 06, 2019, 06:01:01 PM
nope that doesn't work i already tried that :( thanks though

nothing happens at all with the boomerang or anything in that situation and if you put an else in...like

function custom_sensor_1:on_interaction_item()
  if item_used == "boomerang" then
    hero:freeze()
    sol.audio.play_sound("secret")
  else
    sol.audio.play_sound("wrong")
  end
end


the "else" will always be activated...even when using a boomerang
Title: Re: :on_interaction_item(item_used)
Post by: Christopho on September 06, 2019, 06:08:50 PM
The type of item_used is item, not string, that's why Starlock's example does not work.
Title: Re: :on_interaction_item(item_used)
Post by: ponderitus on September 06, 2019, 06:27:05 PM
I don't know what that means...so how would you get that to work then?
Title: Re: :on_interaction_item(item_used)
Post by: Starlock on September 06, 2019, 08:02:27 PM
If the only problem with the code is that it is calling a string, all you would need to do is take away the quotation marks.
Title: Re: :on_interaction_item(item_used)
Post by: ponderitus on September 06, 2019, 08:09:47 PM
so...

function npc_1:on_interaction_item(item_used)
  if item_used == boomerang then
    sol.audio.play_sound("secret")
  end
end


i just tried that and that doesn't work either....
Title: Re: :on_interaction_item(item_used)
Post by: Max on September 07, 2019, 02:17:22 AM
I think what you're missing is fundamentally misunderstand what an "item" is in this context.

If I say x = 1
Then x is a number

If x = "boomerang"
Then x is a string

If x = y
Then x is whatever the variable y is.

Right now you're saying
`if item_used == x then`
But you haven't defined the variable x. You have to tell the code that by the variable x, you mean the equipment item named boomerang, so you need to use a game method to get that equipment item.

```
local x = game:get_item("boomerang")
if item_used == x then ...
```
Title: Re: :on_interaction_item(item_used)
Post by: ponderitus on September 09, 2019, 11:37:15 AM
Cheers bro I totally get what you're saying.

I did try a variation of this but i never got the "game:get_item("boomerang")" part of it.

what you wrote makes perfect sense thought and I've used that so many times on other things, nice explanation for anyone else that gets stuck on something like this.

and...yes those last 2 lines are indeed what i needed. thanks :)