+1 vote

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

detachmentleaderimporter.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?

Godot version v3.3.1
in Engine by (102 points)

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

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

1 Answer

+1 vote
Best answer

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

by (102 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.