Hello, thanks for your answer, I'll try to explain myself better :)
So Your navigation node has player as a child, but You want to delete
its Player and use another one, but get variables from the first one ?
What I'm trying to do is instancing the player by code in the navigation2d scene but not by adding it manually in the scene tree. I instanced the player using this code in the navigation 2d script:
extends Navigation2D
onready var player = preload ("res://Player.tscn").instance()
func _unhandled_input(event):
if event is InputEventMouseButton:
if event.button_index == BUTTON_LEFT and event.pressed:
var path = get_simple_path(player.position, event.position,false)
$Line2D.points = path
player.path = path
And after instanced by code when i write the code i can get the player.position and the player.path variable assigend in the player script:
Player script:
extends KinematicBody2D
var speed = 50
var path : = PoolVector2Array()
func _process(delta):
# Calculate the movement distance for this frame
var distance_to_walk = speed * delta
# Move the player along the path until he has run out of movement or the path ends.
while distance_to_walk > 0 and path.size() > 0:
var distance_to_next_point = position.distance_to(path[0])
if distance_to_walk <= distance_to_next_point:
# The player does not have enough movement left to get to the next point.
position += position.direction_to(path[0]) * distance_to_walk
else:
# The player get to the next point
position = path[0]
path.remove(0)
# Update the distance to walk
distance_to_walk -= distance_to_next_point
But I don't know why it doesn't work well. So in order to work well I need to manually instanced the player in the navigation 2d scene and in the script just take the instanced player.
extends Navigation2D
onready var player = $Player
func _unhandled_input(event):
if event is InputEventMouseButton:
if event.button_index == BUTTON_LEFT and event.pressed:
var path = get_simple_path(player.position, event.position,false)
$Line2D.points = path
player.path = path
So my question is :what is the diference between manually instancing the player in the navigation scene and then in the navigation2d scene using this code to get the player variable and position:
onready var player = $Player
Than instancing the player by script and using this piece of code to get the player variable and position:
onready var player = preload ("res://Player.tscn").instance()
I don't get why it works in one way and not in the other one.
project:
https://drive.google.com/file/d/1xMxuR-ALA9bGH6hoh6iUWRVC1GudgX8h/view?usp=sharing