Master Game Loop

Started by
10 comments, last by TerranFury 22 years, 4 months ago
Minor suggestions... from the above code, the ai''s update cycle is linked to the render speed - a bit of a no-no.

You can execute all the ai objects in lockstep:

void UpdateAi()
{
AiNode* node;

for (node = GetAiNodeList(); node != null; node = node->next)
node->thinks(node);
}

void MainLoop(void)
{
while (!done)
{
clock_t ticks = clock();

GetInput();
if (ticks >= nextai)
{
UpdateAi();
nextai += CLOCKS_PER_SEC / aifps;
}
Render(ticks);
}
}

or execute all of the objects in their own times and at their own rates

void UpdateAi(clock_t ticks)
{
AiNode* node;

for (node = ainodelist; node != null; node = node->next)
{
if (node->ticks <= ticks)
node->thinks(node);
}
}

void MainLoop(void)
{
while (!done)
{
clock_t ticks = clock();

GetInput();
UpdateAi(ticks);
Render(ticks);
}
}

where ''thinks'' is a pointer to the function to update the node

Both methods have their pros and cons.

For preference, I have all objects operating in lockstep and use a tree structure to hold the objects - not a list. The tree structure allows parent objects to exert a modicum of control over the children and also can be used to enforce the sequence of object updates (within the parent/child relationship).

Advertisement
You''re right, CryptoHobbes. The AI loop and render loop should really be in seperate threads.

This topic is closed to new replies.

Advertisement