godotengine / godot

Godot Engine – Multi-platform 2D and 3D game engine
https://godotengine.org
MIT License
88.88k stars 20.15k forks source link

Error: Could not find type "x" in the current scope when duplicating script files #82392

Open nicom7 opened 11 months ago

nicom7 commented 11 months ago

Godot version

v4.1.stable.mono.official [970459615]

System information

Godot v4.1.stable.mono - Windows 10.0.19045 - Vulkan (Forward+) - dedicated NVIDIA GeForce GTX 1070 (NVIDIA; 31.0.15.3623) - Intel(R) Core(TM) i7-6700 CPU @ 3.40GHz (8 Threads)

Issue description

Duplicating a type_1.gd file to create a new script type_2.gd out of the existing one will cause parser errors like Could not find type "Type1" in the current scope for other scripts that depend on the existing Type1 class.

Reloading the project has no effect but removing the .godot folder will fix the issue. In the same way, the attached example doesn't include the type_2.gd script because the issue doesn't occur when the project has not been opened before.

Steps to reproduce

  1. Create a main scene and attach a script main.gd
  2. Create a new script type_1.gd with an explicit class_name Type1
  3. In the main.gd script add a variable of type Type1
  4. Duplicate the type_1.gd file to type_2.gd and rename the class name to Type2
  5. Go back to the main scene script or run the game: The parser error will be shown

Minimal reproduction project

bug_unresolved_type.zip

dalexeev commented 11 months ago

See also:

myyk commented 9 months ago

This bug happened to me too on Godot v4.1.stable.mono as well. Thanks for the bug report @nicom7. The workaround to delete .godot/ worked for me as well.

hakuhan commented 9 months ago

I tried re-save all missing scripts. Editor will find these "x". And these types will be store in ".godot/global_script_class_cache.cfg" file of project root. So I tried remove all the lines in that file. And issues appear again. But when I reset back lines in that file. Issues are gone. I have added ".godot/global_script_class_cache.cfg" file to my .gitignore to prevent these issues happening again when I download my project from a new computer.

Umopepisdn commented 8 months ago

One workaround for resolving this issue appears to be:

  1. Comment out the class_name statement
  2. Save
  3. Uncomment class_name statement
scriptsengineer commented 6 months ago

This is very unpleasant, especially when you had a large project and had to download it to another PC, basically all scripts are like this. Is there any record of a possible solution?

hakuhan commented 6 months ago

This is very unpleasant, especially when you had a large project and had to download it to another PC, basically all scripts are like this. Is there any record of a possible solution?

I submit ".godot/global_script_class_cache.cfg" file to remote. It seems working for me. Event I think it's not a good solution.

scriptsengineer commented 6 months ago

Important Update for those who will fix this

In my project there was a class that generated this import error, crashing the editor, and after restarting the .godot\global_script_class_cache.cfg file it was empty.

However, changing my file to extend the class traditionally using the name of the base class for the script path solved the problem, when importing now it doesn't crash and when I restart the editor follows the normal editor flow recognizing all scripts and correctly filling the file .godot\global_script_class_cache.cfg

I hope that identifying these problematic scripts will be useful to everyone. My theory is that the importer gets lost when it comes to importing scripts that have a hierarchy.

It is worth noting that in my case the problematic script extended a class that extended another class that was in a gdextension.

My changes: image

DeoVolenteGames commented 5 months ago

I also encountered this error message recently when importing this inventory system addon, which I believe is the same project as mentioned in scriptsengineer's post. The issue in my case was an existing script I wrote with the same name as one of the C++ classes in the addon. After rolling back and renaming the script I could import the addon without errors.

It was very confusing at the time, as it seemed to corrupt everything that referenced a gdextension class in some way, including previously working addons. I'm not sure if it's exactly the same error because of this.

cortrano commented 4 months ago

Same problem with dialogue manager addon. When i delete /.godot folder and open project, i get error "Could not find type "x" in the current scope" for every custom class_name in this addon.

@Umopepisdn method works, but it's a nightmare, because addon has many custom classes and I have to comment/uncomment every one.

Tested on 4.2.1 and 4.2.2

RemotelyHuman commented 1 month ago

I've also encountered this bug in my project. Godot 4.2.2 on Windows 10 22H2.

mattiasboden commented 1 month ago

Also encountering this, seems to be when using add-ons. I'm using the C# version, suspecting there is something going on with c# and gdscript not fully connecting with each other. Getting the error with dialogic, dialogue manager and also a music manager addon. Using latest Godot 4.3 RC3

mattiasboden commented 4 weeks ago

Just suspecting this one might be something a lot of users run into. Any way to ask for priority on this issue? Still happening with 4.3 Stable.

evan-gordon commented 3 weeks ago

I'm experiencing this with the Godot Voxels library.

One workaround for resolving this issue appears to be:

1. Comment out the class_name statement

2. Save

3. Uncomment class_name statement

In my scenario this doesn't work since the class that is throwing the error VoxelStreamRegionFiles is baked into the custom build of the engine :)

In my case though, I only have the issue when exporting the project.