Godot Version
4.5
Question
I need help changing an enemy scene into an object scene and back into the original scene
I,ve been working on puting together a prototype where the player hits enemies to change them into objects, and is able to hit them back into enemies if needed.I’ve been able to put together a hitbox and hurtbox system that works by working off some tutorials on youtube, however, it seems as though the hitbox hits it infinitly leading to a “Cannot call method ‘instantiate’ on a null value.” error.
class_name Hurtbox
extends Area2D
@export_subgroup(“Transformation”)
@export var target:Node2D
@export var transformation : PackedScene
@export var hurtbox_lifetime : float =0.05
func _process(delta: float) → void:
if hurtbox_lifetime > 0.0:
var new_timer = Timer.new()
add_child(new_timer)
new_timer.timeout.connect(add_child)
new_timer.call_deferred(“start” , hurtbox_lifetime)
func _ready() → void:
monitoring = false
set_collision_layer_value(1,false)
set_collision_layer_value(2,true)
func receive_hit(_target) ->void:
print(“I’m Hit!!”)
var instance = transformation.instantiate()
target.add_sibling(instance)
target.remove_child(target)
class_name Hitbox
extends Area2D
@export_subgroup(“Properties”)
@export var hitbox_lifetime:float
@export var shape: Shape2D
func _init(_hitbox_lifetime:float, _shape:Shape2D ):
hitbox_lifetime=_hitbox_lifetime
shape=_shape
func _ready() → void:
monitorable = false
area_entered.connect(_on_area_entered)
if hitbox_lifetime > 0.0:
var new_timer = Timer.new()
add_child(new_timer)
new_timer.timeout.connect(queue_free)
new_timer.call_deferred("start" , hitbox_lifetime)
if shape:
var collision_shape = CollisionShape2D.new()
collision_shape.shape = shape
add_child(collision_shape)
set_collision_layer_value(1,false)
set_collision_mask_value(1,false)
set_collision_mask_value(2,true)
func _on_area_entered(area: Area2D) → void:
if not area.has_method(“receive_hit”):
return
area.receive_hit(null)
Any help is greatly appreciated.