0
\$\begingroup\$

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 = "";
                            dt.AddMilliseconds( 500 );// add half a second to dt
                            break;

                        case ghostFacingRight:
                            angle = 3.15f;
                            ghostDirection = ghostFacingDown;
                            Program.form.direction = "";
                            dt.AddMilliseconds( 500 );
                            break;

                        case ghostFacingDown:
                            angle = -1.6f;
                            ghostDirection = ghostFacingLeft;
                            Program.form.direction = "";
                            dt.AddMilliseconds( 500 );
                            break;

                        case ghostFacingLeft:
                            angle = 0.0f;
                            ghostDirection = ghostFacingUp;
                            Program.form.direction = "";
                            dt.AddMilliseconds( 500 );
                            break;
                    }
                }
            }
        }
\$\endgroup\$
2
  • \$\begingroup\$ 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. \$\endgroup\$
    – Hackworth
    Commented Jun 5, 2012 at 14:24
  • \$\begingroup\$ @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. \$\endgroup\$
    – SomeGuy
    Commented Jun 5, 2012 at 21:55

2 Answers 2

6
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

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
}
\$\endgroup\$
3
  • 5
    \$\begingroup\$ Using loops inside the update isn't bad for performance. It's only an issue if they take too long, like anything else. \$\endgroup\$ Commented Jun 5, 2012 at 15:14
  • \$\begingroup\$ 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. \$\endgroup\$
    – SomeGuy
    Commented Jun 5, 2012 at 21:54
  • \$\begingroup\$ 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... \$\endgroup\$
    – Navy Seal
    Commented Jun 6, 2012 at 11:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .