Array.RemoveElement not working

I’m trying to make a system that gives a periodic point to players inside a capture area. I know that the device itself has this option, but I need to save the points into verse.

This is my code:

TeamScoreManager := class(creative_device):
    @editable ScoreZone : capture_area_device = capture_area_device{}#Area
    @editable ScorePoint : score_manager_device = score_manager_device{}
    var AgentsInsideArea : []agent = array{}#Agents inside the area

    OnBegin<override>()<suspends>:void=
        ScoreZone.AgentEntersEvent.Subscribe(ScoreEnter)
        ScoreZone.AgentExitsEvent.Subscribe(ScoreExit)
        Sleep(60.0)
        spawn:
            GivePeriodicPoint()
        Sleep(360.0)
    
    ScoreEnter(Agent : agent):void=
        set AgentsInsideArea=AgentsInsideArea+array{Agent}#I create a new array made by the old one + an array made of only one agent which is the one who joins
        Print("Agent Added")
    
    ScoreExit(Agent : agent):void=
        if(IndexToRemove := AgentsInsideArea.Find[Agent]):#I take the index of the agent who left
            Print("IndexToRemove: {IndexToRemove}")
            if(AgentsInsideArea.RemoveElement[IndexToRemove]):#I remove the index found before
                Print("Agent Removed")
        
    GivePeriodicPoint()<suspends>:void=
        loop:
            Sleep(1.0)
            for(Agent : AgentsInsideArea):#For every player in the array, give him points
                if(FC := Agent.GetFortCharacter[].GetHealth()):
                    Print("{FC}")#This is just for some testing
                ScorePoint.Activate(Agent)

I’ve made an array of agents, which are the players inside the area.

By using Print, the code Print “Agent Added” when someone joins the area, and also prints “Agent removed” when somebody leaves, but the player is not removed from the Array. When I leave the area, I still get the point every second.

What am I doing wrong? Is this a bug or I’m missing on something?

Problem found. The RemoveElement actually returns a new array and doesn’t edit the one you pass before the function, so I just wrote this:

if(set AgentsInsideArea = AgentsInsideArea.RemoveElement[IndexToRemove]):
    Print("Agent Removed")
2 Likes

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