How to connect signals to an instantiated node

4

i was having trouble with a shooter when i instantiate a bullet how can i detect when it hits something. at first I thought signals but i cant connect them to objects not made yet at least to my knowledge how can i fix this?

var bullet_node = load(scene_path).instantiate()
some_node_with_signal.connect("signal_name", bullet_node.some_func)
# or
some_node_with_signal.signal_name.connect(bullet_node.some_func)

...


thanks but im having trouble implementing it and I get and error could you tell me more psecifically what to do in my code extends CharacterBody2D

const bulletpath = preload(“res://scenes/bullet.tscn”)
var run_speed = 1250
var jump_speed = -1500
var gravity = 3000
var accel = 90
var velociti = Vector2(5,0)
var speed = 300
var gun = Gun.get_parent()
var canshoot = true

func get_input():
acceleration()
velocity.x = 0
var right = Input.is_action_pressed(‘right’)
var left = Input.is_action_pressed(‘left’)
var jump = Input.is_action_just_pressed(‘up’)

if is_on_floor() and jump:
	velocity.y = jump_speed
if right:
	velocity.x += run_speed
if left:
	velocity.x -= run_speed

func acceleration():
if velocity.x >= 100:
run_speed += accel
else:
run_speed = 1500

if velocity.x <= -100:
	run_speed -= accel
else:
	run_speed = -1500

run_speed = clamp(run_speed, -2000, 2000)

func _physics_process(delta):
Gun.wheretoshoot = get_global_mouse_position()
if Input.is_action_pressed(“shoot”):
if canshoot:
shoot()
$Timer.start()
canshoot = false
velocity.y += gravity * delta
get_input()
move_and_slide()

func shoot():
canshoot = false
var bullet = bulletpath.instantiate()

get_parent().add_child(bullet)
bullet.position = Gun.wheretoshoot

Connect the bullet to the area

var bullet_instance = bulletpath.instance() # Create an instance of the packed scene
bullet_instance.connect(“area_entered”, self, “_on_bullet_area_entered”)

func _on_timer_timeout():
canshoot = true

func _on_bullet_area_entered(area):
if area is Area2D:
print(“dead”)

So what does the error say? Upon looking at your script it look fine to me. Maybe you could give us more information on what you want to accomplish and what is the setup?

1 Like

unexpected identifier in body and im trying to instantiate a bullet then move it then when it collides destroy it and what it colldies with

Change to

bullet_instance.connect(“area_entered”, self._on_bullet_area_entered)

Signal connection parameters has changed between Godot 3 and 4

1 Like

My response should work if the bullet is an area2d node, if not you, got another problem to solve.