Identifier not declared in current scope

Godot Version

4.1.1

Question

Hi,

I am trying to switch from importing classes with preload() to defining class_name and use them globally.
It works fine with most of my classes, but one of them triggers a "Identifier “Track” not declared in current scope.
Here is the (dead simple) content of track.gd:

extends RefCounted
class_name Track

var mode: Constants.ModeEnum
var max_val = INF

func _init(mode_: Constants.ModeEnum):
    self.mode = mode_

What bugs me is that other really similar classes are referred no problem by their class_name when this one can’t.
I tried changing the name of the class and file in case it hid another godot class name but I get the same behavior.

Thanks in advance for your help!

Try removing _init() function from it and see if it helps.

Don’t forget to restart Godot editor every time you encounter seemingly unexplainable error. Very often it helps.

Thank you for your answer.
Removing the _init() did not help.

I am using the headless version, and restarting did not solve the issue. I had to manually edit .godot/global_script_class_cache.cfg to conform to which of my classes have a class_name specified.
I guess that’s what would happen automatically when loading the editor UI. It would be nice if the headless version behave the same, though.

Not sure if that’s the issue but I think _init() always either needs to work without parameters or must include default values for parameters to work.
So try to set a default value for mode_ to see if that helps?

As stated, this was not the issue. I actually never had issues with arguments in _init() functions. Maybe you refer to some specific types like extends Node that may be automatically instanciated, with no parameter, upon scene mounting?

The thing is that .instantiate() function uses a parameterless constructor only.

That means that if you create your instances through .instantiate() insead of .new(), then your classes MUST have parameterless constructors. Otherwise it would fail. The worst thing is that it would fail silently (no errors in console, no crashes, it just doesn’t work and you don’t know why).

It’s probably not related to your particular issue, but it’s still a useful thing to know.

See more:

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.