I need help improving my enemy AI code (Top Down Shooter)

Apologies for the late response, I’m about to graduate and I’ve been trying to catch up on several assignments. I tried adding separate objects zombie zombie and it works, kind of. They press right up against each other front to back and sometimes I’ll kill one and the other will delete with it like the merging glitch, but it only happens occasionally, and they’re still separated, so it works for me!

@Sciller4
I combined your last two ideas, and made a brand new system. It still kind of lags, but the game still seems to run without too many glitches or noticeable frame drops. I decided to add flags in the rooms of the house, that when the zombies spawn, they’re sent to a random flag.
bait
I just made about four of these and put them in the corner rooms. Then when the scene starts, I just hide them. (It’s the same map, but this game is also being made for class credits, so I need to get it completed soon, so I’m just continuously developing the game and then going back to fix broken and janky code later.)


This code takes care of all the spawning and how the enemies track you down. For the sake of quickness and keeping it lightweight at the moment, I made it so once the zombie spawns, it just sends it to a random flag. I used to have some code so that when it hit one, it would then select another flag and then run towards a new room continuously until it hits the player, but it didn’t work, and just ran in and sat at the bait, so I took that part out. Instead, I have the game measure the distance from them to the player, and if you get close enough, they begin to chase you, so you can sometimes look over and see one or two waiting in a room, but once you get close, they chase you, balancing difficulty a bit if you’re cornered in a room and more are spawning in, and most of the time it just seems like they walk in and then begin to attack immediately so it works well for an alpha, I’ll continue posting my progress with it when I add more.

@krunkster
I ran debug mode for an average play session of my game, this is what I got back


Events and Objects seem to be the ones that take up the most processing power here. Render does too, but it doesn’t have any sub categories, so I’m not exactly sure what that is, my guess is it’s just how much it takes to render the graphics on screen.

In events, AI and Collision take up the most power, as expected. Other than that, everything else seems fine, I could probably skim down the code for all the weapons though.

Here’s objects. Pathfinding takes up some power, but the pathfinding obstacles seem to take up more. The platformer object is the player. It’s top down, but I also made it a player character and then turned off the controls for it for easy collision for the player and the walls. I’m not sure why health takes up so much either. It might be because it’s referenced for every character on screen in collisions.

1 Like