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

Accessing a member variable before declaring a variable that would overshadow returns null #54944

Closed
DND-dou opened this issue Nov 13, 2021 · 4 comments · Fixed by #79880
Closed

Comments

@DND-dou
Copy link

DND-dou commented Nov 13, 2021

Godot version

4.0-dev.20211108

System information

Ubuntu20.04,headless,HD Graphics 530

Issue description

I'm using Callable in Godot4.The following code works well:

func test():
	print("in test")

func _ready():
	print("_ready:")

	print(test)
	test.call()

output:

_ready:
Node(Node2D.gd)::test
in test

But when I write this:

func test():
	print("in test")

func _ready():
	print("_ready:")

	print(test)
	print(typeof(test))

	var test=12

Output:

_ready:
null
0

Similarly, when I get a function with the same name before a variable declaration, I get null

Is it working properly?

If my grammar is OK,Why do I get null instead of Callable?
Is my grammar wrong? Godot4 doesn't have any reminders!

Steps to reproduce

run this in 4.0.dev

func test():
	print("in test")

func _ready():
	print("_ready:")

	print(test)
	print(typeof(test))

	var test=12

Minimal reproduction project

No response

Production team edit: added highlighting

@dalexeev
Copy link
Member

dalexeev commented Nov 13, 2021

Similarly, when I get a function with the same name before a variable declaration, I get null

It looks like local variable declarations affect the whole function/block. That is, your code is actually:

func test():
    print("in test")

func _ready():
    var test # (It happens implicitly.)

    print("_ready:")

    print(test)
    print(typeof(test))

    test = 12

Probably, the compiler should print an error/warning "Using a local variable before its declaration".

Use self.<name> to access the class members if the function has a local variable with the same name.

@KoBeWi
Copy link
Member

KoBeWi commented Nov 13, 2021

There is already a warning about shadowing method/variable names (the example code here shows one too), so the solution is to not use conflicting names,

@DND-dou
Copy link
Author

DND-dou commented Nov 14, 2021

Similarly, when I get a function with the same name before a variable declaration, I get null

It looks like local variable declarations affect the whole function/block. That is, your code is actually:

func test():
    print("in test")

func _ready():
    var test # (It happens implicitly.)

    print("_ready:")

    print(test)
    print(typeof(test))

    test = 12

Probably, the compiler should print an error/warning "Using a local variable before its declaration".

Use self.<name> to access the class members if the function has a local variable with the same name.

@dalexeev
yes,I see.But that's strange!
When I use such code:

func _ready():
	print("_ready:")
	
	print(val)
	var val=12

Output:

ERROR: Parse Error: Identifier "val" not declared in the current scope.
   at: GDScript::reload (res://Node2D.gd:10)
ERROR: Method/function failed. Returning: ERR_PARSE_ERROR
   at: reload (modules/gdscript/gdscript.cpp:855)

it's ERROR!!!
So I can't use a variable before declaring it!!!
Godot does not move variable declarations to the front!
So if this is true, when will Godot move the variable declaration to the front?

@akien-mga akien-mga modified the milestones: 4.0, 4.1 Feb 20, 2023
@akien-mga akien-mga changed the title The Callable obtained is unusable Accessing a member variable before declaring a variable that would overshadow returns null Mar 6, 2023
@akien-mga akien-mga modified the milestones: 4.1, 4.2 Jun 23, 2023
@dalexeev
Copy link
Member

With #79880 this will work as expected, but will generate warnings.

func test():
    pass

func _ready():
    print(test) # Node(node.gd)::test
    print(typeof(test)) # 25

    var test = 12

    print(test) # 12
    print(typeof(test)) # 2

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