Why is it saying my identifier is out of scope.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By SilentCipher

In the _ready() method, I’m calling method on that is declared in the same script, _change_state(IDLE). The error is saying that my IDLE keyword in the call is out of scope, any ideas?

enum STATES { IDLE, MOVE }
var state = null

func _ready():
	_change_state(IDLE)


func _change_state(new_state):
	emit_signal('state_changed', new_state)
	
	# Initialize the new state
	match new_state:
		IDLE:
			$AnimationPlayer.play("idle")
		MOVE:
			$AnimationPlayer.play("walk")
	
	state = new_state

which identifier? is the signal “state_changed” declared in this script or another?

ener | 2021-10-08 07:46

Hello ener,

Thanks for your reply.

Yes! that method _change_state() is declared on the same script file.

Also my apologies, I’ll adjust the question.

The error is that in the line where _change_sate(IDLE) is being called. it is saying IDLE is out of scope. I’ll edit the question.

SilentCipher | 2021-10-08 16:13

:bust_in_silhouette: Reply From: Lola

Hello,
the reason is you named your enum:

enum { A, B }
enum NAMED_ENUM { C, D }

func _ready():
    print(A) # ok
    print(NAMED_ENUM.C) # you have to do this explicitely
    print(C) # out of scope

Hello Lola,

I see, Thank you for the answer. This worked the moment i updated it like that.

Do you know if there is a section in the documentation that talks about this so I can learn more on it, I haven’t been able to find it. I come from programming in C++, C#, and Java, so not naming the enum is new to me.

Thanks again.

SilentCipher | 2021-10-08 17:34

Sure! But as you can see there’s not that much to it.
Unnamed enums are a shorthand to create a lot of constants.
Named enums in 3.x are dictionaries (no more in 4.0, they’ve removed the hack) meaning you can do this:

enum MyEnum { A, B, C }

func foo():
    for key in MyEnum:
        print("MyEnum.", key, " = ", MyEnum[key])

Which prints:

MyEnum.A = 0
MyEnum.B = 1
MyEnum.C = 2

Lola | 2021-10-09 06:34