Godot Version
4.2.2
Question
Hi all, I am trying to use a function in a packedScene I’ve instantiated, But I keep getting the error the function ‘set_direction’ in base node2D doesn’t exist, any ideas?
Heres the code for the thing I’m trying to get working
var bullet = bullet_scene.instantiate()
bullet.position = global_position + bullet_offset
get_tree().root.add_child(bullet)
print("Bullet instantiated:", bullet)
bullet.set_direction(target.global_position, bullet_speed)
and this is the function in the bullet.tscn I have preloaded
func set_directions(target_position: Vector2, bullet_speed: float):
direction = (target_position - global_position).normalized()
speed = bullet_speed
rotation = direction.angle()
mrcdk
May 24, 2024, 5:00am
2
There’s a typo.
bullet.set_direction(target.global_position, bullet_speed)
→ direction
func set_directions(target_position: Vector2, bullet_speed: float):
→ directions
I have changed that and I am still getting the error, also tried to preload the bullet.tscn which didn’t seem to fix it and I saved the changes too
If your bullet scene has a script with a custom class name like:
class_name Bullet extends Node2D
you need to apply a cast in this line:
bullet = bullet_scene.instantiate() as Bullet
im having this exactsame problem. I’ve made this change you suggest, but now im gettind base: ‘Nil’
this is how i defined my custom class:
extends RigidBody2D
class_name Bala
##(rest of the code)
and here im instantiating it:
var inst_bala = bala.instantiate() as Bala
do you happen to have any suggestion?