Hello everyone.
I know that my question is probably very stupid, but I can't figure out what the problem is. (maybe there is no problem. and I just don't have enough knowledge)
The project tree is very simple:
Node2D(named:MainScene)
- TileMap
- - KinematicBody2D
- - - CollisionShape2D
- - - Sprite
- - - - Panel
- - - - - Label
In the KinematicBody2D node, a script with simple control and variable speed.
extends KinematicBody2D
var speed = 70
func _ready():
pass #
func _process(delta):
var velocity = Vector2()
if Input.is_action_pressed("up"):
velocity.y -= speed
if Input.is_action_pressed("down"):
velocity.y += speed
if Input.is_action_pressed("left"):
get_node("Sprite").flip_h = true
velocity.x -= speed
if Input.is_action_pressed("right"):
get_node("Sprite").flip_h = false
velocity.x += speed
if Input.is_key_pressed(KEY_SHIFT):
speed = 120
else:
speed = 70
move_and_slide(velocity)
position.x = clamp(position.x, 5, 335)
position.y = clamp(position.y, 0, 330)
In the Label node, a simple script that should show the value of the speed variable from the script in KinematicBody2D.
And here lies my problem.
If I try to access the KinematicBody2D node to get the value of a variable velocity in $KinematicBody2D.speed (auto-complete, and there have to print the manual), when you run the project I receive an error "Invalid get index 'speed' (on base: 'null instace')"
If you are applying through get_node(), then the autocomplete suggests ".", what's not working.
The only working way I could find:
text = "speed: " + String(get_parent().get_parent().get_node("KinematicBody2D").speed)
It seems to me that I am missing some simple way to indicate the path to another node.
Or is that exactly how it should work?