Verse Library verse

03 Standalone

Automatically collects interactable objects by disabling nested lights and meshes when players interact.

verse-library/verse-scene-graph-build-a-system/03-standalone.verse

using { /Verse.org }
using { /Verse.org/Native }
using { /Verse.org/SceneGraph }
using { /Verse.org/Simulation }

# Bolt this onto a pickup's parent entity. It collects the pickup
# (hides its mesh and lights) when a player finishes interacting.
collectible_pickup_component := class<final_super>(component):

    # Runs once when the component starts running in the game.
    OnBeginSimulation<override>():void =
        # Be polite: run the base class setup first.
        (super:)OnBeginSimulation()
        # Find every interactable below us and listen for success.
        Interactables := Entity.FindDescendantComponents(interactable_component)
        for (Interactable : Interactables):
            Interactable.SucceededEvent.Subscribe(OnCollected)

    # Runs when a player successfully interacts. The collector is
    # handed to us as an agent (the player who grabbed it).
    OnCollected(Collector:agent):void =
        # Switch off every light nested under the pickup.
        for:
            Light : Entity.FindDescendantComponents(light_component)
        do:
            Light.Disable()
        # Hide the pickup's shape by disabling its mesh.
        if (Mesh := Entity.GetComponent[mesh_component]):
            Mesh.Disable()

Comments

    Sign in to vote, comment, or suggest an edit. Sign in