How to check if an item has been picked up

I originally wanted to check if a player had a specific gun in their inventory but I can’t find anything in the API reference to support that sort of query against the player.

I’ve since opted to use the item spawner as the point of reference because it only spawns one specific gun, so I’d like to check the item spawner to see if the item has been picked up and thus I can set my conditions.

I noticed looking at the documentation there is a listenable data member called ItemPickedUpEvent. My questions is, does anyone know how I can check/call/listen to this even in verse code? I’ve tried a few implementations and nothing really works.

Hi @Yanayaya ,

You can learn how to subscribe to events on our Coding Device Interactions page.

Thanks, that’s a bit of a lazy reply. I’ve read this but I’m speaking about the item spawner specifically and trying to understand exactly how I can check for the item being picked up. Subscribing to the event seems fair but I’m trying to implement that and struggling. If you can provide more context and information that would be massively appreciated :slight_smile:

You would subscribe to the ItemPickedUpEvent the same way you saw that OnButtonInteractedWith was subscribed to in the documentation I linked. Create a function to be called when the event is raised, and then pass that function to the Subscribe() function. You can see the implementation the following example.

    @editable
    ItemSpawner:item_spawner_device = item_spawner_device{}

    # Runs when the device is started in a running game
    OnBegin<override>()<suspends>:void=
        ItemSpawner.ItemPickedUpEvent.Subscribe(HandleItemPickedUp)

    HandleItemPickedUp(InPlayer:agent):void=
        Print("An Item was picked up!")
        # Your code here

Now every time an item is picked up, you should see “An item was picked up!” displayed on screen. The InAgent parameter will be passed into the function by the event. I don’t use it in this example, but it’s required. You can use it to reference the player who picked up the item.

Remember to compile your code, drag in your Verse device, and set the ItemSpawner reference to the Item Spawner Device on your island. You can also see how to do this in the documentation I linked previously. Hope this helps.

2 Likes

use conditional_button_device for it.

conditional_button_device.HasAllItems[Agent]
conditional_button_device.IsHoldingItem[Agent]