Jump to content
  • Advertisement
Sign in to follow this  

How to manage object functions efficiently?

This topic is 2533 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts


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

This is my 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.
eventResolver(evList, gevList, evList, 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
i = i;
if(i+1 < evList.size())
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){

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:

i = i;

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
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!