:on_interaction_item(item_used)

Started by ponderitus, September 06, 2019, 05:35:02 PM

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

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

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

The type of item_used is item, not string, that's why Starlock's example does not work.

September 06, 2019, 06:27:05 PM #4 Last Edit: September 06, 2019, 06:50:03 PM by ponderitus
I don't know what that means...so how would you get that to work then?

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.

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....

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 ...
```

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 :)