Getting all player states: GetPlayerControllerIterator vs. AGameState::PlayerArray

Hey,
Iterating over all player states seems to be something that happens fairly commonly, and until recently I’ve been accessing it using GetPlayerControllerIterator and getting the playerstates from there, but I just found out about AGameState’s PlayerArray.

Is there any benefit to doing either way? Should they always be perfectly in sync?

It might be a very tiny bit faster to use the array instead of the player controller iterator

Is it safe to assume that the array and PlayerController iterator will always be mirrors of each other? Are there any times where it would be unsafe to assume that? I’m wondering if during level transitions or ServerTravels a PlayerController may not be set up appropriately or the player state array might not have all the objects yet.

That is, is it safe to assume that if a PlayerState is in the Array, the same ‘player’ has a PlayerController in the player controller iterator with the same playerstate and vice versa?

I’m assuming the pointers in the Array are the same objects as the pointers in the PlayerControllers, so I’m not as worried about that as I am about the two locations not having the same collection of objects at all times.

Safe to assume they are the exact same objects.If they aren’t, that’s a bug :slight_smile:

Sorry, that was the thing I wasn’t worried about. I didn’t have coffee yet this morning, so it might have sounded like jumbled garbage.

Safe to assume that the array and iterator both have the same players at the same times though?

For now I’m going to try to avoid it during server travel or while joining a server because that seems like the place where a controller might be available to the iterator but the state might not yet be added to the array, but I’m just curious If that’s something I shouldn’t worry about at all.

It shouldn’t be. They are created at the same time

Perfect, cool. :slight_smile:

Not really. PlayerStates do not get destroyed immediatly when a player leaves the game, they stay for the specified amount of time defined in their config. So you may have more player states then player controllers.

1 Like