I’m using a lot of resources in my project so my fales became really confusing very fast. So I want to have some resources with a custom extension. Researching about this I ended up creating those custom ResourceFormatSaver and ResourceFormatLoader:
extends ResourceFormatSaver
class_name DialogueFormatSaver
func _get_recognized_extensions(_resource: Resource) -> PackedStringArray:
return PackedStringArray(["dialogue"])
func _recognize(resource: Resource) -> bool:
return resource is Dialogue
func _save(resource: Resource, path: String, _flags: int) -> int:
if not (resource is Dialogue):
return ERR_INVALID_DATA
var file = FileAccess.open(path, FileAccess.WRITE)
if file:
var result := var_to_str(inst_to_dict(resource))
file.store_string(result)
return OK
return ERR_CANT_OPEN
This would be my ResourceFormatLoader:
extends ResourceFormatLoader
class_name DialogueFormatLoader
func _get_recognized_extensions() -> PackedStringArray:
return PackedStringArray(["dialogue"])
func _get_resource_type(path: String) -> String:
var extension = path.get_extension().to_lower()
if extension == "dialogue":
return "Resource"
return ""
func _handles_type(type: StringName) -> bool:
return ClassDB.is_parent_class(type, "Resource")
func _load(path: String, _original_path: String, _use_sub_threads: bool, _cache_mode: int) -> Variant:
if ResourceLoader.exists(path):
var file = FileAccess.open(path, FileAccess.READ)
return dict_to_inst(str_to_var(file.get_as_text()))
else:
push_error("File does not exists")
return null
This method “works” but it turns my resource into a string dictionary, which makes it lose some functionality like opening in the inspector, appearing in the FileSystem or the possibility to drag and drop in an exportedvariable of my Resource’s type (Dialogue).
So I’m wondering if there is a way to use the same _save() logic as the one being used by Godot for Resource files. Same thing with _load(). I don’t mind if they are identical since all I want is a regular resource file but with my .dialogue extension.
Is this possible to do without modifying the engine? Thanks beforehand!