Lag in game after using loop

We made a map in UEFN that featured pets following the players. At first it seemed to go really well but after 15 minutes the server starts to have unbearable lag. We are doing a lot of animating in the loop where the pet follows the player. We thought the issue might be, that the verse code causes the lag. Is there any way we can fix this unbearable lag?

 loop:
    Playspace : fort_playspace = GetPlayspace()
    if (GetLooping(PlayerIndex)?):     
        Print("Looping.. + {PetNumber}")
        if (PetNumber = 0):
            AnimateCat(FortniteCharacter, Agent)
        else if (PetNumber = 1): 
            AnimateDog(FortniteCharacter, Agent)
        CharacterPos := FortniteCharacter.GetTransform()           
        PetTransformAdjust1 : vector3 = CharacterPos.Translation - vector3{X := -120.0, Y:= -40.0, Z:=75.0}
        TransformCompare := PetTransformAdjust1
        Sleep(0.1)  
        CharacterPos2 := FortniteCharacter.GetTransform()  
        PetTransformAdjust : vector3 = CharacterPos2.Translation - vector3{X := -120.0, Y:= -40.0, Z:=75.0}
        PetRotation := CharacterPos2.Rotation
        #Print("Player {GetIndexFromPlayer(PlayerInteracted)+1} Looping..")
        if (TransformCompare.X = PetTransformAdjust.X): 
            #Print("position same")    
            if (PetNumber = 0):              
                WalkAnimationOffCat(FortniteCharacter, true)
                RandomIdleAnimationCat(FortniteCharacter)
            else if (PetNumber = 1): 
                WalkAnimationOffDog(FortniteCharacter, true)
                RandomIdleAnimationDog(FortniteCharacter)
        else:                
            #Print("different position")       
            if (PetNumber = 0):
                RandomIdleAnimationRemoveCat(FortniteCharacter)     
                WalkAnimationOffCat(FortniteCharacter, false)
            else if (PetNumber = 1):
                RandomIdleAnimationRemoveDog(FortniteCharacter)
                WalkAnimationOffDog(FortniteCharacter, false)
        if (PetNumber = 0):
            AnimateCat(FortniteCharacter, Agent)
        else if (PetNumber = 1): 
            AnimateDog(FortniteCharacter, Agent)
        if (not TypePet.IsValid[])  {break}  
        if (FortniteCharacter.IsCrouching[]):                          
            CrouchTransformAdjust := PetTransformAdjust - vector3{X := 0.0, Y:= 0.0, Z:=-20.0}
            var Result : move_to_result = TypePet.MoveTo(CrouchTransformAdjust, RotateToPlayer(FortniteCharacter, TypePet), 0.5)                    
            if (Result = move_to_result.DestinationReached):
        else:
            var Result : move_to_result = TypePet.MoveTo(PetTransformAdjust, RotateToPlayer(FortniteCharacter, TypePet), 0.5)                    
            if (Result = move_to_result.DestinationReached):
        if (not TypePet.IsValid[])  {break}
        GingerCatButton.ActivatedEvent.Subscribe(GingerCatButtonTask)
        BlackWhiteButton.ActivatedEvent.Subscribe(BlackWhiteButtonTask)
        WhiteCatButton.ActivatedEvent.Subscribe(WhiteCatButtonTask)
        GingerColorCatButton.ActivatedEvent.Subscribe(GingerColorButtonTask) 
        BlackBrownPuppyButton.ActivatedEvent.Subscribe(BlackBrownButtonTask)
        BlackDotsPuppyButton.ActivatedEvent.Subscribe(BlackDotsPuppyButtonTask)
        WhitePuppyButton.ActivatedEvent.Subscribe(WhitePuppyButtonTask)
        BlackPuppyButton.ActivatedEvent.Subscribe(BlackButtonTask)                        
        DeletePetButton.InteractedWithEvent.Subscribe(DeletePetButtonPressed)
        set AllPlayersBeforePlayerLeft = Playspace.GetPlayers()
        Playspace.PlayerRemovedEvent().Subscribe(PlayerLeft)
        EliminationDevice.EliminatedEvent.Subscribe(RemovePetAfterElim)
    else:
        if (TypePet.TeleportTo[CatHidePlace, IdentityRotation()]):
        break
1 Like

From what I can see, you subscribe to a lot of events every loop iteration. The previous subscriptions aren’t removed so you’re essentially telling the device to do the same function 2x, 3x, 4x,…

To test if this is the case, add a simple Print("Test) to the function you’re executing when one of those events is triggered.

To fix your issue, remove all the Subscribes from the loop and place them in the OnBegin function. If the functions that are called when an event is triggered changes, remove the subscription when necessary and make a new subscription with the new function.

Another thing that might help a bit is using async functionality to make everything a bit more fluid. For example a MoveTo function suspends the loop until it finishes running. If this is intented to be synchronous, then apologies for the suggestion :sweat_smile:
If not, you could use spawn { } or other async options to put some tasks on a separate thread and continue your loop.

Hope this helps!

1 Like

Getting the same issue here. I am using loop in a similar fashion however using sleeps of about 4 minutes. Every x minutes we use TeleportTo on a prop that has a few other props attached to it. We get the unbearable lag after about 30 minutes in a private Island. Have tried turning the Verse off and the lag was non-existant. I guess we’ll have to change our code to not use loop instead.

Can you post some code so I can take a look?

Can’t really right now for this particular project unfortunately. However I managed to fix it using Timer Devices that would trigger whatever would be done in the loop first. So the Timer Devices now function as the Sleep basically.

1 Like