Help with error: "Script class can only be set together with base class name"

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

Hi all,

I’m getting this error that I can’t figure out, here’s (what I believe) is the relevant bit of code:

const Stance := preload("res://player/stances/")

@onready var _stances: Array[Stance] = [

The error is pointing to the @onready line, and says:

E 0:00:01:0034    _ready: Script class can only be set together with base class name
C++ Error    Condition "script.is_valid() && p_class_name == StringName()" is true.
C++ Source    core/variant/array.cpp:731 @ set_typed()
Stack Trace @ _ready()

It seems to have popped up some time without my noticing, the game still runs correctly. (I’ve also tried restarting Godot a few times.)

Any help is greatly appreciated!

:bust_in_silhouette: Reply From: takaturre

Ran into the same issue in Godot v4.beta6. In my case it happened due to using constants from another script. For example:

A static helper script:

const MY_CONSTANT := { TYPE_FLOAT: 0.0 }

Using constant in another script with error

const MyHelpers := preload("")
var test := MyHelpers.MY_CONSTANT # Error.

Using constant in another script without error

const MyHelpers := preload("")
var test := MY_CONSTANT # No problem.

Note that this didn’t happen with static methods. Only with constants.

Refactoring codes more ran into the issue again, but it was again solved similarly - transferring the constant refs:

const SomeClass := ManyClasses.SomeClass

Thoughts: This likely a tiny bug related to typing and preloading scripts/classes. Saving order might also disguise the issue temporarily in some cases. There are a lot of small typing related peculiarities that are currently being worked on in Godot v4.0 beta versions. So the issue might also disappear by updating to a newer version.