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

Singleton's cannot be used as const in a module #44837

Closed
chucklepie opened this issue Dec 31, 2020 · 4 comments
Closed

Singleton's cannot be used as const in a module #44837

chucklepie opened this issue Dec 31, 2020 · 4 comments

Comments

@chucklepie
Copy link

Godot version:
3.2.3

OS/device including version:
Linux

Issue description:
I know enums don't work very well with singletons/exports but I have the following in an autoload:

const COLOURS = { 
	"black" 		: [0.0,0.0,0.0],
	"blue" 			: [0.0,0.0,192.0/256.0]
}

If I try to create a constant using it:

var y = Globals.COLOURS.blue     #works fine as var
const z = Globals.COLOURS.blue

I get error invalid index COLOURS in constant expression.

Maybe this is more a 'feature request', but you would expect autoload variables to be loaded first or at least a script able to know of the autoload's existence as it parses the code...

image

@Calinou
Copy link
Member

Calinou commented Dec 31, 2020

A singleton reference is not constant because the first part (Globals) is a result of get_node(), which itself is not a constant expression. I wouldn't consider this to be a bug.

Stuff like this could be solved by introducing immutable variables, but it's not likely to happen soon.

@Calinou Calinou added discussion and removed bug labels Dec 31, 2020
@chucklepie
Copy link
Author

chucklepie commented Dec 31, 2020

I get what you're saying, but constants are "known at compile-time" according to the docs, which implies it is defined regardless where it is declared, it is simply known at compile time?

So an autoload having a constant means the value is known at compile time. So that implies that a const based off a const is also perfectly valid because that is done at compile time?

@omicron321
Copy link
Contributor

why use globals, when you can do MyClass.MY_CONST

@KoBeWi
Copy link
Member

KoBeWi commented Dec 31, 2020

Autoloads are just references to a node. They can be modified, removed or replaced, so they can't be const.

However your code will work just fine when you add class_name.

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

6 participants