I am having some issues that I can’t figure out. I have an Enemy01 and in Level 1 I place and duplicated the enemy 8 times. I have a gun and projectile system and since the bullets/projectiles were going through the enemy, I had to stop it. I put a destroy actor in the BulletBP to kill the bullet on overlap and now, I get these errors.
Error Blueprint Runtime Error: Attempted to access Enemy_4 via property K2Node_DynamicCast_AsEnemy_1, but Enemy_4 is pending kill from function: ‘ExecuteUbergraph_Bullet01’ from node: DestroyActor in graph: EventGraph in object: Bullet01 with description: Attempted to access Enemy_4 via property K2Node_DynamicCast_AsEnemy_1, but Enemy_4 is pending kill
Not sure what is happening. Here is the BulletBP.
Sorry, made a mistake, I meant BulletBP not EnemyBP
Thanks for any help
As I was looking over the BP, I switched the Destroy node to the False on the branch thinking maybe I am destroying the bullet before it can run it’s course. Nope, same error.
Starting to think duplicating Enemies are no longer allowed. ??
If you destroy the enemy from within the bullet BP and the enemy is reached by many bullets in rapid succession, each bullet will try to destroy the same enemy, hence the error.
The right way to do it is to have the enemy detect if it has been hit by a bullet and destroy itself if that is true.
You can simply have the enemy detect a hit or begin overlap with a bullet, then take it from there.
You are detecting the overlap from the bullet point of view, the problem is that you can have multiple bullets in rapid sequence hit an enemy, which causes the error above.
I am suggesting you to do it the other way around: have each enemy detect if the were hit/overlap by a bullet and take care of destroying themselves if that happens. The code should be in the each enemy’s Blueprint (provided they all have different blueprints).
Would that be with a AnyDamage in the EnemyBP? Cause I have never had those nodes work. Not sure what you mean exactly.
Isn’t that what I have? On Bullet overlap?
I have other enemies with BP’s, but is duplicating on level the same as duplicating the BP itself? Is what it acts like. I don’t know how to see if the enemy was hit by a bullet in the EnemyBP only by overlap on the bullet in the BulletBP. Sorry.
Should I run Health over there too(EnemyBP), or keep health on the bulletBP?
Trying to figure out if I destroy self as in Enemy or Bullet?
If you have one Enemy_BP and you place it in the level multiple times, all enemies will be driven by the same Enemy_BP. To detect a hit by a bullet, use an OnHit Event (or OnBeginOverlap Event) inside the Enemy_BP, cast to Bullet_BP. If the cast is successful, the enemy was hit by a bullet and you can call the hit sound, the emitter spawning and then Destroy self.
Destroy self as an Enemy. Health is a property of the enemy, it should be handled inside the Enemy_BP. Say that Health = 100. Every time the enemy is hit by a bullet you decreased it by 20. Finally, when the enemy is hit again and the Health goes below 0, you call Destroy self in the Enemy_BP to destroy.
Inside the same Enemy_BP you can also destroy the bullet that hit the enemy since it is assumed that the bullet will not continue its trajectory once it has hit something.
Looks pretty good to me. When something hits the enemy, you check whether it is a bullet. If it is, you decrease Health (I assume Health starts from 1 and you decrease it by .02 for each bullet, so you need 50 bullets to kill the enemy). If Health is 0 or less, you spawn the explosion sound and spawn the explosion emitter, then destroy the enemy.
Two things missing: 1) You should also destroy the bullet. 2) Sound and explosion should be spawn at the enemy location (use Get Actor Location).
In this way you can also detect if, for example, the enemy has been hit by a grenade and decrease Health more than for the bullet, say by 0.5, so you need 2 grenades to kill the enemy.
Check your collisions. Both the enemy and the bullet should have collisions enabled and Generate Hit event ticked.If you have troubles with collisions, you can also make this work with Overlaps. You will have to swap the OnHit Event with an On Component Begin Overlap Event for the collision box part of the enemy.
You disconnected the code inside the bullet, did you? Also turn on CCD for both the enemy and the bullet. If the bullets are too fast the engine may miss the hit.
I am sorry, but I can’t for the life of me figure out this logic. As I see, there is no logical set up here. This would never function.
This doesn’t work. The enemy does not receive damage and the bullet still travels through the enemy. Did I miss something?
Well, I got the bullet destroying itself, still no hit on the enemy.
Neither the OnHit or OnOverlap work. They both have generate hit on. the collisions were fine from the start as I could kill them before this edit. Was just trying to stop the bullet from traveling past the enemy.
Also, the error only popped up after killing the 3rd enemy in the stage which was puzzling me.