Question concerning AI targetting system in BattleTanks

Hello! So a solid 3 days ago I’ve finished the Battle Tanks segment. And because I’m a person that likes to show off to my mentors at school (who are on the same level as me on game development, my teacher is more like a friend) I wanted to tidy the game up a bit and make it my own by adding a main menu and GameModes with fun objectives. I first started with Team Deathmatch gamemode. I don’t know of a specific way to use the GameMode class so I just went freestyle. I have overridden the Gamemodes’ ‘FindPlayerStart_Implementation’ function to return a spawnpoint (A class based on PlayerStart) based on what team the Playercontroller is. I made it so that the GameMode class is responsible for spawning ALL the AI tanks at the correct spawnpoint by adding a boolean (bInUse) to those spawnpoints and spawning the tanks on all spawpoints for that team that are not bInUse. Then i added respawning. So basically the gamemode has all spawnpoints -> spawns all tanks and aicontrollers and sets a team variable for both of them. -> Stores a list of pointers on each controller of all the tanks that are not in it’s team (aka the enemytanks) -> the spawned Aicontroller possesses the spawned tank and then the controller is supposed to start selecting a target and attacking it. THATS WHERE MY PROBLEM LIES. I don’t know if this is an ok aproach to it but i wanted to keep it simple. Basically what i do is i do a for each enemy tank loop and then get the distance between the tank and the enemy tank and then if the distance is closer then the previous calculated distance, that is the new closest tank. This does it just fine. But like i said i added respawning etc. My game craches after a while and i get the breakpoint telling me GetDistanceTo() failing on a null adress. even though right above it there is an if statement telling it cannot be null. So my guess is when calculating the distance (and it has passed the if not null check) at that very moment a tank dies. and then it tries to get the distance of a nullpointer. I hope this is somewhat clear i really tried expaining it thoroughly.

i’ve tried multiple things:

  • Overriding the functions that get called to try prevent calculating it on a nullpointer [didn’t work]

  • Try/catch statments (This would be the IDEAL solution. But as it turns out Unreal disabled that feature, I tried to activate it but every single solution i’ve tried on the web doesn’t work on the current unreal version).

  • Sorting the Array by distance (No clue why but the sort did not execute at all…)

What i want to ask you all is: What is a solid way to go about this. I’m prepared for refactoring. And how would you handle the AI targeting by range.

I hope for hearing from you all !
Thanks in advance!

Ruben Versavel

Privacy & Terms