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?
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
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?
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
My response should work if the bullet is an area2d node, if not you, got another problem to solve.
Iâm making a game with a projectile that collides with multiple different types of nodes and my workaround was to make an area 2d the child of a CharacterBody2d node (the project) to get access to itâs signals from within the code. You can then check the body itâs colliding with for a function using a if declaration and if it exists call it.