How to play Death Animation whenever the enemies Health is 0? (In Blueprints or C++ and spawn again with the AI Controller)

I’m sorry to say this but I couldn’t get anything helpful on YouTube or any other websites on Object Wildcard. Could you please send me some references from which I can refer to?

@MostHost_LA @Limanima Instead of Cast To BP_Enemy, I’m using Get All Actors Of Class and now it’s working (not spawning the enemy, but print string is working) but there’s one problem. I get this error:

Blueprint Runtime Error: “Accessed None trying to read property Call_Array_Get_Item”. Blueprint: BP_Spawner Function: Execute Ubergraph BP_Spawner Graph: Event Graph Node: Branch.

And I’ve changed some stuff.

Changed the Actor Spawn What? to Actor Array Spawn What? Time. NOTE: I’ve changed the variable to Actor Array Spawn What? Time because there are 65 Enemies in the Gameplay Level (I tried attaching A Spawn What? variable back, but still it gives the same error.

The Tick Event Condition.

The BP_Enemy has a tag name called “Enemy”. So I did this in the BeginPlay Event.

And when the Spawn Event is activated, the Print String function does not work. So the problem is with the Spawn Enemy Event.

It’s very hard to help because I’ve lost the way where you’re at.
Can you send me the project file again so I can take a look at it?
It’s the last time I’m doing this as I don’t have much time to work on my own project…

You can take your own time, I don’t mean to disturb you guys with your work to solve my problem : )

Download the project files -

I’m taking a look at what you have done.

  1. Why are you spawning enemies in the Tick Event of BP_Spawner?
    No need. Enemies are spawned in “BP_Spwaner->Spawn Enemy”

  2. In BPSpawner->“Aa_Spawn_Spots” has no elements, that’s why enemies don’t spawn

  3. There are a lot of things I don’t understand
    What’s this for?
    image

What is “Aa Spawn Spots” supposed to hold?
Why do you have “?” in your variable names? That’s new…
I’m lost…

And why you are not simple spawning a BP_Enemy?
Here:
image
The class comes from here:
image

What’s is going on here?

By looking at your blueprints I have the feeling you don’t know exactly what you are doing and you are just copying from other examples. You have to know what you are copying. The task at hand is simple:

-Enemy got hit? Is he dead? Yes? Destroy, spawn new enemy at location x, y, z.
While developing and testing this, create a map with a single enemy. While this is not working, just spawn the enemy at a fixed position. When you have this working, then you deal with the spawn locations.

Alright, I’m spawning enemies.

The crash you’re having is due to this:

image

Move the line “DamageCollision->OnComponentBeginOverlap” to here:

image

Then, in the BP_Spawner in the “Spawn Enemy” event simply do this:
(note the 300 in Z, or else enemies will spawn under the floor)
image

Do nothing on tick:

image

When you have this working, then you deal with the spawn location.

And where do I call the simple Spawn Enemy Event?

This is the simple Spawn Event (I’ll set the location once it’s working properly)

This is the BeginPlay Event

And I changed the lines you told me to also.

That’s wrong too.

If damagecollision
Then do the operation on it.

Otherwise you crash when it’s null.

So what should I do?

I haven’t changed that.It’s in the BP_Enemy when he dies.
image

I don’t think this is needed. In some situation checking for null is not needed and I think this is one of those situations.
If CreateDefaultSuboject fails then something is very wrong. This shouldn’t happen. This would affect the gameplay, so the game can’t continue anyway. How would the game work without the enemies collision working?
I rather add an assert.
You add a “if (damagecollision)” then what?
This is irrelevant anyway for what OP is trying to achieve and that’s not because of that this isn’t working.

Whenever you instantiate during construction there is a chance other engine overrides call something before it exists (Particularly begin play).
Either don’t call it (Which in this case is a good idea) or test that it’s valid.
Literally just if / then

Then the code that was failing.
The if will prevent null values.
You can also add an else.

Or you can try catch. If you want to waste time or what not…

I know you can do all that and avoid the crash. The question is, gameplay wise, what will happen? Nothing, because the game can’t continue when the actor is not correctly initialized.

Take a look at the ACharacter.cpp for example. A lot of places where null isn’t checked. Creating a mesh actor shouldn’t fail, and if it does, the game can’t continue anyway. That’s why I ratter have asserts then if !null. If !null hides bugs.

Wrong.
The BeginPlay event is fired several times one of these times could be invalid, and the next could be valid.
Gameplay wise, you don’t need to stop the execution of the begin play event to ensure initialization has occurred.
The process can happen on several threads too, so consider that.

If you are doing something else in begin play - which you usually are, since it’s initialization, using a simple if prevents crashes when the object hasn’t actually yet been properly created.
Begin play can happed in parallel - which is another issue.

Also, don’t try and learn from acharacter.
It’s one of the worse cpp in unreal.
Take apart the vehicle stuff or pawn… or really just about anything else…

Note that the line of code in question, moved from “beginplay” to the constructor.

I would argue that you add an If there too before initializing it to prevent crashes in odd situations where the assignment has failed.
It’s not common, but you prevent an hard crash.

@Limanima So the Spawn Event is still not working.

So there is nothing attached to BeginPlay & Tick Event. Only Spawn Event is there

After the BP_Enemy is Destroyed I call the Spawn Event there.

Remember I’ve told you to set z location to 300 or else enemies would spawn bellow ground?

Edit: never mind, you are setting to 300…

Place a breakpoint in “Spawn->SpawnActor BP Enemy” and I see if the execution breaks there.

Breakpoint? What do you mean by that?
Edit: Okay sorry I got what you mean. Should I set the breakpoint on Spawn Event Node, Spawn Actor From Class or both?

Here.


Does the execution stops here?