Item Drop

Scripting with the ItemDrop class.

An ItemDrop is a GameObject that represents a dropped Item Stack.

Fields & Properties

Name
Description
Type
Access

id

The item drop ID. This can be used to determine if the item drop has already been obtained.

string

public

itemStack

The item stack that this object represents.

ItemStack

public

useTimeout

If the item drop should be despawned after some time. Despawn is scheduled on start.

bool

public

spriteRenderer

The sprite renderer component that displays the item sprite.

SpriteRenderer

private

Creating an Item Drop

To create an ItemDrop, use the SpawnInstance method. You will need a reference to an ItemStack. This method spawns an instance of an ItemDrop in the game world. You can set the position and whether timeout should be enabled.

ItemDrop myItemDrop = ItemDrop.SpawnInstance(myItemStack, position, useTimeout);

Handling Auto Disable

Item drops can auto-disable themselves on start with the logic of your choice by setting the ItemDrop.shouldEnable field.

ItemDrop.shouldDisable = new(itemDropID =>
{
    if (itemDropID == "MyItemDropID")
    {
        // Item drop should be disabled
        return true;
    }

    // Item drop should be enabled
    return false;
});

Timeout

If timeout was enabled when spawning an item drop instance, the item drop will be automatically destroyed based on the itemDropTimeout field in Settings.

Pick Up

Picking up item drops is handled by the inventory. Once an item drop is picked up, the ItemStack it represents is added to the inventory and the ItemDrop instance is destroyed.

Checking All Drops In Range

You can get the full list of items that the player is in range of with the ItemDrop.dropsInPickupRange field.

Last updated