Some Notes on Godot Resources

Today’s blog is a really small one. I recently learned about a few Godot features that I only found by randomly stumbling across mentions of them buried in the documentation. People who know about these will find this relatively useless, but those who don’t know will find it a great relief.

The first one is gonna be the really simple one that I simply… didn’t notice in Godot’s UI. I feel extremely foolish for this, but better to spread the word in case anyone else made the same mistake I did.

Did you know that you can give names to built-in scripts? That they aren’t limited to being named [owner scene]:[uid]? Yeah when you’re making a built-in script that’s what the Name field is for. Thankfully you can also fill in a name after the fact by going to the node that the script is attached to and in the node’s editor panel clicking on the script resource to find the name field there. How did I miss this.

The other feature I found is far more substantially useful and so far I’ve only found it by going through the class reference list, I haven’t seen any mention in any of the actual tutorials on Resources.

If you are trying to make custom Resources that import and export to a specific format, the classes ResourceFormatLoader and ResourceFormatSaver are how you do it! The documentation for the two classes are here: ResourceFormatLoader and ResourceFormatSaver.

You don’t need to implement all of the methods for these classes thankfully, in fact some are better left alone. For my part, I was implementing a loader and saver for my dialogue tree format. For the loader I just needed to implement:

@tool
class_name PKSceneLoader
extends ResourceFormatLoader


func _get_recognized_extensions() -> PackedStringArray:
	return [ "pkscene" ]


func _recognize( resource: Resource ) -> bool:
	return resource is PKScene


func _handles_type( type: StringName ) -> bool:
	return type == "Resource"


func _get_resource_script_class( path: String ) -> String:
	return "PKScene" if path.get_extension() == "pkscene" else ""


func _get_resource_type( path: String ) -> String:
	return "Resource" if path.get_extension() == "pkscene" else ""


func _load( path: String, original_path: String, use_sub_threads: bool, cache_mode: int ) -> Variant:
	# ... load and return the PKScene Resource

and for the saver:

@tool
class_name PKSceneSaver
extends ResourceFormatSaver


func _get_recognized_extensions( _resource: Resource ) -> PackedStringArray:
	return [ "pkscene" ]


func _recognize( resource: Resource ) -> bool:
	return resource is PKScene


func _save( resource: Resource, path: String, flags: int ) -> Error:
	# ... return Error.OK if the PKScene Resource saved successfully

Making these tool scripts and giving them class names lets Godot use these savers and loaders automatically in the editor. Unfortunately it doesn’t make my custom resource files be included in the project export for some reason unless I add the file extension to the whitelist manually. Somewhat disappointing, but simply fixed.

If anyone is having trouble getting custom Resources working, I hope this gives you the lead you need!