How to get the reference of a Volume in an array based on Signal event

I have a group of volumes of type volume_device that I set in an array. When the player enters the volume I want to generate a signal that shares the location of the volume but I can’t find a way to get a reference to the volume from my array.

I always get that compile ERROR:

Ambiguous identifier; could be localhost.MyFirstProject.VolumeTest_device.SignaledVolumeEnter.Volume or localhost.MyFirstProject.VolumeTest_device.SignaledVolumeEnter.Volume(3588)

The code:

using { /Fortnite.com/Devices }
using { /Verse.org/Simulation }
using { /UnrealEngine.com/Temporary/Diagnostics }
using { /UnrealEngine.com/Temporary/SpatialMath }

VolumeTest_device := class(creative_device):
    @editable
    VolumesArray :[]volume_device = array{}

    # Runs when the device is started in a running game
    OnBegin<override>()<suspends>:void=
        for (index := 0..VolumesArray.Length-1):
            if (Volume := VolumesArray[index]):
                Volume.AgentEntersEvent.Subscribe(SignaledVolumeEnter)
                
    SignaledVolumeEnter(Volume:agent) : void =
        if (Volume := volume_device?):
            Location:vector3 = Volume.GetTransform().Translation
            Print("This is the location of the Volume: {Location}")

Is it the right way to do that ? Any suggestions?

Hey, I think this should work for you:

# Create a class which holds references to required values defined when subscribing
volume_test_device_zone_enter_handler := class():

    Volume: volume_device

    On(Agent: agent): void =

        Location: vector3 = Volume.GetTransform().Translation
        Print("This is the location of the Volume: {Location}")


volume_test_device := class(creative_device):
    @editable
    VolumesArray :[]volume_device = array{}

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

        # This is a Verse for loop sytax
        for. I -> Volume : VolumesArray
        do. Volume.AgentEntersEvent.Subscribe(volume_test_device_zone_enter_handler{Volume:=Volume}.On)

Once you understand how that’s working, you may want to look at Wrapping Subscribe() to pass additional data to listeners | Uefn Code Snippet. That handles this in a more generic way but will probably be confusing to get straight into if you are new to coding or Verse.

1 Like

Thank you so much, very simple and elegant solution!

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