How do you remove specific element from array?

If there is an array that has 10 elements of creatures, and if one of them exit from the mutator zone, it can be removed from array automatically. But how??? the important thing is, must know what the index number(that the creature has) is.

the process is:

  1. initialize creatures in array when they has spawned.
  2. When they enter in the mutator zone, print the element index number of entered creature.
  3. Remove element when they exit.
    so you don’t know which creature is about to exit. It means I need to use variable for that, But I have no clue FOR REAL… can you guys help me plz?

You can use this method to find the index of the agent (since agent is comparable)

    # Returns the first index whose element in `Input` equals `ElementToFind`.
    # Fails if ElementToFind does not exist in the array.
    (Input:[]t where t:subtype(comparable)).Find<public>(ElementToFind:t)<computes><decides>:int = external {}

Then in your example just replace 0 with the found index :+1:


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

#Finds an element in an array, then removes it.

test_array := class(creative_device):
    
    MyArray:[]int =array{1,2,3,4,5,6,7,8,9,10}

    @editable
    MyButton: button_device = button_device{}

    @editable
    MyValue: int = 10

    OnBegin<override>()<suspends>:void=
        Print("The length of the array is : {MyArray.Length}", ?Duration:=10.0)
        MyButton.InteractedWithEvent.Subscribe(Remover)          
        

            
    Remover(Agent:agent): void =

        if (ThisValue:=MyArray.Find[MyValue]):
            ThisValue
                    
            if(NewArray:=MyArray.RemoveElement[ThisValue]):
                Print("The length of the array is = {NewArray.Length}", ?Duration:=10.0)
                Print("The current values in the array are: " )
                for(i: NewArray):
                    Print("{i}")
    

Just an example to support the answer given.

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