There are many solutions to this problem. The one I think you might be most interested in is learning to use a timer for functions.
In a game a lot of things need to happen during a couple of frames. If one thing makes the frame stop for it to complete its task, it shuts the whole game down until its finished.
The job is to limit how much a single function can do during any given frame. So you limit it and only do as much as necessary to get to the next frame. So you use a limited loop, store any variables that need to be worked on the next frame and then continue where you left off. This way everything runs smoothly.
Also, with for loops, you can unroll them, so that instead of running through the loop checking if the statement is true, you just run through it a fraction of the time and you do the same action multiple times adding the number of function calls -1 at the end.
- Code: Select all
for(i = 0; i < g_MAXOBJECTSTACK; ++i) //g_MAXOBJECTSTACK = 25
{
g_TreeStack[i] = CreateActor("tree", "tree", "(none)", "(none)", -2000, -2000, true);
++i;
g_TreeStack[i] = CreateActor("tree", "tree", "(none)", "(none)", -2000, -2000, true);
++i;
g_TreeStack[i] = CreateActor("tree", "tree", "(none)", "(none)", -2000, -2000, true);
++i;
g_TreeStack[i] = CreateActor("tree", "tree", "(none)", "(none)", -2000, -2000, true);
++i;
g_TreeStack[i] = CreateActor("tree", "tree", "(none)", "(none)", -2000, -2000, true);
g_TreesOnStack = g_TreesOnStack +5;
}
//or
for(i = 0; i < g_MAXOBJECTSTACK;) //g_MAXOBJECTSTACK = 25
{
g_TreeStack[i] = CreateActor("tree", "tree", "(none)", "(none)", -2000, -2000, true);
g_TreeStack[i] = CreateActor("tree", "tree", "(none)", "(none)", -2000, -2000, true);
g_TreeStack[i] = CreateActor("tree", "tree", "(none)", "(none)", -2000, -2000, true);
g_TreeStack[i] = CreateActor("tree", "tree", "(none)", "(none)", -2000, -2000, true);
g_TreeStack[i] = CreateActor("tree", "tree", "(none)", "(none)", -2000, -2000, true);
i = i +5;
g_TreesOnStack = g_TreesOnStack +5;
}
The reason you would use i + numberofcalls - 1 is because at the end of the loop it add 1 to i.