Parser Error: Could not resolve class "res://scenes/object_template.gd"

Hi everyone, I’ve spent a few hours on trying to solve this but I’m stuck on what to do next.

I have a parent scene named object_template.tscn. I have created an inherited scene named bin_template.tscn which adds some additional export variables to the class. Another scene is inherited from this named rubbish_pile.tscn.

When I run my project I get the error below

Parser Error: Could not resolve class “res://scenes/object_template.gd”.

This seems to be happening when the scene is preloaded. I’m doing this in global.gd using the code below:

var PestScenes = {
	"rubbish_pile": preload("res://scenes/pests/rubbish_pile.tscn")
}

If I replace that with the following the project starts fine:

var RubbishPileScene = preload("res://scenes/pests/rubbish_pile.tscn")

I have plenty of other scenes being preloaded using the first syntax and there are no issues, and I can’t work out why this one is different. Since it works using the second option I don’t think there are any problems with the code in any of the scenes, but must be something about the way I’m preloading the scene on start-up.

Any ideas on what I should be checking?

Godot saves tables with JSON, which only allows integers and strings, not instances

I have other examples in my code where this approach is working, for example:

var ObjectScenes = {
	"large_light": preload("res://scenes/amenities/large_light.tscn"),
	"small_light": preload("res://scenes/amenities/small_light.tscn")
}
1 Like

Can you show us the gd script for more reference?

Also, your ObjectScenes table is not the same as the PestScenes. I hope you are aware of that when reviewing your code.

This is the code that’s triggering the error:

class_name BinTemplate
extends ObjectTemplate

# this is the node in this scene which holds the rubbish animations
# there should be 4 animations - rubbish0, rubbish1, rubbish2, rubbish3
@export var AnimatedObject : Node3D

# how much rubbish the bin can hold
# the amount of rubbish is tracked in Dirtiness
@export var RubbishCapacity : float = 100

func local_ready():
	AnimatedObject.get_node('AnimationPlayer').play("rubbish0")

func local_process(delta):
	if (Dirtiness / RubbishCapacity) <= 0.1:
		AnimatedObject.get_node('AnimationPlayer').play("rubbish0")
	elif (Dirtiness / RubbishCapacity) <= 0.3:
		AnimatedObject.get_node('AnimationPlayer').play("rubbish1")
	elif (Dirtiness / RubbishCapacity) <= 0.7:
		AnimatedObject.get_node('AnimationPlayer').play("rubbish2")
	else:
		AnimatedObject.get_node('AnimationPlayer').play("rubbish3")
	
	# too much rubbish so the bin needs to overflow
	if Dirtiness > RubbishCapacity:
		if get_node_or_null("Rubbish/RubbishPile") == null:
			# create a rubbish pile
			var rubbish = Global.RubbishPileScene.instantiate()
			$Rubbish.add_child(rubbish)
			
		# how much excess Dirtiness is there?
		var extra_dirtiness = Dirtiness - RubbishCapacity
		# remove this from the bin
		Dirtiness -= extra_dirtiness
		# add it to the rubbish pile
		$Rubbish/RubbishPile.Dirtiness += extra_dirtiness
	
	if Dirtiness <= 0 and ObjectType == "pest":		# rubbish pile which has been cleaned up
		self.queue_free()		# remove the rubbish pile

I have two inherited scenes built on the same bin_template.tscn / bin_template.gd. I have also tried preloading them using the following code:

var RubbishBinScene = preload("res://scenes/amenities/rubbish_bin.tscn")
var RubbishPileScene = preload("res://scenes/pests/rubbish_pile.tscn")

This works:

var RubbishBinScene = preload("res://scenes/amenities/rubbish_bin.tscn")
#var RubbishPileScene = preload("res://scenes/pests/rubbish_pile.tscn")

But this doesn’t:

#var RubbishBinScene = preload("res://scenes/amenities/rubbish_bin.tscn")
var RubbishPileScene = preload("res://scenes/pests/rubbish_pile.tscn")

and neither does this:

var RubbishBinScene = preload("res://scenes/amenities/rubbish_bin.tscn")
var RubbishPileScene = preload("res://scenes/pests/rubbish_pile.tscn")

so it feels like the problem is rubbish pile.

Also, your ObjectScenes table is not the same as the PestScenes. I hope you are aware of that when reviewing your code.

What do you mean by this?

A little confused. In your first post you state that this works:
var RubbishPileScene = preload("res://scenes/pests/rubbish_pile.tscn")
Here in your latest post you say it doesn’t.

#var RubbishBinScene = preload("res://scenes/amenities/rubbish_bin.tscn")
var RubbishPileScene = preload("res://scenes/pests/rubbish_pile.tscn")

Is the class BinTemplate the final class or are you doing
class_name RubbishBin/RubbishPile extends BinTemplate.
If so then check that code.
Probably will need to see ObjectTemplate.
Are RubbishBin and RubbishPile the same root node type?

Hi Sancho.

Initially rubbish_pile.tscn on its own worked, but then it stopped working. I didn’t make any changes to the code in any of the gd files, it seems it worked once then subsequent runs it gievs the “could not resolve class” error.

Today another file which extends ObjectTemplate has also started throwing the same error.

[EDIT: I have restored a back-up which has fixed this file, but do not have a back-up from before BinTemplate was having problems]

I’ve shared copies of bin_template.gd, global.gd and object_template.gd at the link below. BinTemplate is the final class, RubbishBin and RubbishPile are the same class, the only difference is some of the 3D models in the scene but otherwise referencing the same .gd file.

https://bpa.st/M5CA

The root node for all of these is Node3D.

Ok, I’ve recreated rubbish_pile.tscn and done a bunch of testing at each step along the way. I think I’ve pinpointed the point at which the error is introduced.

I have ObjectTemplate.

Then BinTemplate extends ObjectTemplate.

rubbish_pile.tscn is inherited from BinTemplate.

Everything is fine until the moment I add a line of code in ObjectTemplate which instantiates the rubbish pile with the code:

Global.RubbishPileScene.instantiate()

I think it has something to do with ObjectTemplate instantiating a scene which is inherited from it, but I don’t understand why. Can anyone help shed some light on this for me?

I understand the problem here is that the ObjectTemplate is trying to instantiate a class which extends itself, creating a kind of class loop.

To get around this, I have added a function in Global which instantiates RubbishPile and returns it back to the calling module, in this case ObjectTemplate.

ObjectTemplate is then able to use that scene in its own processing.

Thank you to all the people who helped me work through this both here and on Discord.

Its called cyclic dependency (or circular dependency).

1 Like