# Loop in Tick Called Function

**URL:** <https://forums.unrealengine.com/t/loop-in-tick-called-function/340036>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine\
**Created:** [November 29, 2015, 6:48pm UTC](https://forums.unrealengine.com/t/loop-in-tick-called-function/340036 "2015-11-29T18:48:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_e8414475](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_e8414475](https://forums.unrealengine.com/u/anonymous_user_e8414475)\
**Post date:** [November 29, 2015, 6:48pm UTC](https://forums.unrealengine.com/t/loop-in-tick-called-function/340036/1 "2015-11-29T18:48:59Z")

</div>

I’m running the following code:

```
void AFEWeapon_Projectile::FireWeapon()
{
	const FVector StartTrace = GetMuzzleLocation();
	const FVector ShootDir = GetAdjustedAim();

	SpawnProjectile(StartTrace, ShootDir, true, FColor::Red); //Set debugging manually if desired
}

void AFEWeapon_Projectile::SpawnProjectile(FVector Origin, FVector Direction, bool debug, FColor debugcolor)
{
	ActiveProjectiles.Add(FProjectile(Origin, Direction, ProjectileVelocity, debug, debugcolor));
}

void AFEWeapon_Projectile::UpdateProjectiles()
{
	int i = 0;
	for (FProjectile fp : ActiveProjectiles)
	{
		fp.Update(GetWorld());
		i++;
		//Process hit results after the projectile has been updated
	}
	UE_LOG(LogTemp, Warning, TEXT("Loop iterations: %d"), i);
}

FHitResult AFEWeapon_Projectile::WeaponTrace()
{
	return FHitResult();
}

void AFEWeapon_Projectile::ProcessHit()
{

}

void AFEWeapon_Projectile::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
	UpdateProjectiles();
}

```

This is supposed to update the `TArray` ActiveProjectiles. However the logging from `UpdateProjectiles()` looks like this:

> LogTemp:Warning: Loop iterations: 0  
> LogTemp:Warning: Loop iterations: 1  
> LogTemp:Warning: Loop iterations: 0  
> LogTemp:Warning: Loop iterations: 1  
> LogTemp:Warning: Loop iterations: 0  
> LogTemp:Warning: Loop iterations: 1

Why is this happening? It also seems to be causing the calls to `Update()` for my USTRUCTS to not work properly. What causes the loop to run inconsistently? Is tick a bad method to use here?

---

<div class="post-metadata">

**Author:** ![Begounet](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Begounet](https://forums.unrealengine.com/u/Begounet)\
**Post date:** [November 29, 2015, 8:20pm UTC](https://forums.unrealengine.com/t/loop-in-tick-called-function/340036/2 "2015-11-29T20:20:33Z")

</div>

Is it possible that you have 2 instances of AFEWeapon\_Projectile in the game ? Thus, when you FireWeapon, only one of them really spawn an object. If you call FireWeapon a second time, do you get :

> LogTemp:Warning: Loop iterations: 0  
> LogTemp:Warning: Loop iterations: 2

Try adding more infos like :  
UE\_LOG(LogTemp, Warning, TEXT(“[%s] Loop iterations: %d”), \*GetName(), i);

PS : You should use a reference in your for loop to avoid copies (in general it is for optimization but here, it is more to be sure that is not a copy issue) :

```
for (FProjectile& fp : ActiveProjectiles)
{
   ...
}

```

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_e8414475](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_e8414475](https://forums.unrealengine.com/u/anonymous_user_e8414475)\
**Post date:** [November 29, 2015, 8:31pm UTC](https://forums.unrealengine.com/t/loop-in-tick-called-function/340036/3 "2015-11-29T20:31:03Z")

</div>

That did it… it was the reference issue. I’m a python programmer by trade, I’m used to everything being references automatically. Thanks for all the help!
