not detecting player spawned event in first round

im creatin a super power box pvp map and im trying to make it so that once the game starts it gets all the spawners and subscribes to an onplayerspawned event, once it detects a player it chooses his class and gives him his UI. it works well generally but when i start my uefn session it never works in the first round, im wondering if its because the player spawns before all spawners get initialized. how do i fix this?

Game_Manager := class(creative_device):

    @editable
    NumOfClasses : int = 16
var CustomPlayers : [player]Custom_Player = map{}

    OnBegin<override>()<suspends>:void=
        GetPlayspace().PlayerRemovedEvent().Subscribe(OnPlayerLeftServer)
        InitSpawners()
    
    InitSpawners():void=
        Print("initializing spawners")
        Spawners := GetCreativeObjectsWithTag(spawner{})
        for(Obj : Spawners):
            if(Spawner := player_spawner_device[Obj]):
                Spawner.SpawnedEvent.Subscribe(OnPlayerSpawned) 

    OnPlayerSpawned(Agent: agent):void = 
        Print("player spawned")
        if(Player := player[Agent]):
            InitPlayer(Player)
    
    InitPlayer(Player : player):void= 
        Rand := GetRandomInt(1, NumOfClasses)
        if: 
            Agent := agent[Player]
            Fort := Agent.GetFortCharacter[]
        then:
            if(Existing := CustomPlayers[Player]){}
            else:
                Fort.EliminatedEvent().Subscribe(OnPlayerEliminated)

                CustomPlayer := Custom_Player{MyAgentObj := Agent}
                if(set CustomPlayers[Player] = CustomPlayer){}
                CustomPlayer.InitWidget()
                CustomPlayer.SetClass(Rand)
                OnClassSelected(Player, CustomPlayer.ReturnClass())
                Print("class is: {CustomPlayer.ReturnClass()}")
2 Likes

I have the same issue. Did you ever resolve this?

Did you find out what is causing this? I encounter similar issues.

1 Like

I also see this issue sometimes in first round of UEFN session.

This way work for me :

// Put this on your OnBegin method

AllPlayers := GetPlayspace().GetPlayers()
for (Player : AllPlayers):
InitPlayer(Player) // Your Method

2 Likes

Thanks, that seems to work. Still trying to understand why the SpawnedEvent is not called. Have the impression that is timing related.

Are you trying to test from the UEFN launch session? It doesn’t work. I have the same problem and many times I have to test but actually uploading a private map and testing through Fortnite itself. I’m not sure if that’s be design or what but definitely makes testing a lot more difficult.

Add this to your OnBegin, it will ensure any agents who loaded in before devices initialized are handled properly.
And On that note: Make sure to validate your players every time before you do any player handling.

This is an ongoing and poorly/unaddressed issue across the whole platform. Epic will pass you Invalid Players through spawn and server join events and every once in a while you will initialize the player faster than they become active and it can break the game state for the player and in certain cases the game.

By running this validation loop you make sure the player passes that IsActive check before you do anything with them, and while its only the difference of fractions of a second it will save your game state and your code.

Spread the word, good luck.


  OnBegin<override>()<suspends>:void=
     InitSpawners()
     GetPlayspace().PlayerRemovedEvent().Subscribe(OnPlayerLeftServer)
     for(Player:GetPlayspace().GetPlayers()):
         spawn{ValidatePlayer(Player)}
         
  OnPlayerSpawned(Agent: agent):void = 
     Print("player spawned")
     if(Player := player[Agent]):
         spawn{ValidatePlayer(Player)}
  
  ValidatePlayer(Player:player)<suspends>:void=
      race:
         block:
              loop:
                  if(Player.IsActive[]):
                     InitPlayer(Player)
                     break
                  else:
                     Sleep(0.33)
         block:
             Sleep(90.0)
             Print("Warning: Player Init Timeout")
1 Like

Hi, I’ve always used SpawnedEvents and PlayerRemovedEvents to manage players and have never had issues with invalid players.

However, after reading your recent posts, I will be adding a ValidatePlayer function in OnPlayerSpawned.

But checking if Player.IsValid every time I use a player seems unnecessary because they should always be valid until they leave.

Am I right, or did I miss something?

I think you got it, it’s just necessary on game join and maybe on respawns but I can’t confirm that. Putting it just on player spawns or server join would be the only necessary action here so it sounds like you are understanding it all correctly and in either case this should clarify :+1:

(You should also use it when you submit persistence data updates, but that’s the only other case)

1 Like