godotengine / godotengine/godot

Dynamic `GDScript` cannot resolve `class_name` in script

Open
#98,985 4 comments 0 reactions 0 assignees View on GitHub
bug discussion topic:core topic:gdscript
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

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.