0 votes

How can i get access to my highest skript in my game because i want to get a variable from there.

asked Aug 2 in Projects by FabianSchick123 (14 points)

1 Answer

0 votes

1) One way to do that is by loading the script in question into another script. From there you can access the variable. Here's and example:

In the script named Foo.gd we have the variable named example, which has a value of 3

var example = 3

In another script named Bar.gd we access Foo's example variable by using the following code:

onready var Foo = load("res://Scripts/Foo.gd") #path to script

func _ready():
    var foo = Foo.new().example
    print(foo) # this will print out 3

2) Another way is by using so-called singletons, which are basically global variables and functions you can access from anywhere in your game:
https://docs.godotengine.org/en/3.1/getting_started/step_by_step/singletons_autoload.html#doc-singletons-autoload

The main idea of this method is that you create a script that contains which autoloads at the beginning of your game and which you can access at any moment by calling get_node("/root/<resource_name>")

answered Aug 2 by johnygames (614 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.