4.2.1
Just started work on this project, thought this would be the ‘easy’ part, but I’m struggling to get the projectiles the player can shoot to behave how I want. The projectiles themselves work ok, travel is fine, collision is fine.
But, I want the position of the projectile’s point of impact to be sent to back to the player as a variable, and I just cannot get this to work.
Currently I have this on the projectile itself.
extends Area2D
var shot_speed = 500
signal proj_pos
var impact_pos = Vector2(1,1)
func _ready():
pass
func _physics_process(delta):
position += transform.x * shot_speed * delta
func _on_body_entered(body):
if body.is_in_group("objects"):
impact_pos = self.global_position
emit_signal("proj_pos", impact_pos)
queue_free()
And this on the player, to recieve the signal. Neither of the prints work.
func _on_projectile_proj_pos(impact_pos):
impact(impact_pos)
print("does this recieve?")
func impact(impact_pos):
print("does this recieve?")
I just need to have the the position of impact as a variable that the player scene can use, at the moment of that impact, so that I can do the rest of what I want.
I was trying to follow tutorials I found but even with almost 1:1 code it doesn’t quite work. Been at this for hours trying to work it out, so any help would be greatly appreciated.
Do you get any errors? You may need to specify that your signal emits a value.
signal proj_pos(impact_point: Vector3)
And make sure to use the 4.x version of signal/connections
if body.is_in_group("objects"):
impact_pos = self.global_position
proj_pos.emit(impact_pos) # 4.x style
queue_free()
Did you connect the signal from the editor to the player? If you did from the EDITOR, then it won’t work that way.
The editor signal connection only works for nodes that you manually put in the scene tree when the game starts, your projectiles spawning are new instances added dynamically and so will lack a signal connection.
This is what you need to add to your player shoot() method:
projectile.proj_pos.connect(_on_projectile_proj_pos)
If you’re lost, then let me show you what to put in your player script:
# I'm making assumptions of your good might look, so use it as a reference:
@export var projectile_scene: PackedScene # Reference to your projectile scene
func shoot():
var projectile = projectile_scene.instantiate()
# Set position/rotation/etc
projectile.global_position = global_position
projectile.global_rotation = global_rotation
# Connect the signal BEFORE adding to scene tree
projectile.proj_pos.connect(_on_projectile_proj_pos)
# Add to scene tree I assume the main node?
get_parent().add_child(projectile)
Share some of your bullet and shooting player code if you didn’t manage, so we can see how you’re spawning.
2 Likes
Thank you!
That one line was literally all it needed!
2 Likes