Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
nicom7 opened this issue Sep 26, 2023 · 18 comments

Comments

@nicom7
Copy link

nicom7 commented Sep 26, 2023

Godot version

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

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

@myyk
Copy link

myyk commented Dec 6, 2023

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
Copy link
Contributor

hakuhan commented Dec 12, 2023

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
Copy link

One workaround for resolving this issue appears to be:

  1. Comment out the class_name statement
  2. Save
  3. Uncomment class_name statement

@scriptsengineer
Copy link

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
Copy link
Contributor

hakuhan commented Mar 12, 2024

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
Copy link

scriptsengineer commented Mar 12, 2024

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
Copy link

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
Copy link

cortrano commented Apr 18, 2024

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
Copy link

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

@mattiasboden
Copy link

mattiasboden commented Aug 12, 2024

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
Copy link

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
Copy link
Contributor

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.

@webskydavid
Copy link

Hi, I have the same issue on 4.4 dev 3 (not mono) (macos Sonoma 14.4.1) when using ImGui addon. It starts working after i remove the .godot folder. But doing it every time before I open the project is annoying.

@mattiasboden
Copy link

Is there any ETA on this issue please? It's really annoying, I thought it would have higher priority :)

@bootcher007
Copy link

I believe I have same issue
Godot v4.3 stable Windows 10 Pro
Just to be sure it's the same issue here is my code:
file enemy_manager.gd:
class EnemyManager extends Node3D:
#some code
file monster_eye.gd:
extends Node3D
@onready var enemy_manager : EnemyManager = %EnemyManager #node name is same as class name

I wanted to do static typing for method and variable name suggestions.
I tried workaround of resaving class EnemyManager, but it did not help

@Radivarig
Copy link

Happened to me after renaming a script then adding class_name to it.

@mattiasboden
Copy link

I've updated to 4.4 beta 1 and do not see this error anymore. Is it fixed (or am I just having a "temporarily" working project)?
Anyone else seeing this issue in 4.4 b1?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: For team assessment
Development

No branches or pull requests