Explosive barrel - chain explosion help

Godot Version

Godot engine v4.3 stable official

Question

I wanted to make explosive barrel in my 2d shooter game but i have a problem implementing chain reaction. At first i tried to just detect like i detect bullet if body(on_body_entered) has class ,Wybuch" but it detects also explosion of itself and it just triggers itself(probably), because on start of playtest i can see explosion sprite and it deals damage, but does not delete itself after few seconds and i dont know why.
Tree:
image
Code:
extends CharacterBody2D
@onready var wybuch = $“…/…/Enemy1/Base/Turret/Wybuch”
#wybuch simply stands for explosion in polish(my native language) i have half english half polish project
@onready var hurt: Area2D = $“…/…/Enemy1/Base/Turret/Wybuch/Hurt”
@onready var sprite_2d = $Sprite2D
@onready var hitbox = $Hitbox

func _ready():
wybuch.visible = false
hurt.monitoring = false

func _on_hitbox_body_entered(body):
if body is Bullet: #If its bullet, delete it.
body.queue_free()
Wybuch()
else:
if body is Wybuch: #Explosion from barrel
if body.visible == true:
Wybuch()
if body is Wybuch2: #Explosion from other source
if body.visible == true:
Wybuch()

func Wybuch(): #triggers explosion
hitbox.monitoring = false
wybuch.visible = true
hurt.monitoring = true
sprite_2d.visible = false
wybuch.visible = false
hurt.monitoring = false
queue_free()

func _on_hurt_body_entered(body): #if body is main character, deal damage and turn area2d off
if body.name == “Bohater”:
Global.HP -= 2
hurt.monitoring = false

Firstly, I would like to encourage you to use the preformatted text option for code
it looks like </> this, and helps with code indentation.
As of now, I assume the code looks like this

extends CharacterBody2D
@onready var wybuch = $“…/…/Enemy1/Base/Turret/Wybuch”
#wybuch simply stands for explosion in polish(my native language) i have half english half polish project
@onready var hurt: Area2D = $“…/…/Enemy1/Base/Turret/Wybuch/Hurt”
@onready var sprite_2d = $Sprite2D
@onready var hitbox = $Hitbox

func _ready():
    wybuch.visible = false
    hurt.monitoring = false

func _on_hitbox_body_entered(body):
    if body is Bullet: #If its bullet, delete it.
        body.queue_free()
        Wybuch()
    else:
        if body is Wybuch: #Explosion from barrel
            if body.visible == true:
                Wybuch()
        if body is Wybuch2: #Explosion from other source
            if body.visible == true:
                Wybuch()

func Wybuch(): #triggers explosion
    hitbox.monitoring = false
    wybuch.visible = true
    hurt.monitoring = true
    sprite_2d.visible = false
    wybuch.visible = false
    hurt.monitoring = false
    queue_free()

func _on_hurt_body_entered(body): #if body is main character, deal damage and turn area2d off
    if body.name == “Bohater”:
        Global.HP -= 2
        hurt.monitoring = false

For the start I would place a breakpoint and see the progression of the Wybuch and which objects are being detonated. Do they reach queue_free()? Is the body truly Wybuch and Wybuch2 and shouldnt you check for body.name == Wybuch instead?

After some time of testing with breakpoints i saw that i had wrong path and instead of changing visiblity and monitoring of explosive barrel it changed other sprites that also has explosion and child nodes with the same names. After changing paths barrel explosion works on contact with bullet, but chain reaction doesn’t work. I made sure that ,Wybuch" has class_name Wybuch but it still doesn’t work. I also placed barrels close so hitboxes would be inside of explosion range. I tried changing if statement to if body.name == “Wybuch”: ,and if body.name == “Hurt”: and i set breakpoint on it but breakpoint didnt trigger.

to be honest, I believe there is a flaw in the logic.
I believe that if one barrel explodes, the explosion itself should trigger other explosions.
Therefore the chain reaction should happen inside of Wybuch()
If the method is called, then the object should check for all other objects that it is currently colliding with.

func Wybuch(): #triggers explosion
    var other_bodies = hurt.get_overlapping_bodies()
    for body in other_bodies:
        if body is Wybuch: #Explosion from barrel
            if body.visible == true:
                Wybuch()
        if body is Wybuch2: #Explosion from other source
            if body.visible == true:
                Wybuch()
        
    hitbox.monitoring = false
    wybuch.visible = true
    hurt.monitoring = true
    sprite_2d.visible = false
    wybuch.visible = false
    hurt.monitoring = false
    queue_free()

_on_hitbox_body_entered() is only executed if a body enters the range of another body, which does not happen when then Barrels don’t move.

1 Like

I tried your code but it didn’t work. I decided to make this explosion a little different and i made explosion as different scene which is preloaded and played when barrel detects bullet. Thanks to this i could make chain reaction. I checked if body is Barrel using class and if it is it detonates: body.wybuch(). But i did it thanks to you spotting where is problem with on_body_entered(): so thank you very much.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.