Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add static/class variables #5025

Closed
me2beats opened this issue Jul 30, 2022 · 14 comments
Closed

Add static/class variables #5025

me2beats opened this issue Jul 30, 2022 · 14 comments

Comments

@me2beats
Copy link

me2beats commented Jul 30, 2022

Describe the project you are working on

Games, plugins

Describe the problem or limitation you are having in your project

I have some classes with some common data than may be changed during a game, and I don't need to create an object for it.

In Python I can just

class A:
    x = 1

A.x = 2
print(A.x)

that would print 2.

In GDScript I can't do this.

Describe the feature / enhancement and how it helps to overcome the problem or limitation

Adding static/class variables would solve the problem, you don't need to create an object to use these variables.

Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

extends Node
class A:
    static var x = 1

func _ready():
    A.x = 2
    print(A.x)

would print 2

another example:
script1.gd:

static var a = 1

script2.gd:

func _ready():
    var script1= preload("script1.gd")
    script1.a = 2
    print(script1.a)

would print 2

If this enhancement will not be used often, can it be worked around with a few lines of script?

It can't, this is a gdscript feature

Is there a reason why this should be core and not an add-on in the asset library?

It can't, this is a gdscript feature

@Calinou
Copy link
Member

Calinou commented Jul 30, 2022

This was already discussed in godotengine/godot#6840 a while ago and rejected.

@h0lley
Copy link

h0lley commented Jul 30, 2022

consider using ProjectSettings instead.
you can query it in static functions.
and ProjectSettings is probably a less potentially confusing place for globals.

@Calinou
Copy link
Member

Calinou commented Jul 30, 2022

and ProjectSettings is probably a less potentially confusing place for globals.

In fact, the ProjectSettings singleton was even called Globals in the Godot 2.x days 🙂

@me2beats
Copy link
Author

I've read the discussion and it seems the main problem is

if the script is unloaded and reloaded, this variable will be reset
and it will be super confusing

Why will it be super confusing tho?

@KoBeWi
Copy link
Member

KoBeWi commented Jul 30, 2022

const variables are static, so you can use a "constant" Dictionary and modify its contents.

if the script is unloaded and reloaded, this variable will be reset

class_name scripts are never unloaded btw

@me2beats
Copy link
Author

const variables are static, so you can use a "constant" Dictionary and modify its contents.

Does it work for Godot 4 as well?

@h0lley
Copy link

h0lley commented Jul 30, 2022

Does it work for Godot 4 as well?

was just about to say; no in 4 const Dictionary will be fully constant

@dalexeev
Copy link
Member

godotengine/godot#6840 (comment):

I talked to @reduz and he said static variables aren't available to encourage the use of singletons. He proposed to add a system similar to the autoload that doesn't require the script to be a Node (so they wouldn't need to be on the tree). That is my preference as well.

@just-like-that
Copy link

const variables are static, so you can use a "constant" Dictionary and modify its contents.

Does it work for Godot 4 as well?

By using a singleton in combination with a var Dictionary "statics" are still possible.

@KoBeWi
Copy link
Member

KoBeWi commented Aug 2, 2022

If you have a singleton, every property is effectively "static".

@henriquelalves
Copy link

I wanted to create a Singletone that is only instanced in a Scene, instead of populating Project Settings with an Autoload. The reason for that is that this is a Singletone that only makes sense during Gameplay (in my particular case, it generates "damage toasts" in a 3D scene), so what I had in mind was:

  • have a static var singletone
  • on ready() of that class, assign singletone to the instanced node (self)
  • have static func calling the behavior from the singletone, or throwing an error if the Singletone is not a valid instance / null.

Not using a Singletone means I'll have to pass the reference for this node around, which is cumbersome and boring. Creating an Autoload for this object is okay, but I'd much prefer having the instantiation behavior contained in my script instead of relying on Autoload, so anyone knows what to expect of the script without having to check whether this is checked on Autoload, if it's doing something during the game initialization, etc. This is kind of a middle-ground between between being quick to develop and be sufficiently scalable, and not having static variables in this case just seems like an unnecessary limitation.

@henriquelalves
Copy link

Then again, I can just create a "Singletones.gd" autoload with typed variables for each singletone I want, and populate them on-the-fly. It's not like there isn't a lot of ways to circumvent this, but developers expect static to be something that works for both data and functions, and people will lose time looking at the documentation and thinking on how to circumvent this design decision.

@h0lley
Copy link

h0lley commented Nov 10, 2022

consider having a class for all game entities that can take damage, e.g. Combatant. then implement your damage toasts in there. it's singleton btw.

@akien-mga
Copy link
Member

Superseded by #6156.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants