godot-python icon indicating copy to clipboard operation
godot-python copied to clipboard

Cannot load because it doesn't expose any class to Godot error when changing scene to the second time

Open steven-thegamer opened this issue 1 year ago • 3 comments

I got these error when I try to load a scene for the second time.

Pythonscript 0.50.0 (CPython 3.8.5.final.0) hi Cannot load res://test_node.py (test_node) because it doesn't expose any class to Godot

The 'hi' is from a python script attached to a Godot Node with the task to print('hi') on ready. When I change the scene to another scene and open the same scene again, this error pops up as if it can't load the python script. Not the node, but the script itself only. This is what the script does.

from godot import exposed, export
from godot import *


@exposed
class test_node(Node):
	def _ready(self):
		print('hi')

What's wrong with this and does anoyne have a slution?

steven-thegamer avatar Jun 29 '24 09:06 steven-thegamer

Plese reset godot to fix error

hqmdokkai avatar Oct 24 '24 12:10 hqmdokkai

Bumping this, because I hit a very similar problem. Reloading the project doesn't work, I tried resetting godot (turning it off and on) didn't help. Also this problem happens only if the scene contains a python script in a node.. I "fixed" it by changing the script to a GDScript, but that isn't really a good solution..

Have you or anyone else found a fix for this?

S0u1y avatar Nov 29 '24 23:11 S0u1y

Bumping this, because I hit a very similar problem. Reloading the project doesn't work, I tried resetting godot (turning it off and on) didn't help. Also this problem happens only if the scene contains a python script in a node.. I "fixed" it by changing the script to a GDScript, but that isn't really a good solution..

Have you or anyone else found a fix for this?

Okay, I have fixed it by loading all the scenes only once in an autoloaded singleton script and then swapping to them when I need to;

SceneSwapper.gd:

var scenes = {
	"AnalysisStage": load("res://AnalysisStage.tscn"),
	"Game": load("res://game.tscn"),
	
}

func _ready():
#     This loads all menu scenes from a project directory
	var directory = Directory.new()
	if directory.open("res://MainMenuScreens/") == OK:
		directory.list_dir_begin()
		var file_name = directory.get_next()
		while file_name != "":
			if file_name == "." or file_name == ".." or file_name.begins_with("__"):
				file_name = directory.get_next()
				continue
			if file_name.ends_with(".tscn"):
				scenes[file_name.split(".tscn")[0]] = load("res://MainMenuScreens/" + file_name)
			
			file_name = directory.get_next()
	

#potentially unsafe
func get_scene(scene_name) -> PackedScene:
	return scenes[scene_name]

Scene Swapping:


func _on_MainMenu_button_down():
	get_tree().change_scene_to(SceneSwapper.get_scene("Main Menu"))

S0u1y avatar Nov 30 '24 02:11 S0u1y