# Why my editor is crashing from this code?

**URL:** <https://forums.unrealengine.com/t/why-my-editor-is-crashing-from-this-code/453267>\
**Category:** Mobile\
**Tags:** turn-based, unreal-engine, crash, editor, question, CPP, set-timer\
**Created:** [August 2, 2019, 8:53pm UTC](https://forums.unrealengine.com/t/why-my-editor-is-crashing-from-this-code/453267 "2019-08-02T20:53:51Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_ce888f58](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_ce888f58](https://forums.unrealengine.com/u/anonymous_user_ce888f58)\
**Post date:** [August 2, 2019, 8:53pm UTC](https://forums.unrealengine.com/t/why-my-editor-is-crashing-from-this-code/453267/1 "2019-08-02T20:53:51Z")

</div>

Hello guys, I’m trying to do a turn based game and i found a way to change turn between the players, but i have a problem.

If i try to run this code:

```
while (numActors != 0)
{

	if (apelare)
	{
		i++;
		apelare = false;
		Jucator = jucatori[i];

		Controller = Cast<APawnController>(Jucator->GetController());

		if (Controller != NULL)
		{
			Controller->SetTura_Mea(true);
			numActors--;
			GetWorld()->GetTimerManager().SetTimer(TurnTimer, this, &ARISKGameModeBase::MinusCounter, 1.0f, true);
		}

		else
		{
			AIController = Cast<AAIControllerPawn>(Jucator->GetController());

			AIController->SetTura_Mea(true);
			numActors--;
			GetWorld()->GetTimerManager().SetTimer(TurnTimer, this, &ARISKGameModeBase::MinusCounter, 1.0f, true);
		}

	}
}

void ARISKGameModeBase::MinusCounter()
{
	Counter--;

	if (Counter == 0)
	{
		apelare = true;
		Counter = 10;
		GetWorldTimerManager().ClearTimer(TurnTimer);

		AIController = Cast<AAIControllerPawn>(Jucator->GetController());

		if (AIController != nullptr)
		{
			AIController->SetTura_Mea(false);
		}

		else
		{
			Controller = Cast<APawnController>(Jucator->GetController());
			Controller->SetTura_Mea(false);
		}
	}
}

```

I’ll explain what i’m trying to do with this:

While there are still ‘players’ in game

If i didnt call the ‘MinusCounter’ function(apelare is true

I make "apelare "false

I pick a player

I see if his controller is playercontroller or aicontroller

After i find his controller i set a bool on that controller so i know it’s his turn

After that i start a timer that is meant to be the turn timer

At the end of the timer i set the bool on false so i know it’s not his turn anymore and i set apelare to true

I pick the next player

Why is this crashing? Any ideas?

---

<div class="post-metadata">

**Author:** ![MCX292](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@MCX292](https://forums.unrealengine.com/u/MCX292)\
**Post date:** [August 3, 2019, 11:57am UTC](https://forums.unrealengine.com/t/why-my-editor-is-crashing-from-this-code/453267/2 "2019-08-03T11:57:44Z")

</div>

From this code snippet, I do not see anything that sets “i” to zero to start.

I also don’t see any code that prevents “i” from going out of range on jucatori[i] - does it not need to loop back to zero at some point when you have cycled through all of the players?
