How do I send a variable to a different script?

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

I’ve been trying to figure this out for hours and the only information I can find is in these Q&As. I can’t really find anything in the docs either and its driving me insane.

I’m quite new to programing and all I was trying to do is mess around and get a feel for the engine. I’m trying to send my KinematicBody2Ds velocity to a RichTextLabel so that I can check its velocity in-game instead of through the output console. My problem seems like something so simple and vital to creating games yet everything I’ve tried hasn’t worked.

I’m not sure what to do right now, there has to be a simpler way of doing things instead of using get_node which I can’t find any useful information about outside of those forum posts. Every time I try using get_node a number of errors appear and every time I change something a different error appears. The most plausable explanation I can come up with is that I’m doing something wrong on the KinematicBody script so I’ll post it here.

extends KinematicBody2D
var x = 0
var y = 0
var velocity = Vector2()

func _ready():
    pass 

func _process(delta):

velocity = Vector2(x, y)

if Input.is_action_pressed("ui_up"):
	if y > -100:
		y = y - 10
elif velocity.y < 0:
	y = y / 1.1
	
if Input.is_action_pressed("ui_down"):
	y = y + 5
elif velocity.y > 0:
	y = y / 1.1
		
if Input.is_action_pressed("ui_right"):
	x = x + 5
elif velocity.x > 0:
	x = x / 1.1	
		
if Input.is_action_pressed("ui_left"):
	x = x - 5
elif velocity.x < 0:
	x = x / 1.1
	
if y != 0:
	if y < 1:
		if y > -1:
			y = 0
			print(y)
		
print(velocity)

velocity = move_and_slide(velocity)

return y #i thought this might do something, probably not though
pass

I finally found my problem after hours of trying, and it is something Godot should fix. Whenever I copied a node path I assumed it was the proper node path, as it would just make sense that when you copy a node path that node path should be the proper node path and it should just be a copy and paste into anything that needs it. I was wrong as I needed to add “…/” before what I copied for it to work. I don’t know if there’s a specific reason that Godot doesn’t give you the proper node path but if there really isn’t it seems like quite a major issue for beginners.

Logan McDaniel | 2020-07-03 20:19

:bust_in_silhouette: Reply From: ThomasScripter

Good evening!

How do you used the method get_node() ? In the script of the RichTextLabel you should save the reference which you got with get_node() in a onready var. Furthermore instead of using the method get_node() you could use the $-operator in the script of the RichTextLabel. onready var = $“KinematicBody2D”

I hope I could helped you!

:bust_in_silhouette: Reply From: frankiezafe

Indeed, to access your RichTextLabel, you have load it in a variable by using its path.

You have two options to do so:

  • RichTextLabel is a child of KinematicBody2D object, then you can use the notion: $child0/child1/my_rich_text_label
  • RichTextLabel is not a child, then you have to access it via get_node; in this case, you have to go up the tree, find the parent of your RichTextLabel and specify the full path to it.

Let’s say you have a scene structure like this:

root
	character
		KinematicBody2D
	ui
		layout
			column
				RichTextLabel

To get RichTextLabel from KinematicBody2D, you will have to do:

var rtl : RichTextLabel = get_node("../../ui/layout/column/RichTextLabel")

You have to up to the root node by using …/…/
Once there, you get the deepest parent of RichTextLabel and go down the tree until you reach RichTextLabel.

It’s a standard way to navigate in a tree in computers, …/ meaning go up one level in the tree.

To change the text of RichTextLabel, you will have to turn your variables into text. For instance:

rtl.text = "velocity: " + str(velocity)

I hope this will help you :slight_smile:

Well, you can use $ to go up in the tree:

$"../../foo"

hilfazer | 2020-07-06 08:07

Never tested this notation, thanks :slight_smile:

frankiezafe | 2020-07-06 09:59

:bust_in_silhouette: Reply From: overlord666

use a autoload scrip set the variable there the pase it to where you want it