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

Please don't stop assignning values to variables decleared with const #21177

Closed
Geequlim opened this issue Aug 19, 2018 · 5 comments
Closed

Please don't stop assignning values to variables decleared with const #21177

Geequlim opened this issue Aug 19, 2018 · 5 comments

Comments

@Geequlim
Copy link
Contributor

Geequlim commented Aug 19, 2018

Godot version:

Godot 3.1.dev compiled with 8bc9986

OS/device including version:

Arch Linux x64

Issue description:

In GDScript I always define static variables with the keyword const .
The code below works fine from godot 2 to godot 3.0.

class MyClass:
	const static_variable = 100
	
	static func get_static_value():
		return static_variable
	
	static func set_static_value(p_value):
		static_variable = p_value

func _ready():
	MyClass.set_static_value(100)
	print(MyClass.get_static_value())

In the new version of godot compiled from master branch. This code parse failed with this message

Parser Error: Cannot assign a new value to a constant.

Please consider remove this syntax checking or add a new way to define static variables like this

static var static_value = 1
@Geequlim
Copy link
Contributor Author

CC @vnen

@ghost
Copy link

ghost commented Aug 19, 2018

You've been using bug as feature. I don't think we're going to support that.

Reassigning const bug: #6221 #15200 #20179
Static var: #6840

@Zylann
Copy link
Contributor

Zylann commented Aug 19, 2018

Note: there is a simpler legit way to do the same thing you just did:

Create a normal script inheriting Resource, put non-const vars inside, create a Resource in the inspector, put the script on it, and save as file. Now you can preload it and use it as pseudo-singleton with shared vars as long as the resource is referenced somewhere at runtime.
But honestly, an autoload is closer to globals if that's what you want.

This still isn't global vars, but the hack you used technically did that AFAIK.

@vnen
Copy link
Member

vnen commented Aug 19, 2018

If this is really wanted we can add static variables (assuming it could work properly, see discussion in #6840). But allowing constants to be reassigned is a big problem and maybe the source of many game bugs when done by mistake, so I'm 100% against it.

@akien-mga
Copy link
Member

Indeed, this was a bug and we won't reintroduce it. But discussion can continue in #6840 regarding adding proper static variables.

@akien-mga akien-mga added this to the 3.1 milestone Aug 20, 2018
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

4 participants