tipforeveryone Posted October 29, 2019 Share Posted October 29, 2019 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: 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? Is there any better solution for character reaction ? depend on time like I need Quote Link to comment Share on other sites More sharing options...
reepblue Posted October 29, 2019 Share Posted October 29, 2019 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. Quote Cyclone - Ultra Game System - Component Preprocessor - Tex2TGA - Darkness Awaits Template (Leadwerks) If you like my work, consider supporting me on Patreon! Link to comment Share on other sites More sharing options...
tipforeveryone Posted October 29, 2019 Author Share Posted October 29, 2019 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 and yes, single player game Quote Link to comment Share on other sites More sharing options...
Josh Posted October 29, 2019 Share Posted October 29, 2019 There is nothing I see there that would cause any slowdown in C++ or even in Lua, with 20 enemies. Quote 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 More sharing options...
tipforeveryone Posted October 29, 2019 Author Share Posted October 29, 2019 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 ? Quote Link to comment Share on other sites More sharing options...
Josh Posted October 29, 2019 Share Posted October 29, 2019 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. Quote 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 More sharing options...
Ma-Shell Posted October 29, 2019 Share Posted October 29, 2019 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. Quote Link to comment Share on other sites More sharing options...
Josh Posted October 29, 2019 Share Posted October 29, 2019 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. 1 Quote 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 More sharing options...
reepblue Posted October 30, 2019 Share Posted October 30, 2019 23 hours ago, tipforeveryone said: please give me an example 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. Quote Cyclone - Ultra Game System - Component Preprocessor - Tex2TGA - Darkness Awaits Template (Leadwerks) If you like my work, consider supporting me on Patreon! Link to comment Share on other sites More sharing options...
Rick Posted October 30, 2019 Share Posted October 30, 2019 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. Quote Link to comment Share on other sites More sharing options...
Josh Posted October 30, 2019 Share Posted October 30, 2019 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. Quote 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 More sharing options...
Recommended Posts
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.