Author Topic: :on_interaction_item(item_used)  (Read 1087 times)

ponderitus

  • Jr. Member
  • **
  • Posts: 66
    • View Profile
:on_interaction_item(item_used)
« 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.

Code: [Select]
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?

Starlock

  • Full Member
  • ***
  • Posts: 106
    • View Profile
Re: :on_interaction_item(item_used)
« Reply #1 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.

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

ponderitus

  • Jr. Member
  • **
  • Posts: 66
    • View Profile
Re: :on_interaction_item(item_used)
« Reply #2 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

Code: [Select]
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

Christopho

  • Administrator
  • Hero Member
  • *****
  • Posts: 1177
    • View Profile
Re: :on_interaction_item(item_used)
« Reply #3 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.

ponderitus

  • Jr. Member
  • **
  • Posts: 66
    • View Profile
Re: :on_interaction_item(item_used)
« Reply #4 on: September 06, 2019, 06:27:05 pm »
I don't know what that means...so how would you get that to work then?
« Last Edit: September 06, 2019, 06:50:03 pm by ponderitus »

Starlock

  • Full Member
  • ***
  • Posts: 106
    • View Profile
Re: :on_interaction_item(item_used)
« Reply #5 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.

ponderitus

  • Jr. Member
  • **
  • Posts: 66
    • View Profile
Re: :on_interaction_item(item_used)
« Reply #6 on: September 06, 2019, 08:09:47 pm »
so...

Code: [Select]
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....

Max

  • Full Member
  • ***
  • Posts: 223
    • View Profile
Re: :on_interaction_item(item_used)
« Reply #7 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 ...
```

ponderitus

  • Jr. Member
  • **
  • Posts: 66
    • View Profile
Re: :on_interaction_item(item_used)
« Reply #8 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 :)