Iterate through all player controllers from within the GameMode

So im trying make custom tick and iterate through all player controllers from within the GameMode. I did the following, but the engine crashes when trying to start the “for” cycle
BeginPlay:

GetWorld()->GetTimerManager().SetTimer(CustomTickTimerHandle, this, &AIgoraGameMode::CustomTick, TickFrequency, true);

CustomTick:

for(FConstPlayerControllerIterator Iterator = GetWorld()->GetPlayerControllerIterator(); Iterator; ++Iterator)

Ok, it was not the iterator itself, but function call inside it.

	for(FConstPlayerControllerIterator Iterator = GetWorld()->GetPlayerControllerIterator(); Iterator; ++Iterator)
	{
		AIgoraPlayerControllerBase* Controller = Cast<AIgoraPlayerControllerBase>(Iterator->Get());
		if(Controller != nullptr)
		{
			OnPlayerEffectsActions->HandleOnPlayerEffects(Controller);
		}
	}

So function call causes crash even though it is an empty function. When i call it outside of CustomTick (even in default engine’s Tick) it works fine, so it has something to do with timer. Any ideas?

Looks like you’re checking the wrong thing. What’s OnPlayerEffectsActions? Might be a timing issue being too soon if that’s the case and you could use the next argument for an initial delay.

Also TActorRange simplifies your code.

for (AIgoraPlayerControllerBase* Controller : TActorRange<AIgoraPlayerControllerBase>(GetWorld())
{
    //code
}
1 Like

Privacy & Terms