godotengine / godotengine/godot
Dynamic `GDScript` cannot resolve `class_name` in script
- Dominant language
- C++
- Stars
- 117k
- Forks
- 26.8k
- PR merge metrics
- PR metrics pending
Description
### Tested versions
- Reproducible in Godot v4.3.stable
### System information
Windows 10.0.22631 - Vulkan (Forward+) - dedicated NVIDIA GeForce RTX 4060 Ti (NVIDIA; 32.0.15.6109) - Intel(R) Core(TM) i7-10700KF CPU @ 3.80GHz (16 Threads)
### Issue description
When using the `GDScript` class to dynamically create a script, the `class_name` fails to resolve within the same script and results in the following error:
> Parser Error: Identifier not found: Foo
Attempting to save the script to a file and loading it using `load()` at run-time also doesn't work. Loading the same script statically when created in the editor works.
This is an issue when developing a code generator for Godot as testing it is impossible without dynamically creating classes. It is also a problem when creating GDScript-based mods.
### Steps to reproduce
```gdscript
func _ready() -> void:
var gdscript := GDScript.new()
gdscript.source_code = "class_name Foo extends Resource
static func get_foo() -> Foo:
return Foo.new()
"
gdscript.reload()
var foo : Variant = gdscript.new()
print(foo)
```
### Minimal reproduction project (MRP)
[project.zip](https://github.com/user-attachments/files/17686435/project.zip)
Contributor guide
Assessment
This issue has not been assessed yet.