Jump to content

[CPP] Time calculation drops FPS


tipforeveryone
 Share

Recommended Posts

Situation: I have 20 enemy characters in a map. 

Goal: Everytime I fire a weapon, some of enemy who are in range from my position can hear and reaction after a small period of time (ex: 2 seconds - 2000ms)

Current solution:

  • Each fire shot calls a function Gun_Sound(), this function will scan all enemy character and if in range, call character's function Sound_Reaction()
for (auto it = enemyList.begin(); it != enemyList.end(); it++){
  if ((*it)->GetDistance(myPosition) < range){
    (*it)->Sound_Reaction();
    (*it)->reactionActive = true;
    (*it)->timeMark = Time::GetCurrent();
  }
}
  • To simulate "reaction" feature of character, I use this popular method
//This function is put in UpdateWorld() of each Character Actor Class
void Character::Sound_Reaction(){
  if (reactionActive){
    if (Time::GetCurrent() - timeMark > reactionDuration){ //reactionDuration can be 2000 for example
      reactionActive = false; //stop and finish reaction process
      //Do some code
    }
  }
}

Problem: iterating through characterList each shot is ok, but each time I fire weapon, that Sound_Reaction() with Time calculation stuff drops my FPS dramaticaly. As my expectation, I don't think it can make FPS worse like this.

Questions:

  1. Is there any better solution to scan enemy in range ? I wonder if I have more enemy (50?), must I iterate through all of them each time I shoot to collect in-range one?
  2. Is there any better solution for character reaction ? depend on time like I need
Link to comment
Share on other sites

43 minutes ago, tipforeveryone said:

Hmm.. I found out the problem is not Time functions stuff, it is Point() and AllignToAxis() which I used to make character model turn to player position. are they expensive ?

Not really.

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

16 hours ago, tipforeveryone said:

Is there any better solution to scan enemy in range ? I wonder if I have more enemy (50?), must I iterate through all of them each time I shoot to collect in-range one?

There is but that isn't trivial to implement: Basically you create a grid where each cell lists all the enemies within the region. This requires that when an enemy changes position, it also removes itself from the old cell's list and inserts itself into the new one. Then the player only has to search through the lists of the nearby cells. Of course the additional bookkeeping might have a different performance impact but especially, if the entities also sometimes react to one another or if you're checking the distance more often, this will certainly pay off.

Link to comment
Share on other sites

Running this code only 20 times should not make a big difference. You should eliminate other things like the bullet actually firing so you can see if that is actually the cause.

  • Like 1

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

23 hours ago, tipforeveryone said:

please give me an example :D and yes, single player game

 

 

To store a pointer globally, you can just do:

// in player.h:
extern Player* myplayer; 

// Player.cpp
Player* myplayer; 

void Player::Attach()
{
	// Code..
	//......
	myplayer = this;
}

 

I thought of this, and I think Josh is right, the loop code shouldn't slow anything down. I use a for loop for bullets and it's fine. If it was my game, I'd do something nifty like have the NPC's look for sound sources and react based on their state/volume/etc. This way physics can also alert the npcs. Just make sure to ignore the check if they are already alert/firing at the player.

I'm also ridiculous and it might be too much/not needed for your project; but there's an idea.

Cyclone - Ultra Game System - Component PreprocessorTex2TGA - Darkness Awaits Template (Leadwerks)

If you like my work, consider supporting me on Patreon!

Link to comment
Share on other sites

4 hours ago, Rick said:

Just a different take on this perhaps use GetEntityInAABB would be a better approach so you don't have to store pointers of other entities within other entities or have access to global lists or anything like that.

For big numbers of entities, yes, but if it's just a list of 20 objects this would likely be slower.

Anyways, we are all talking about optimizing something that does not need to be optimized, because it is not the cause of his problem.

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...