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

Scene is invalid/corrupt according to the Godot editor, but works normally ingame #79545

Closed
Tracked by #80877 ...
MewPurPur opened this issue Jul 16, 2023 · 33 comments · Fixed by #85501
Closed
Tracked by #80877 ...

Scene is invalid/corrupt according to the Godot editor, but works normally ingame #79545

MewPurPur opened this issue Jul 16, 2023 · 33 comments · Fixed by #85501
Milestone

Comments

@MewPurPur
Copy link
Contributor

MewPurPur commented Jul 16, 2023

Godot version

4.1

System information

Ubuntu 22.04.2 LTS 22.04 - Vulkan (Forward+) - integrated Intel(R) HD Graphics 4400 (HSW GT2) () - Intel(R) Core(TM) i7-4600U CPU @ 2.10GHz (4 Threads)

Issue description

This is the tscn file:

[gd_scene load_steps=2 format=3 uid="uid://bgrpmpmsi1ae2"]

[ext_resource type="Script" path="res://src/UI/LevelEditor/UserLevelsMenu.gd" id="1_0uvtn"]

[node name="UserLevels" type="Control" node_paths=PackedStringArray("initial_focus")]
layout_mode = 3
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
script = ExtResource("1_0uvtn")
initial_focus = NodePath("MainContainer/Create")

[node name="MainContainer" type="VBoxContainer" parent="."]
layout_mode = 0
anchor_left = 0.5
anchor_top = 0.5
anchor_right = 0.5
anchor_bottom = 0.5
offset_left = -40.0
offset_top = -20.0
offset_right = 40.0
offset_bottom = 20.0
grow_horizontal = 2
grow_vertical = 2

[node name="Create" type="Button" parent="MainContainer"]
unique_name_in_owner = true
layout_mode = 2
button_mask = 0
text = "#create_new"

[node name="LevelContainer" type="VBoxContainer" parent="MainContainer"]
unique_name_in_owner = true
layout_mode = 2
theme_override_constants/separation = 9

[connection signal="pressed" from="MainContainer/Create" to="." method="_on_Create_pressed"]

It results in this:
image

And the following error: "scene/resources/packed_scene.cpp:92 - Condition "nc == 0" is true. Returning: nullptr"

It also creates adds an empty scene in the scenes tabbar, and when I click on it, it automatically closes, throwing the following two errors:

  • editor/editor_data.cpp:683 - Index p_idx = 2 is out of bounds (edited_scene.size() = 2).
  • editor/editor_data.cpp:745 - Index p_idx = 2 is out of bounds (edited_scene.size() = 2).

Steps to reproduce

Unknown, only happened to one of my scenes :(

However, I can give some details surrounding the scene. I made it at some point during 4.0's alphas. I probably never touched it since. In my git, it says it's been unchanged for 5 months. However, I opened it yesterday. Not doing any changes to it, just opening it. It has a simple script, which opens normally.

Minimal reproduction project

Couldn't, this happened on my major project (far too big to post)

@DamienBoildieu
Copy link

DamienBoildieu commented Jul 16, 2023

Same thing happened to me after adding a material with a shader to a sprite node of my scene. I checked the paths in the .tscn file and they're ok.
My scene file:

[gd_scene load_steps=7 format=3 uid="uid://cp0o31yhvxjnn"]

[ext_resource type="Script" path="res://src/World/building_site.gd" id="1_e7ijw"]
[ext_resource type="Shader" path="res://src/Shaders/building_site.gdshader" id="2_e7q7u"]
[ext_resource type="PackedScene" uid="uid://brdbo8vtrmx5b" path="res://src/GUI/animated_bar.tscn" id="2_h68n2"]
[ext_resource type="AudioStream" uid="uid://bu3gbv7ov2qxs" path="res://assets/Darkworld Audio - Survival Effects [Free]/Tools/HammerNail1.wav" id="3_rbxv0"]

[sub_resource type="RectangleShape2D" id="RectangleShape2D_emhlc"]

[sub_resource type="ShaderMaterial" id="ShaderMaterial_n6v6x"]
shader = ExtResource("2_e7q7u")
shader_parameter/threshold = 0.1

[node name="BuildingSite" type="StaticBody2D"]
script = ExtResource("1_e7ijw")

[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
shape = SubResource("RectangleShape2D_emhlc")

[node name="Sprite2D" type="Sprite2D" parent="."]
material = SubResource("ShaderMaterial_n6v6x")

[node name="DisplayContainer" type="Node2D" parent="."]

[node name="AnimatedBar" parent="DisplayContainer" instance=ExtResource("2_h68n2")]

[node name="AudioStreamPlayer2D" type="AudioStreamPlayer2D" parent="."]
stream = ExtResource("3_rbxv0")
bus = &"Sound"

Its associated script:

class_name BuildingSite
extends StaticBody2D


signal building_complete(site: BuildingSite)


@export var building_modulate: Color = Color(1.0, 1.0, 1.0, 0.5)


var recipe: Recipe:
	get:
		return recipe
	set(new_recipe):
		recipe = new_recipe
		if is_ready:
			building_site_update()


var cumulated_time: float = 0.
var is_ready: bool = false
var nb_workers: int = 0
var is_finished: bool = false
@onready var sprite: Sprite2D = $Sprite2D
@onready var collision: CollisionShape2D = $CollisionShape2D
@onready var animated_bar: AnimatedBar = $DisplayContainer/AnimatedBar
@onready var audio: AudioStreamPlayer2D = $AudioStreamPlayer2D
@onready var display_container: Node2D = $DisplayContainer


func _ready() -> void:
	is_ready = true
	building_site_update()


func _process(delta):
	if not is_finished and nb_workers > 0:
		update_work(cumulated_time + nb_workers * delta)
		if cumulated_time > recipe.construction_time:
			complete_build()


func add_worker(new_workers: int) -> void:
	nb_workers += new_workers
	if work_in_progress():
		audio.play()


func remove_workers(formers_workers: int) -> void:
	nb_workers -= formers_workers
	if nb_workers < 0:
		nb_workers = 0
	if not work_in_progress():
		audio.stop()


func reset_work() -> void:
	update_work(0)


func building_site_update() -> void:
	sprite.texture = recipe.texture
	collision.shape.size = recipe.texture.get_size()
	var offset: Vector2 = collision.position - collision.shape.size/2
	offset.y -= collision.shape.size.y/2
	display_container.position = offset
	var bar_scale: float = collision.shape.size.x/animated_bar.size.x
	display_container.scale = Vector2(bar_scale, bar_scale)
	animated_bar.max_value = recipe.construction_time
	reset_work()
	is_finished = false


func update_work(value: float) -> void:
	cumulated_time = value
	animated_bar.value = value


func work_in_progress() -> bool:
	return not is_finished and nb_workers > 0


func complete_build() -> void:
	audio.stop()
	is_finished = true
	building_complete.emit(self)


func interact_with(other: Node2D) -> void:
	var charac := other as TopDownCharacter
	if charac != null:
		charac.resume_build(self)

The shader:

shader_type canvas_item;

uniform float threshold;


vec2 compute_gradients(vec4 center, vec4 left, vec4 right, vec4 top, vec4 bottom, vec2 steps) {
	vec4 grad_x = ((center - left)/steps.x + (center - right)/steps.x)/2.;
	vec4 grad_y = ((center - top)/steps.y + (center - bottom)/steps.y)/2.;
	return vec2(length(grad_x), length(grad_y));
}

void fragment() {
	vec2 steps = vec2(float(textureSize(TEXTURE, 0).x), float(textureSize(TEXTURE, 0).y));
	steps = 1./steps;
	vec4 pix_color = texture(TEXTURE, UV);
	
	vec2 pos_l = UV;
	if (pos_l.x > 0.)
		pos_l.x -= steps.x;
	vec4 col_l = texture(TEXTURE, pos_l);
	
	vec2 pos_r = UV;
	if (pos_r.x < float(textureSize(TEXTURE, 0).x)-steps.x)
		pos_r.x += steps.x;
	vec4 col_r = texture(TEXTURE, pos_r);
	
	vec2 pos_t = UV;
	if (pos_t.y > 0.)
		pos_t.y -= steps.y;
	vec4 col_t = texture(TEXTURE, pos_t);
	
	vec2 pos_b = UV;
	if (pos_b.y < float(textureSize(TEXTURE, 0).y)-steps.y)
		pos_b.y += steps.y;
	vec4 col_b = texture(TEXTURE, pos_b);
	
	vec2 grad_length = compute_gradients(pix_color, col_l, col_r, col_t, col_b, steps);
	if (length(grad_length)> threshold)
		COLOR = vec4(1., 1., 1., 1.);
	else
		COLOR = vec4(0., 0., 0., 0.0);
}

Edit: I restarted the app a third time and this time, I could opened the file. It feels even more disturbing.

@KoBeWi
Copy link
Member

KoBeWi commented Jul 16, 2023

Sounds like #78128

@dalexeev
Copy link
Member

See also #74253 (comment). I also think this is a preload issue.

@MewPurPur
Copy link
Contributor Author

I got this every time in 4.1, but updating to 4.1.1 fixed it immediately. Huh.

@PLyczkowski
Copy link

Here's what working for me to fix this error: close editor, search in the .editor folder for all files containing the corrupted scene's name (they are going to be name something like your_scene_name.tscn-editstate-2e5312b3e75d2338868814f479b1feeb.cfg), delete them, restart editor.

@MewPurPur
Copy link
Contributor Author

Oh, is it still a thing? I thought it was fixed, but maybe it just randomly disappeared when I updated once.

@MewPurPur MewPurPur reopened this Nov 16, 2023
@PLyczkowski
Copy link

I'm on 4.2 beta5 atm. Scene is invalid/corrupt error seems to have multiple causes, so my source of this issue may be different, but in my case the steps above fix the problem.

@KoBeWi
Copy link
Member

KoBeWi commented Nov 16, 2023

But is it related to this particular issue, i.e. scene was corrupt, but had no problems at runtime?

@akien-mga akien-mga modified the milestones: 4.2, 4.x Nov 16, 2023
@PLyczkowski
Copy link

@KoBeWi Hmm, if I recall correctly errors were thrown at runtime as well, though my amateur interpretation was that the deleted editstate and folding .cfg files shouldn't have an effect on runtime since they seem to relate to the editor

@wbwqq
Copy link

wbwqq commented Nov 18, 2023

Here's what working for me to fix this error: close editor, search in the .editor folder for all files containing the corrupted scene's name (they are going to be name something like your_scene_name.tscn-editstate-2e5312b3e75d2338868814f479b1feeb.cfg), delete them, restart editor.

Had the exact same issue on 4.1.3 stable, and this fixed it!

@PLyczkowski
Copy link

Got the error again, and the above steps did not help this time. What helped was: duplicate scene in a file explorer (not in Godot editor, since the error stays that way), open the duplicated scene (it opens without the error), 'Save As' the scene overwriting the original scene.

@PLyczkowski
Copy link

PLyczkowski commented Nov 27, 2023

This issues keeps reoccurring for me (4.2 rc2)...

Updated instruction on how to (temporarily) fix, since the above instructions not always worked for me (do each step for each corrupted scene file, as I found there can be more than one):

  • Duplicate scene file in external editor
  • Open duplicated scene in godot, overwrite on the original corrupted scene
  • Close all scripts and open scenes in the editor, close editor
  • Delete all files in the .godot/editor folder containing the corrupted scene's name (they are going to be name something like your_scene_name.tscn-editstate-2e5312b3e75d2338868814f479b1feeb.cfg)
  • Reopen editor

@KoBeWi
Copy link
Member

KoBeWi commented Dec 11, 2023

Can you test the issue after #85083? (available in 4.2)
You should get more information why the scene can't be opened.

@PLyczkowski
Copy link

For me the issue hasn't reappeared in 4.2 stable yet. Will try this out if it does.

@JRileyH
Copy link

JRileyH commented Dec 31, 2023

This issue keeps occuring for me in 4.2 stable.

error

  scene/resources/resource_format_text.cpp:284 - res://src/room.tscn:8 - Parse Error: 
  Failed loading resource: res://src/room.tscn. Make sure resources have been imported by opening the project in the editor at least once.
  Failed to instantiate scene state of "res://src/room.tscn", node count is 0. Make sure the PackedScene resource is valid.

room.tscn

[gd_scene load_steps=3 format=3 uid="uid://boqpfrvhnl4es"]

[ext_resource type="Script" path="res://src/room.gd" id="1_306kj"]

[sub_resource type="RectangleShape2D" id="RectangleShape2D_74ys4"]

[node name="Room" type="Node2D" groups=["room", "selectable"]]
script = ExtResource("1_306kj")

[node name="Area2D" type="Area2D" parent="."]

[node name="CollisionShape2D" type="CollisionShape2D" parent="Area2D"]
shape = SubResource("RectangleShape2D_74ys4")

[node name="SystemSprite" type="Sprite2D" parent="."]
scale = Vector2(3, 3)

@PLyczkowski 's solution still works, albeit kind of annoying. I open VS Code, copy paste room.tscn to room2.tscn. Go back to Godot editor delete room.tscn. Then rename room2.tscn to room.tscn and the scene will then load.

@neruthes
Copy link

neruthes commented Jan 19, 2024

I had the same problem on 4.2.1 stable. The wordaround rm .godot/editor/* did help. Looking forward to seeing a real fix in future. My scene does not have preload() in its attached script; nor does it contain another PackedScene as a child in the tree.

@KoBeWi
Copy link
Member

KoBeWi commented Jan 20, 2024

Reproduction project:
godot-bug-mwe-1-master.zip
Bee.tscn can't be opened in editor. Seems to be caused by preload.

@llagerlof
Copy link

@KoBeWi

Seems to be caused by preload.

I wasn't even thinking about this, but when that problem happened to me I was working on preloading some scenes. After saving and reopening the project, "Scene is invalid/corrupt".

@PLyczkowski
Copy link

PLyczkowski commented Feb 11, 2024

Got the same error message as @JRileyH on mine in 4.2.1 after the issue reappeared: "scene/resources/resource_format_text.cpp:284 - res://card_engine/board.tscn:10 - Parse Error:
Failed loading resource: res://card_engine/board.tscn. Make sure resources have been imported by opening the project in the editor at least once. Failed to instantiate scene state of "res://card_engine/board.tscn", node count is 0. Make sure the PackedScene resource is valid."

I noticed I am preloading a script with a class name in there while the script already registered that name using class_name.

So in corrupted scene:
var Entity = preload("res://card_engine/entity.tscn")
while entity.gd has:
class_name Entity
as well as
Zone = preload("res://card_engine/zone.gd")
when zone.gd already has:
class_name Zone

@VisitingOcean
Copy link

VisitingOcean commented Feb 14, 2024

Same issue/error on 4.2.2. On my 3rd scene rebuild cycle. I ensured my variable names were not the same as the class names that were being preloaded and the corruption happened without any changes to the scene before corruption.

@MewPurPur
Copy link
Contributor Author

Got a scene in another project that's glitched like this. 2 in 3 times I open Godot, it refuses to open, but sometimes it does open correctly, if I close and re-open Godot enough times. It's on my open-source project, and other contributors seem to also be having the issue.

@VisitingOcean
Copy link

Is there a known way to structure things to avoid this problem? Project I am working on is pretty much at a standstill due to it as all attempts to work around it have resulted in it again.

@neruthes
Copy link

Is there a known way to structure things to avoid this problem? Project I am working on is pretty much at a standstill due to it as all attempts to work around it have resulted in it again.

Use another editor to remove preloads in scripts. Type annotation can be seen as an indirect preload so remove them as well. Write export var PackedScene instead of preload tscn. These workarounds should be sufficient to make the project editable again before the fix is delivered in 4.3.

@VisitingOcean
Copy link

Is there a known way to structure things to avoid this problem? Project I am working on is pretty much at a standstill due to it as all attempts to work around it have resulted in it again.

Use another editor to remove preloads in scripts. Type annotation can be seen as an indirect preload so remove them as well. Write export var PackedScene instead of preload tscn. These workarounds should be sufficient to make the project editable again before the fix is delivered in 4.3.

Thank you for the guidance. The corrupted scene does not have any preloads in it's script. Should this be done for all scripts attached to scenes that load into the corrupted scene? The scenes included into the corrupted scene were added through the editor and not programmatically.

@neruthes
Copy link

Is there a known way to structure things to avoid this problem? Project I am working on is pretty much at a standstill due to it as all attempts to work around it have resulted in it again.

Use another editor to remove preloads in scripts. Type annotation can be seen as an indirect preload so remove them as well. Write export var PackedScene instead of preload tscn. These workarounds should be sufficient to make the project editable again before the fix is delivered in 4.3.

Thank you for the guidance. The corrupted scene does not have any preloads in it's script. Should this be done for all scripts attached to scenes that load into the corrupted scene? The scenes included into the corrupted scene were added through the editor and not programmatically.

This particular situation is more complicated than I expected. I can only wish you good luck.

@VisitingOcean
Copy link

Is there a known way to structure things to avoid this problem? Project I am working on is pretty much at a standstill due to it as all attempts to work around it have resulted in it again.

Use another editor to remove preloads in scripts. Type annotation can be seen as an indirect preload so remove them as well. Write export var PackedScene instead of preload tscn. These workarounds should be sufficient to make the project editable again before the fix is delivered in 4.3.

Thank you for the guidance. The corrupted scene does not have any preloads in it's script. Should this be done for all scripts attached to scenes that load into the corrupted scene? The scenes included into the corrupted scene were added through the editor and not programmatically.

This particular situation is more complicated than I expected. I can only wish you good luck.

If I figure anything out in my troubleshooting I'll report it here. Thanks for the help!

@VisitingOcean
Copy link

I rebuilt the scene last night. Had it fully working. The scene has no direct preloads, or loads in its script. Through the editor I instantiated my character scene which does preload the pause menu. No issues while working on it for about 2 hours last night. When I woke up today the scene is corrupt. I verified with git diff that the scene file had not changed at all. The only things in the scene were the instantiated character and a tilemap.

The same bug seems to be also effecting a plugin I am using Dialogue Nodes. With this plugin a set of characters is defined in a tres file. I have found that anytime the main scene becomes corrupt, the character.tres file also stops working inside the editor and errors with 'Parse Error: Node export is only supported in Node-derived classes, but the current class inherits "Resource".' which is super weird because as with the scene, nothing has changed since last night.

The only way ive been able to get things working is by deleting and recreating all effected scenes/files from scratch. I had tried manually removing ext lines from the scene, deleting things in the .godot/editor folder, and a few other of the fixes I have found in other threads that are not on the top of my mind at the moment.

Don't know if any of this is helpful but trying to provide as much data as possible for people more knowledgeable than me.

@VisitingOcean
Copy link

Apologies for the data dumps but want to provide as much information as I can. This time I was able to get the main scene working by deleting the reference to the player:

[node name="player" parent="." instance=ExtResource("2_u5me5")]

The character.tres file is still an issue, but I was able to recover the main scene. The player node's script does do quite a bit of preloading and child creation.

@PLyczkowski
Copy link

I noticed I am preloading a script with a class name in there while the script already registered that name using class_name.

So in corrupted scene: var Entity = preload("res://card_engine/entity.tscn") while entity.gd has: class_name Entity as well as Zone = preload("res://card_engine/zone.gd") when zone.gd already has: class_name Zone

Nope, removing those did not stop the issue from reappearing

@romlok
Copy link
Contributor

romlok commented Feb 24, 2024

I've also been experiencing this with one the scenes in my latest project.

I think one thing that's sometimes gotten confused along the way is that (at least in some cases) it's the scene which is being preloaded that gets corrupted, not the the scene doing the preloading.

On testing out the repro bee project, I noticed that it only seems to happen the first time you open that project - ie. before the .godot directory is created. If you already have a .godot directory from having just opened the project, you don't get the corruption error and can open the bee scene just fine!
However, this has not been the case for my project. 😕

@akien-mga akien-mga modified the milestones: 4.x, 4.3 Feb 24, 2024
@redsett
Copy link

redsett commented Feb 25, 2024

Just to add to this thread. I have a fairly large project. I just now hit this randomly in 4.2 stable.

Replacing "preload" with "load" did fix the problem. And I don't have any name clash issues like others have reported.

@LeifintheWind
Copy link

Getting this issue or something similar on v4.2.stable.official [46dc277]. I have a script A with a preload of scene B, and the script B for that scene had a preload of a different scene C (that did not have any preloads).

Removing the preload reference in either script solves the issue. But I still need a way to load that scene C, and using preload or even load cause the error. I've duplicated the "corrupted" scene a few times and attempted to reset it based on the suggestions here, but to no avail.

Is the merged fix supposed to work for the latest 4.2 stable version? Or 4.3

@AThousandShips
Copy link
Member

This hasn't been cherry picked but is planned to be in a future 4.2.x version (note the PR has the cherry pick:4.2 tag)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.