How to know the position of an Area2D node?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By marco_vito

So, I’m really new to programming and Godot, and this might be a simple question. But I’m having some trouble in finding the position of an Area2D and using in code to determine the position of a new instanciated scene.

The goal is to have an enemy drop a projectile. The enemy Scene is made with an Area2D node. The projectile is also an Area2D node.

The code for the Enemy is bellow:

extends Area2D

signal is_hit
export (PackedScene) var Bullet

func _on_Area2D_body_shape_entered(body_id, body, body_shape, area_shape):
	emit_signal("is_hit")
	$AnimatedSprite.animation = "hit"
	$DeathSound.play()
	$DeathTimer.start()
	
func _on_DeathTimer_timeout():
	get_parent().queue_free()

func _on_ShootingTimer_timeout():
	var bullet = Bullet.instance()
	add_child(bullet)
	$ShootingSound.play()
	bullet.position = self.position

The code for the projectile is bellow:

extends Area2D

export var speed = 300

func _process(delta):
		var motion = Vector2(0,1) * speed
		position += motion * delta

func _on_Area2D_body_shape_entered(body_id, body, body_shape, area_shape):
	get_parent().queue_free()

func _on_VisibilityNotifier2D_screen_exited():
	get_parent().queue_free()

And here is a YouTube link with the problematic behaviour:

The projetile does not show up where it should, which, I guess, is caused by my own lack of knowlodge about how to get the position of an Area2D.
And there’s also the fact that the enemy vanishes after a while, which, I guess, it’s because the projectile is activating a “body entered” signal for the enemy, which them activates the timer that makes the enemy disapear.

Any help is apreciated.

:bust_in_silhouette: Reply From: Zylann

When you create your projectile, you added it as child of the enemy. That means the projectile’s position will be relative to that enemy, and if the enemy moves, the projectile will also move.
To fix that I would recommend you add the projectile as child of the level, or whatever is parent of the enemy. For example, by doing get_parent().add_child(bullet).

Also, I see you do get_parent().queue_free() in your projectile script. Is that script on the root of the projectile scene? If it is, that means the projectile will destroy its parent. Is that wanted?

Thank you for your reply. First of all, it was not intended for the bullet to destroy its parent. I’ve searched the comand to get rid of a node, and got “get_parent().queue_free()” as an asnwer. After you explained it to me, it’s kind obvious I was wrong and should only use “queue_free()”. Sorry for the rookie mistake.

As for the enemy Scene, there are no parents. The ideia is to build a Breakout game with a couple of extra elements. So, I was trying to make an enemy capable of shooting a ball. So, my process was to make the ball as a Scene and use a Timer to make the enemy instanciate a Ball from time to time; since it’s a breakout game, the enemy won’t move, which means there’s no issue with the ball using the relative position of the enemy.

My only issue now is that the ball appears in the middle of the screen, and not right bellow the enemy. And I don’t know how to make the ball inicial position be that of the enemy.

marco_vito | 2019-06-25 06:10

:bust_in_silhouette: Reply From: Ingeniou5

I don’t know if this is related to your question, but to get Area2D’s position there is get_node(Area2D).position
And for specific axis:

get_node(Area2D).position.x
get_node(Area2D).position.y