Noob question - How do i access properties of nodes in other scenes?

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

I am making a pong clone and the ball bounces back when it hits a paddle with area_entered. The bouncing back works finely, but i also want to make it so if the ball is hitting the left side of the paddle it bounces left, and if it is hitting the right side of the paddle it bounces right. This means i have to compare the position of the ball to the position of the paddle. I have tried several methods of trying to access the position property of the paddle, and have been searching the internet for answers for a long time, but i can’t figure it out. So as stupid of a question as it may be, how do i access the position of the paddle? The paddle is a different scene from the ball and i am accessing the position property of the paddle.
Here is the code:

func _on_Ball_area_entered( area ):
if area.get_name() == 'Player Paddle':
	motion.y = yspeed
	if position.x > ?????:
		print("greater than!")
:bust_in_silhouette: Reply From: GarrettGemini

I’m also new to godot and ran into a similar problem with something I’m working on. What I did to solve this was to create a higher level scene that uses an instance of both the paddle and the ball. (right next to the + icon to create a new scene there is a chain icon to add an instance of another scene, or you can do through code.)

then you can access the properties of those instance with the $

var paddle_pos = $paddle.get_position()

an example of adding scenes in code:

    extends Node2D

var paddle = load("res://Scenes/player.tscn")
var balls = load("res://Scenes/ball.tscn")

func _ready():
	var player = paddle.instance()
	var ball = balls.instance()
	add_child( player )
	add_child( ball )
	#NOTE: no $ sign when using variable name
	var player_pos = player.get_position()

LIke I said, I’m new to this too, but that’s something like what I did and worked for me.


Oh yeah! I already have them in instanced scenes in the main scene, but i forgot i could control both of them from a script in the main node. Thank you!

lincolnpepper | 2018-03-29 20:07