UEFN VERSE - Add props to an array to trigger a dispose() and remove them when the player is closed to it.


using { /Fortnite.com/Devices }
using { /Verse.org/Native }
using { /Fortnite.com/Characters }
using { /Verse.org/Random }
using { /Verse.org/Simulation }
using { /UnrealEngine.com/Temporary/Diagnostics }
using { /UnrealEngine.com/Temporary/SpatialMath }
using { /Fortnite.com/Playspaces }

CollectibleSystem :=  class(creative_device):

    @editable
 creatureSpawner : creature_spawner_device = creature_spawner_device{}


    # Reference to a creative_prop_asset array
    @editable
    var CoinsProp:[]creative_prop_asset := array{}

   # A map to store references to the spawned props.
    var SpawnedCoinsProps:[]creative_prop = array{}

    OnBegin<override>()<suspends>:void=

        creatureSpawner.EliminatedEvent.Subscribe(OnCreatureEliminated)

        Playspace : fort_playspace = GetPlayspace()
        AllPlayers := Playspace.GetPlayers()

          Print("About to LOOP " )

        if (FirstPlayer := AllPlayers[0]):
             if (FortniteCharacter :fort_character = FirstPlayer.GetFortCharacter[]):
                  
                 loop:

                   Sleep(1.0)
                      Print("ENTER THE LOOP " )

                   PlayerPosition := FortniteCharacter.GetTransform().Translation
              
                   for ( Prop : SpawnedCoinsProps):
                        Proppos:=Prop.GetTransform().Translation
                        thedistance:=Distance(PlayerPosition,Proppos)
                        Print("Your distance prop is: " + ToString(thedistance))
                        if(thedistance <= 150.0):   
                          Prop.Hide()  
                          Prop.Dispose()
                      
   OnCreatureEliminated(DeviceAIInteractionResult: device_ai_interaction_result):void=
        if (TargetAgent := DeviceAIInteractionResult.Target?, TargetFortCharacter:=TargetAgent.GetFortCharacter[]):
     
            spawnPos := TargetFortCharacter.GetTransform().Translation

            if:
                PropToSpawn := CoinsProp[0] # Use the first prop in the array for now
                SpawnedProp := SpawnProp(PropToSpawn, spawnPos, IdentityRotation())
  

             if (SourceAgent := DeviceAIInteractionResult.Source?, FortCharacter:=SourceAgent.GetFortCharacter[]):
               var PlayerLocation : vector3 = FortCharacter.GetTransform().Translation

                distance:=Distance(spawnPos,PlayerLocation)
       

I’m trying to get an array of props spawned when Ennemies are elimated ( SpawnedCoinsProps)
to check in a loop if Player is closed enough to one of them to dispose the actual prop ( remove from the scene to add +1 coins in a gauge)

My problem :

I ‘m not succeeding in filling this array when the props are spawned.
I tryed different syntaxes and method but I’m struggling to find how to fix this little puzzle.

I’ll see if I can find someone to help.

Hi @BobbySixKillah

I ‘m not succeeding in filling this array when the props are spawned.

Looking at your script here, I see that you’re not adding to your SpawnedCoinProps array or ensuring that you grab the prop safely.

In OnCreatureEliminated, after you grab the first prop and spawn it. You’ll also want to ensure it’s spawned and unwrap the returned creative_prop

            # Properly handle SpawnProp return tuple
        if (PropToSpawn := CoinsProp[0]):
            SpawnResult := SpawnProp(PropToSpawn, SpawnPos, IdentityRotation())
            if (SpawnedProp := SpawnResult(0)?):
                if (SpawnResult(1) = spawn_prop_result.Ok):

From there you can add it to the array:

	            # Properly handle SpawnProp return tuple
            if (PropToSpawn := CoinsProp[0]):
                SpawnResult := SpawnProp(PropToSpawn, SpawnPos, IdentityRotation())
                if (SpawnedProp := SpawnResult(0)?):
                    if (SpawnResult(1) = spawn_prop_result.Ok):
                        # Add spawned prop to array
                        set SpawnedCoinsProps += array{SpawnedProp}

Let me know if this helps your problem

Yes it works now,
I wasn’t clear on how to question this prop added and the syntax to add the prop object to the array in the “if (SpawnResult(1) = spawn_prop_result.Ok)”

Thanks for your help!

1 Like