Godot Version
4.2.2 stable Windows 11
Question
I have this script extension hierarchy:
CharacterBody2D > enemy.gd > ai_fighter.gd > yellowslime.gd
In enemy.gd, I declare this function:
func CreateShotA1(bullet_type : String, colour : int, pos : Vector2, speed : float, accel : float, target_speed : float, angle : float, angular_velocity : float, lifespan : float, dmg : float):
var objShot = load("res://Enemy/Bullets/" + bullet_type + ".tscn").instantiate()
objShot.colour = colour
objShot.global_position = pos
objShot.speed = speed
objShot.accel = accel
objShot.target_speed = target_speed
objShot.angle = angle
objShot.angular_velocity = angular_velocity
objShot.lifespan = lifespan
objShot.damage = dmg
return objShot
print("shot created!")
And in yellowslime.gd, I have this code to call the function on death:
if hp <= 0 :
CreateShotA1("ball_M", 0, global_position, 60, 0, 60, 90, 0, 3, 5)
###death code is after this
My ball_M scene uses the universal bullet script:
func _ready():
$Sprite2D.set_frame(colour)
if not lifespan == null:
await get_tree().create_timer(lifespan).timeout
queue_free()
func _physics_process(delta):
speed = move_toward(speed, target_speed, accel)
angle += angular_velocity
velocity = Vector2.from_angle(angle) * speed
move_and_slide()
In theory, this code should spawn a ball_M bullet on yellowslime’s death, at the same position as yellowslime, moving straight downwards at a constant speed, that doesn’t shift direction, and lasts for 3 seconds. However, nothing happens, and the output console doesn’t even print “shot created!”, indicating that either the CreateShotA1 function isn’t being called at all, or it is being called but the code is not running.
What am I doing wrong? Any help is appreciated.