How to import a custom resource?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By darkshadow

Hi all, I am trying to make a custom resource and importer for a binary file, which is a list of strings (starting with something simple)

the following three files I have in an addons folder called detachment_leader_importer:

DetachmentLeaders.gd

tool
extends Resource
class_name DetachmentLeaders
export(Array, String) var detachment_leaders

# Make sure that every parameter has a default value.
# Otherwise, there will be problems with creating and editing
# your resource via the inspector.
func _init(p_detachment_leaders = []):
	detachment_leaders = p_detachment_leaders

detachment_leader_importer.gd

tool
extends EditorPlugin

var import_plugin: EditorImportPlugin

func _enter_tree():
	import_plugin = preload("import_plugin.gd").new()
	add_import_plugin(import_plugin)


func _exit_tree():
	remove_import_plugin(import_plugin)
	import_plugin = null

import_plugin.gd

tool
extends EditorImportPlugin

const DetachmentLeaders = preload("DetachmentLeaders.gd")

func get_importer_name():
	return "fl40k.detachment_leaders"

func get_visible_name():
	return "Detachment Leaders"

func get_recognized_extensions():
	return ["DAT", "dat"]

func get_save_extension():
	return "res"

func get_resource_type():
	return "DetachmentLeaders"

func get_preset_count():
	return 1

func get_preset_name(preset):
	return "Default"

func get_import_options(preset):
	return []

func get_option_visibility(option, options):
	return true

func import(source_file, save_path, options, r_platform_variants, r_gen_files):
	var file = File.new()
	var err = file.open(source_file, File.READ)
	if err != OK:
		return err

	var leader_count = file.get_32()
	var names = []
	for i in range(leader_count):
		var nation: int = file.get_32()
		var name: String = file.get_buffer(24).get_string_from_utf8()
		names.append(name)
		print(name)
	if file.get_position() != file.get_len():
		return ERR_PARSE_ERROR
	file.close()
	var leaders: Resource = DetachmentLeaders.new(names)
	return ResourceSaver.save("%s.%s" % [save_path, get_save_extension()], leaders)

I’ve extended without issue EditorImportPlugin as the ResourceSaver.save is returning zero. Every time I hit the “reimport” resource button that load function seems to run.

However when I click on the file in the FileInspector instead of resource appearing in the inspector I get the following error in the console:

 Cannot get class 'DetachmentLeaders'.
 Cannot get class 'DetachmentLeaders'.
 Cannot get class 'DetachmentLeaders'.
 Cannot get class 'DetachmentLeaders'.
 Cannot get class 'DetachmentLeaders'.
 Cannot get class 'DetachmentLeaders'.
 Cannot get class 'DetachmentLeaders'.
 Cannot get class 'DetachmentLeaders'.
 Cannot get class 'DetachmentLeaders'.
 Cannot get class 'DetachmentLeaders'.
 Cannot get class 'DetachmentLeaders'.
 Cannot get class 'DetachmentLeaders'.
 Cannot get class 'DetachmentLeaders'.
 Cannot get class 'DetachmentLeaders'.
 Cannot get class 'DetachmentLeaders'.
 Cannot get class 'DetachmentLeaders'.
 Cannot get class 'DetachmentLeaders'.
 Cannot get class 'DetachmentLeaders'.
 Cannot get class 'DetachmentLeaders'.
 Cannot get class 'DetachmentLeaders'.
 Cannot get class 'DetachmentLeaders'.
 Cannot get class 'DetachmentLeaders'.
 Cannot get class 'DetachmentLeaders'.
 Cannot get class 'DetachmentLeaders'.
 Cannot get class 'DetachmentLeaders'.
 Cannot get class 'DetachmentLeaders'.
 Cannot get class 'DetachmentLeaders'.
 Cannot get class 'DetachmentLeaders'.
 Cannot get class 'DetachmentLeaders'.
 Cannot get class 'DetachmentLeaders'.
 Cannot get class 'DetachmentLeaders'.
 No loader found for resource: res://.import/DTCHLDR.DAT-a713435bfd0cd67315a3563829e34e23.res.
 Failed loading resource: res://resources_no_commit/DTCHLDR.DAT. Make sure resources have been imported by opening the project in the editor at least once.
 editor/editor_node.cpp:947 - Condition "!res.is_valid()" is true. Returned: ERR_CANT_OPEN

Does DetachmentLeaders have to have a function to explain how the inspector should display? Why does the loader say it fails when the load() function works?

Try adding the custom type in the editor plugin, not only declaring the global class in the script

chotto | 2021-05-27 04:20

How would I do that? Do you mean put the .DAT file into the addon folder as well?

darkshadow | 2021-05-27 16:44

With this in your plugin script:
EditorPlugin — Documentación de Godot Engine (4.x) en español

chotto | 2021-05-27 18:15

:bust_in_silhouette: Reply From: darkshadow

I think I got it working by changing the resource type to the generic Resource rather than my custom type:

func get_resource_type():
return "Resource"

It seems like here and elsewhere with type hinting, godot doesn’t recognise custom class names and it has to be one of the built-in resource types