Need to get variable from other instanced scene?

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

In my game i have an enemy that starts at the top and moves downward. My plan was for it to shoot out a raycast and hit an invisible collision shape behind the player that is part of the scene, and if it hits the wall instead of the player, compare the x value of the collision to the position of the player (the difference between the height of the invisible wall and the player collision is small so it wouldn’t make much of a difference) and rotate the ship by a little bit to the left or right depending on whether the player is to the left or right of that collision (or if it is hitting the player, go towards the player). This way, the ship would have a little bit of angular velocity so the player has a chance to dodge it since the player can only move left and right. The only problem is that i don’t know how to get the player’s position. I tried i signal but i can’t figure out how to make it work. Does anyone know the best way to do this? I don’t want to do tree navigation, so anyone have any other idea that will work?

I want the code to look about like this:

func _process(delta):
	
	if $Raycast.get_collider().get_name() == 'Back':
		if $Raycast.get_collision_point().x < PlayerPosition.x:
			set_rotation_degree(get_rotation_degree() + rotatespeed)
		else:
			set_rotation_degree(get_rotation_degree() - rotatespeed)

Side question but also important, it is telling me that that set_rotation_degree isn’t how you set the rotation degree. What is the set function for rotation?

edit: i figured out the rotate() function, but how do i make the ship move in the direction it is facing?

:bust_in_silhouette: Reply From: SIsilicon

The method I used in my own game is to assign the node of the player to a variable so that I always have access to it and its properties.
onready var player = get_node('/root').find_node(str)
Where str is the name of the player node.

Well if that’s the only way to do it i guess that’s fine. Thank you! Nice to know about the find_node function at least. I’d just rather use something like signals to do it since that means i am not dependent on the tree so i can test it in different environments. But it works, so whatever.

lincolnpepper | 2018-04-12 02:26

Signals are sort of dependent on the tree, since you need both nodes at some point in order to wire up a signal :slight_smile:

Another way to do this is to set a global variable with the player or add the player to a “player” group that only ever has one node in it. Those are both sort of sketchy approaches, though, so best not to use that sort of thinking too often.

markopolo | 2018-04-12 05:58