# Delaying a Foreach loop half a second

I have created a game that has a ghost that mimics the movement of the player after 10 seconds. The movements are stored in a list and i use a foreach loop to go through the commands. The ghost mimics the movements but it does the movements way too fast, in split second from spawn time it catches up to my current movement. How do i slow down the foreach so that it only does a command every half a second? I don't know how else to do it. Please help

this is what i tried : The foreach runs inside the update method

DateTime dt = DateTime.Now;

foreach ( string commandDirection in ghostMovements )
{
int mapX = ( int )( ghostPostition.X / scalingFactor );
int mapY = ( int )( ghostPostition.Y / scalingFactor );

// If the dt is the same as current time
if ( dt == DateTime.Now )
{
if ( commandDirection == "left" )
{
switch ( ghostDirection )
{
case ghostFacingUp:
angle = 1.6f;
ghostDirection = ghostFacingRight;
Program.form.direction = "";
break;

case ghostFacingRight:
angle = 3.15f;
ghostDirection = ghostFacingDown;
Program.form.direction = "";
break;

case ghostFacingDown:
angle = -1.6f;
ghostDirection = ghostFacingLeft;
Program.form.direction = "";
break;

case ghostFacingLeft:
angle = 0.0f;
ghostDirection = ghostFacingUp;
Program.form.direction = "";
break;
}
}
}
}

• Thou shalt not use foreach() in a performace-critical part of software, for it is less efficient than for(). It may not be a big difference, but in a game, every bit counts. – Hackworth Jun 5 '12 at 14:24
• @Hackworth One wouldn't want to use for() in this case either, as one would still be calling the ghost's entire action list in a single update tick. – SomeGuy Jun 5 '12 at 21:55

You have a pretty fundamental misunderstanding of what the update loop does.

You have to perform one "tick" of you operations every time update is called. That is to say, the update method itself is the loop that executes your commands.

Try just executing one iteration of your foreach loop, removing that element from the collection, then just doing that as long as the collection is not empty to start.

If your code is in update you shouldn't be using for each loop like that. Update is already a loop and you should avoid using loops inside the update... its very bad for for the performance.

My suggestion is for you to make a variable like to control the last time a action was made by the ghost.

int a = 2000; // (2 seconds in miliseconds)


then to check if this 2 seconds have been spent since the last time

 if (a > 0 && ghostMimic )
{
// to decrement the time
a -= gameTime.ElapsedGameTime.Milliseconds;
}

if ( a <= 0 )
{
// do next action here

// restart the counter
a = 2000
}

• Using loops inside the update isn't bad for performance. It's only an issue if they take too long, like anything else. – doppelgreener Jun 5 '12 at 15:14
• The issue here is not the game's performance with a loop inside the update; it's that on every update tick, he's calling the entire loop instead of doing it a bit at a time, making the ghost's actions near-instantaneous. – SomeGuy Jun 5 '12 at 21:54
• yes, my bad (english)... what I was trying to point is what jonathan hobbs said. You shouldn't force to use loops unless they are really necessary... – Navy Seal Jun 6 '12 at 11:49