hi i'm getting this error message and i couldn't find how to solve it.

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

the identifier “direction” ins´t declared in the current scope. he says the error is in the move_and_slide(direction.normalized()* speed)

extends KinematicBody

onready var nav = $“…/Navigation” as Navigation
onready var player = $“…/player” as KinematicBody

var path = [0]
var current_node = 0
var speed = 2

func _ready():
pass
func _physics_process(delta):
update_path(player.global_transform.origin)
if current_node < path.size():
var direction : Vector3 = path[current_node] - global_transform.origin
if direction.length() < 1:
current_node += 1
else:
move_and_slide(direction.normalized()* speed)

func update_path(target_position):
path = nav.get_simple_path(global_transform.origin, target_position)

:bust_in_silhouette: Reply From: Major_Monkey_0720

You are getting an error because the variable “direction”, is being declared inside an if statement:

if currentnode < path.size():
    var direction : Vector3 = path[currentnode] - globaltransform.origin

because it’s declared in an if statement, the variable can’t reach the rest of the function. You should declare it either at the top of the function, or at the top of the script. You should change the script to

onready var nav = $"../Navigation" as Navigation
onready var player = $"../player" as KinematicBody

var path = [0]
var current_node = 0
var speed = 2

func ready():
pass

func _physicsprocess(delta):
var direction = Vector3()

updatepath(player.globaltransform.origin)
if currentnode < path.size():
direction : Vector3 = path[currentnode] - globaltransform.origin
if direction.length() < 1:
currentnode += 1
else:
moveandslide(direction.normalized()* speed)

func updatepath(targetposition):
path = nav.getsimplepath(globaltransform.origin, targetposition)