Jump to content
  • Advertisement
Sign in to follow this  
Garra

Some AI advice needed

This topic is 2483 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

So I have a AI class that allows me to initialize a certain number of enemies and that's the number of enemies I have in the game. However, it doesn't allow me to do more interesting techniques with my AI such as spawning two new enemies when one enemy dies or just spawning an enemy on the fly. Basically I want to change the class to give me the ability to create new enemies at any time in the game. My current class has a initialization function that initialized the initial positions of the enemies, sets up the shader, and the enemy object itself. Most of this could be moved outside of the class, except there will be a problem with setting the enemies initial position. I thought of some other ways to do it without modifying my class since creating and destroying objects can be expensive. One of these ways is to make a pool of enemies. I could create a set number of enemies, put them in two vectors or something based on rather or not they are used and then swap the objects around. It seems like this wouldn't work in some cases since you would eventually run out of enemy objects. Does anyone have some advice to gvie me on a good method to set up creating new enemies at any time while the game is running?

Share this post


Link to post
Share on other sites
Advertisement
Just give the AI class a container of enemies. It could be contained inside the object itself, or you could hold a reference/smart pointer to a container owned by, say, the "Game logic" code, or whatever. Then you can arbitrarily expand, contract, and manipulate the container of foes to your heart's content.

Share this post


Link to post
Share on other sites
Whats the difference in adding a container inside of the class and just adding a container of enemy objects in say the main game file, for instance main.cpp. Maybe I'm misunderstanding, but it seems I would also have to due away with the initialization function of the enemy class.

Share this post


Link to post
Share on other sites
The difference is in accessibility. If you need to access the list of enemies from places other than just your AI code - say, for rendering purposes - then you should put the container someplace that is accessible to those areas of code. Generally, put it in the most localized, constrained location possible; this helps keep your code clean.

I don't see why you would need to remove enemy initialization to make this work.

Share this post


Link to post
Share on other sites
Thanks for explaining. The reason I mentioned moving the initialization code was because I'm calling the initialization function of the enemy class from my games Initialization function which of course gets called once. When I want to randomly add another enemy I would have to call that enemies initialize function, just not constantly like it would in the game loop.

Share this post


Link to post
Share on other sites
Even if you have your code for creating an enemy called in the game loop, nothing is forcing you to initialize it more than once, let alone every frame; for example:

CreateEnemies(container)

while(GameIsPlaying())
{
while(NeedToSpawn())
{
AddNewEnemy(container) // Creates the enemy, calls Initialize, puts it in container
}
ProcessAI(container)
ProcessPlayerInput()
Render()
}

Share this post


Link to post
Share on other sites
Thanks I managed to get that part working, however I would like some advice on optimization techniques to use, since after adding 2-3 enemies the FPS is dropping dramatically. There's a few things I'm currently doing to cause the issue, such as using the distance formula to calculate the distance between all the enemies and the player ever frame. All the enemies also draw no matter if they are in the view area or not. I'm still making optimizations such as adding bounding volumes and calculated collisions instead of distance. I thought about adding only frustum culling but that wouldn't solve the issue since my AI is designed zergling-like and you could have many enemies in your view at a time. I also thought about adding in some type of grid or tree, do you have any advice on what to add to solve my optimization issues?

Share this post


Link to post
Share on other sites
Could it be the rendering? Are you using glBegin/End or VBOs?

You could make the distance calculation to only run every x frame to see if thats the problem.

I guess im supposed to say "Profile, profile, PRRROOOFFIIILLLEEe1!!!!!!1!!1111" here like everyone else all the time :P

Share this post


Link to post
Share on other sites
Yep, I'd suggest running a profiler to see where your time is going. That will point you at optimizations that are actually worth investing in, versus stuff that'll make you 0.001% faster - still faster, technically, but you'd never notice ;-)

Share this post


Link to post
Share on other sites
umm could you explain a little on profiling, I did a little research and it looks like Visual Studio Ultimate and Premium have a in-built profiler, but I'm using Professional edition ;o.

I think the profiling idea is the best option as well, even with using the sqrt function calculating distance it seems kinda extreme that spawning a single enemy jumps the FPS from 400+ to 60 and at times goes as low as 30.

Edit: This is being done with DirectX10, not OpenGL. I currently have it set up so I press a keyboard key that tells that a new enemy should be made. I initialize that enemy. The init function creates the actual object which right now is just some vertices that make up a square. Then calls D3DX10CreateMesh to make a mesh with the vertex and index info and sets initial enemy position. Then I have a basic update function that controls the AI and tells it when to do the different AI tasks such as wandering and chasing. Then the render function just sets the enemies transforms and sends the mesh off to the color shader class for rendering.

Editx2: It isn't the distance formula, I can comment out all of the AI movements and just have the box rendering and the FPS drops. Maybe it's because I'm creating new vertices and a new mesh for every enemy in the Initialize function?

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!