how to use vars in static functions

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Daniel Schechtman
:warning: Old Version Published before Godot 3 was released.

So lets say I have this script

#name of the script is example.gd
extends Node

var x = 0
var y = 0

func _ready():
    pass

static func test1(x_val):
    x = x_val
 
static func test2(y_val):
    y = y_val

and lets say I have another script here

extend <insert node type here>
 
var z = preload("example.gd")
 
func _ready():
    z.test(10)
    z.test2(20)

I would get an error that originates in example.gd test and test2 function because x and y are both not defined (even though they are). How can I use variables I initialized earlier in inside the script in static functions?

Update: When I try to preload one script into another, and call a function from the script I preloaded, I get an error saying I can’t call non-static functions. That is why even though static functions aren’t suitable in this case, I tried to use them. If there is a way to use one script from another script, that is more what I am looking for.

Then you have to use Singeltons. Here you can call a Static Class and work with it.

Write a Script “myClass.gd” and then go to your Project Settings and to the Autoload Tab.
Here you add your Script with a Class name for example “Myclass”.

Then you can call it in every other script with

Myclass.setX(123)

NVA_Lee22 | 2017-07-14 11:09

:bust_in_silhouette: Reply From: NVA_Lee22

I haven´t tried that but maybe :

#name of the script is example.gd
extends Node

var x = 0
var y = 0

func _ready():
    pass

static func test1(x_val):
    self.x = x_val

static func test2(y_val):
    self.y = y_val

# Maybe for better access from outside
func getX():
    return self.x

func getY():
    return self.y

func setX(x):
    self.x = x

func setY(y):
    self.y = y
extends Node

var z = preload("example.gd")

func _ready():
    z.setX(10)
    z.setY(20)

If you want to use the example.gd in global scope then maybe you can use godots “singletons” Singletons (Autoload) — Godot Engine (stable) documentation in English

By the way, i´m not sure if you really want to use static functions in your example
Because here is what i found in the Docs

Static functions
A function can be declared static. When a function is static it has no access to the instance member variables or self. This is mainly useful to make libraries of helper

:bust_in_silhouette: Reply From: eve-park

You preload example.gd as a singleton as previously suggested, but you don’t have to. To do what I think you were trying to do, you have it almost right. Instead of:

var z = preload("example.gd")
    func _ready():
        z.test(10)
        z.test2(20)

try:

 func _ready():
    var z = load("res://your_path/example.gd").new()
        z.test(10)
        z.test2(20)

Also, either as a singleton or as a loaded script you actually don’t need to extend or attach example.gd to anything. You can just have an unattached resource floating in a scripts folder somewhere.

:bust_in_silhouette: Reply From: Warlaan

A static function is a global function that is not related to any object. This mechanism is established in other languages, so that’s why GDscript reused it, but I think it would have been a better solution to simply make it possible to declare global functions in a similar way Autoloads are declared, because it would avoid this kind of confusion.

Your test1 and test2 functions could be declared anywhere, the only thing that would change is how to call them. They aren’t related to any object, so the properties of the surrounding class are invisible to them.

People were of course quick to point you in the direction of Autoloads and Singletons, but I would recommend that you think about why you need static variables in the first place. There are cases where it makes sense to use them, but most of the time when beginners are asking about them it’s because they didn’t see how they can make it work without them and why a solution without Singletons might be better.