Making a log fall down after chopping a tree

Godot Version

v4.5.stable.fedora [876b29033]

Question

I have a game with a simple combat system, and i want the trees in it to be choppable. I have it all figured out except for how to make the tree (static body 2d) become a fallen log (rigid body 2d) once it dies.

I have tried instantiate(), which gets me an error message that says “Cannot call non-static function “instantiate()” on the class “PackedScene” directly. Make an instance instead.”. I’m not sure what that means. Using instantiate() to summon enemies and vegetation randomly works perfectly in the main scene.

Please paste the entirety of the function code that is responsible for instantiating the scene.

This is the tree’s code:

extends StaticBody2D

@onready var sprite: AnimatedSprite2D = $AnimatedSprite2D
@onready var hurt_timer: Timer = $AnimatedSprite2D/HurtTimer

#@export var fallen_log = PackedScene

@export var max_health: int = 150
var health: int = max_health 

var wave = global.current_wave


func _ready() -> void:
	pass # Replace with function body.

func _physics_process(_delta: float) -> void:
	if global.current_wave > wave:
		wave = global.current_wave
		health = max_health

func hurt(dmg: int):
	sprite.animation = "hurt"
	hurt_timer.start()
	health -= dmg
	if health > max_health:
		health = max_health
	if health <= 0:
		#var death = fallen_log.instantiate()
		#add_child(death)
		pass


func _on_hurt_timer_timeout() -> void:
	sprite.animation = "default"

The commented lines of code are the ones that don’t work.

You mean to set the type to PackedScene with :

@export var fallen_log: PackedScene

Make sure to fill this value in the inspector or else you will get a null error

2 Likes

yeah ok i hate myself

thank you!