Jump to content
tipforeveryone

[CPP] Time calculation drops FPS

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

Share this post


Link to post

If this is a single player game, I would make a master player pointer and do the check from the npc themselves with a simple if statement. 

 

Share this post


Link to post
49 minutes ago, reepblue said:

If this is a single player game, I would make a master player pointer and do the check from the npc themselves with a simple if statement. 

 

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

 

Share this post


Link to post

There is nothing I see there that would cause any slowdown in C++ or even in Lua, with 20 enemies.

Share this post


Link to post
9 minutes ago, Josh said:

There is nothing I see there that would cause any slowdown in C++ or even in Lua, with 20 enemies.

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 ?

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post

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

Share this post


Link to post
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.

Share this post


Link to post

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.

Share this post


Link to post
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.

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

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.

×
×
  • Create New...