Sign in to follow this  
KazenoZ

How to manage object functions efficiently?

Recommended Posts

Hello,

I'm having a problem with managing object behavior in a situation where there are alot of object to process.

This is my code:
[code]
for(unsigned int i = FPS_EVENT_INDEX; i < evList.size(); i++){
//If the event's status is active, then its' commands
//will be processed and executed.
if(evList[i].active)
eventResolver(evList, gevList, evList[i], ch, screen, project, pList,
numList, strList, sList, picList, sndList, scale);


//If there are too many events, the game will start
//showing lag, in which case, we will cut out the command
//executing loop for this frame and pick it up from where
//we left off in the next frame.

//If the loop started at a high index this frame, we
//don't want to cause too much synch issues, so we'll
//set the loop over to the beginning
if(FPS_EVENT_INDEX != 0 && i+1 >= evList.size()){
i = 0;
}

//If we use the previous condition to reset the loop
//we'll use this to make sure that we never give the
//same event 2 rounds per frame to save synch.
if(i+1 == FPS_EVENT_INDEX){
i = evList.size();
}

//Regular events get more priority than globals since
//they are more likely to be mass produced.
if(SDL_GetTicks()-fps > 650/FPS){
//This next bit is here for debugging purposes,
//it checks if no events were processed at all
//during the current frame
if(i == FPS_EVENT_INDEX)
i = i;
if(i+1 < evList.size())
FPS_EVENT_INDEX = i+1;
i = evList.size();
}
cout << i << ": " << SDL_GetTicks()-fps << endl;
}
//If the loop ended with time to spare, we'll set it so that
//in the next frame we'll start from index 0.
if(SDL_GetTicks()-fps <= 650/FPS){
FPS_EVENT_INDEX = 0;
}
[/code]

Ok, so a few clarifcations;
-The function eventResolver() takes the current object(3rd parameter), if its' status is active(Which means it's to be executed) and goes through a list of functions that are a member variable in the object class.
-All the other parameters are variables that might be affected by the eventResolver function, such as other objects(evList), pictures(picList), strings that are stored in memory(strList), etc.
-All the list items are actually vectors.
-FPS_EVENT_INDEX is an unsigned int that saves the index in the object list that we had to stop at, so it can pick up from it in the next game loop when the for loop assigns i its' value.
-The game is set to 100 FPS, the conditions state 650/FPS instead of 1000/FPS to allow time for a number of other functions that need to occur before the frame ends.


Now, basicly, what happens is that when the evList grows to around 30 objects on screen at 1 time, when each one has its' own functions to execute, the game will start to lag. This was because the loop in the code was taking more time than was limited per frame by the FPS capping of the game.

What I tried doing in this code was to add a limit of events that are processed every frame so that the game would stop processing them for this frame when it sees that it's going to run out of time for the frame, and pick up from where it left off in the next frame(The running of the game loop).
It's also supposed to take care of when you start from the middle of the list and still have time to process more, then it'll go back to the beginning and go again, and in case it completes a full cycle, it'll end like it would normally.


Now, the problem is that this doesn't work. I'm not sure why.
This part:
[code]
if(i == FPS_EVENT_INDEX)
i = i;
[/code]
will be hit sometime around when the object list gets to 30 objects, this means that it can only process 1 object per frame, and that's really putting it all down very bad.

This is using C++ and SDL.

I'm not really sure if I'm doing this right, or if it's even the right way to handle this. Some remarks would be great, thanks.

Share this post


Link to post
Share on other sites
You shouldn't need this level of jiggery-pokery in this kind of loop at this stage in development.

You also shouldn't need to start messing with multi-threading or multi-CPU work.

Your performance problem is going to be inside the event handling; you need to find out why that is taking so long. Messing about with the top-level loop like this won't gain you processing time.

In addition, your update rate seems rather high -- there's no point in going higher than the refresh rate of the display device. Even a very modern high spec LCD only has a temporal resolution of 120Hz. Beyond that point you're just burning CPU needlessly.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this