Kill AI Number of Times?

Is there a way to do a blueprint that will basically “after destroying 10 AI will do something like quit game” for example?

yea its actually pretty easy to implement. all you need to do is have a variable somewhere that represents the number of enemy lives/spawns, then decrement that number each time a enemy dies, then check the value to see if it equals 0, if it does then run your quit script.

in the below example i created a variable EnemiesCount in the game mode and a custom event to decrease and check its value, then if the value is less than 1 quit the game (picture 1). then in the enemy you just need to get the gamemode, cast to identify the gamemode, then call the function before destroying the enemy actor.simple stuff and this definitely isnt the only way to accomplish this.

thats why what i showed is just an example. if you need a different number for each level then you just need a way to set the number on a per level basis or create the script in another location.theres two easy ways to accomplish this:

first in the level bp on begin play get the game mode then cast as shown above then use a get all of class (enemy) and set the variable enemy count to the last index of the array returned by get all of class.

the second option would be to create a actor that stores the script and gets placed in each level. so instead of having the script in the game mode you would have another actor that takes care of this script. the issue with this method would be that your need to have the enemies reference the actor, this isnt all that hard but is more of an annoyance. a way around this problem would be to use a interface if you know how to use those.

again there are many ways to go about things and this is not a comprehensive list. you could also use tags for example. there is no method i know of to bind a event to every enemy dieing, so you need to create logic and communication to do the task. hell you could get all actors of class on tick and check the array length, but that would run terribly so dont do that.

This wont work at all it feels like it will also be in a Fixed state some levels have more AI than others.

I should had been more clearer basically every level has its own number of AI and what im trying to do is have the level itself count of the number of AIs that get destroyed that are placed in the level and once the number of AIs all get destroyed and the number goes from “10 - 0” it will do basically go to the next level.

Thats what im trying to do this feels too fixed. i dont know how to explain it its like every level has its own AI Check List and once all AIs have been checked dead and the number goes down to 0 the next level will start.

Well sure it looks well and all but its not something i would use also your correct with the gamemode that thing is just a Warning.

Its pretty basic im not the best with these types of things the only thing im looking for is just something that keeps track of enemy AI already in place in the level and when they are gone it just executes whatever next level and as simple as possible im not adding any of these to AI or Player Character im looking for a simple “Are all of these AI here? If So dont load the next level” are they all destroyed and no longer in place in the world? Open the next level".

Also the code has to be in the Level Blueprint.

Just wanted to pop in and help Thompson out really quick because he/she has the right idea. The purpose of making the kill count variable means that it won’t be fixed at all. You will have to implement your own logic to update the amount of deaths until a level is cleared.

The only issue I see here is holding the kill count in the GameMode. Since you’ve described that you want to change levels it would be best to put the kill count in a GameInstance class that way it remains persistent across levels. You could also write a method to increase the kill count based on what level you’re in.

Thompson gave you a very basic idea to get you going, but you’ll have to work out the rest of the details yourself depending on how you want your game to function.

For what you described you would want to run the exact same logic, but instead of casting to GameMode, cast to a GameInstance/SaveGame/CustomActor class, then instead of quitting the game, open the level that you want to go to next.

If you don’t want to travel to a whole new level, you can increase the kill count variable in your game instance, then call the game modes RestartLevel method to reset the state of your level and carry on from there. You should also have a custom actor that handles how many enemies to spawn based on your kill count variable.

Here’s some screenshots to help you visualize what I’m talking about.

The rest of the code that Thompson posted is valid so I won’t duplicate that, but the only difference is I recommend a GameInstance or SaveGame class to keep the persistent kill count working through multiple levels. If your game is more of a one and done kind of thing (see how many levels you can win in one playthrough) or if you’re using the same level every time then the GameMode should work fine. Though beware that resetting the level resets all actors in that level.

I hope this helps!

i used game mode for ease of use here and it was described as more of a once you run out of enemies so i didnt bother with persistence in this case. actually it just occured to me that you could probably leverage the lack of persistence between levels for the game mode in this case. basically if your attempting to get all enemies in the level “count of the number of AIs that get destroyed that are placed in the level and once the number of AIs all get destroyed” then you just need to get the number when the level is loaded. so if your enemies derive from one class or share a tag for instance then you could in the game mode on begin play get all actors of class and set the enemy count based on that(as shown in the picture below). it should work for any level you open.

In that case this would work too:

I prefer tags as Thompson mentioned because it makes your code a little more dynamic. That way if you want to use multiple classes, you just assign them a tag and the logic works the same.

This case assumes that the actor is destroyed when they are dead. So if you’re playing a death animation and leaving them in the level for realism, you’ll want to create a custom event and assign it the same way. You’ll need a reference to your AI so using GetAllActorsOfClass would work better in this case so you don’t have to cast.

Sorry I forgot you’ll need to remove the actor from the array. I think UE4 automagically does this, but I do it to be safe.

why?

first why does it have to be in the level bp? that basically prevents you have having a reusable script. meaning a script that you create once then can use it in as many levels as you like.

the second why. why are you opposed to having script in the characters? its pretty basic an simple to implement.

keeps track of enemy AI already in place in the level and when they are gone it just executes

this isnt something that is possible without doing regular checks. you could do this on a timer but that wouldnt execute right when the last enemy dies, so that leaves you with on tick with can greatly affect performance. when it comes to situations like this you want things to be event driven. you want it so the only time any performance is taken is when the enemy dies.

the most automated way is as ive described, when the level starts run a script which gets all the enemies and outputs a number, when enemy dies call a event that decrements that number. if you dont want to create the script many times for each character then use a function library, a interface, or take advantage of inheritance.

well heres exactly what you asked for but in my opinion it will run like ■■■■ so i wouldnt suggest using it.

It sounds like Ernesto wants to use the Level BP to be able to select all of the actors in the level and drag and drop the references in the script to manipulate them however is needed.

I have to echo what Thompson said here and highly recommend against doing that for every reason Thompson mentioned.

The level script is mainly to help you reference actors that are only going to be used in that level. If your game only has one level then using the level bp is okay, but it’s not the best idea. The level bp is best used for triggers that are unique to that level only and even then there are always better ways around it. I’ve honestly never used the level bp and the only time I’ve heard of it being practical is for cinematics and very unique level triggers (I.E. walking into a box to trigger a boss fight or an in game cinematic.)

Using the level bp to run critical gameplay logic is not recommended.

It sounds like Ernesto wants to use the Level BP to be able to select all of the actors in the level and drag and drop the references in the script to manipulate them

talk about a un-flexible script. lol

id also add that the level bp is notoriously difficult to reference when you want to call specific custom events.

the only time i would imagine to use the level bp is for a one off item that will only exist in that one specific level. even then only when its to do with something placed as part of the scene, im thinking giant gate in a game with no other doors but im unimaginative.

The reason why i dont want to implement the code the way you instructed is simply because i chose convenience i want the code to be used in Blueprints simple and what Innnovations said is true.

I tested Thompsons code and works fine as for innovations didnt understand it well and skipped it but like i said im not that knowledgeable with these types of things but i knew that Arrays was one of theose things that was required.

Also Thompson your code runs perfectly fine my game hits 60fps and i notice little to nothing of slowdowns TRUE Ticks are costly but in this case i notice nothing of slowdowns.

Well with the Event Tick i wasnt okay with it so what i did was just tell the player at the start how many enemies to kill and go to a area of the game that takes them to the next level, “Kill all enemies - Enter Trigger Area - Begin Overlap - Checks all Tags” works just fine and done as simple as possible on Level Blueprints.

But still thanks.

its your game so its your choice. that said the code suggested is actually quite simple. what your running on tick now may not be affecting your game much but its still the wrong way to go about it. well at least you have a backup plan now in case what your doing doesnt pan out. good luck

I’m glad you figured it out, but to avoid having to copy and paste that code into every level you make it’s easiest to create an actor that contains a box collision and then just drop that box into each level wherever you need. It still uses the same code that we mentioned, and is going to be flexible and reusable. I get why you want to use the level BP, but taking the time to learn the API and do it right will save you a lot of time in the future.

I wish you the best and hope your project works out for you!