How to spawn ONLY ONE collectible in the game

I have placed 6 collectibles in my map but I only want one of them to spawn randomly. I don’t want chances or anything, only that ONE will spawn for sure.

I was thinking that I could create an array with all the collectibles, create a random number from 1 to 6, get the collectible from the array that has that index and spawn it. However, I can’t seem to find documentation about the collectible device in the verse reference API. Where can I find it? Is this the best way to do it?

1 Like

That’s a logical way to do it for sure. You can’t stop the spawn from the beginning, so I would just hide them. Here are the options you have:

collectible_object_device := class(creative_device_base):
# Signaled when the collectible item is collected.
# Sends the agent that collected the item.
CollectedEvent:listenable(agent) = external {}

    # Makes the collectible visible.
    Show<public>():void = external {}

    # Makes the collectible invisible.
    Hide<public>():void = external {}

    # Immediately respawns the object for the instigating agent.
    # This will be affected by the option *Consume If Collected By*.
    Respawn<public>(Agent:agent):void = external {}

    # Immediately respawns the object for all agents.
    RespawnForAll<public>():void = external {}

    # Teleports the `device` to the specified `Position` and `Rotation`.
    TeleportTo<public>(Position:vector3, Rotation:rotation)<transacts><decides>:void = external {}

    # Teleports the `device` to the specified location defined by `Transform`, also applies rotation and scale accordingly.
    TeleportTo<public>(Transform:transform)<transacts><decides>:void = external {}

Thank you for answer! However, I am getting an error when trying to get a random element from the array. My code:

@editable
ListaViales : []collectible_object_device = array{}

OnBegin<override>()<suspends>:void=
    RandomIndex : int = GetRandomInt(1, ListaViales.Length - 1)
    ListaViales[RandomIndex].Show()

In that code, “RandomIndex” in the last line is underlined and the error is the following:

This invocation calls a function that has the ‘decides’ effect, which is not allowed by its context.

How can I solve it?

wrap it in an if statement

Done, thank you!

if(Vial := ListaViales[RandomIndex]):
    Vial.Show()
1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.