Need help with exporting a custom ressource type.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By BE-Never Games
:warning: Old Version Published before Godot 3 was released.

Hello fellow Godot developers!

To explain my situation: I am currently working on a dialog system plugin for the engine.
I created a custom ‘character’ ressource, so you can create you own character, customize text speed, color, etc.
So now, I want to export a variable so that you can create a character ressource, just like you normally create ressources in Godot (you know, how for example if you want to create a material, you just go to the inspector to your node, create a material and then it immediatly creates and saves it in memory). I know this very simple to do with all of the built in ressources, because you just have to type “export(Ressource)” but how would you go about doing it with a custom one? Is it even possible?

Thanks for any answers in advance! :slight_smile:

:bust_in_silhouette: Reply From: Hinsbart

You already have part of the answer in your question :slight_smile:
You can use export(Resource) not only for the built-in resources, but also for custom ones.
The tricky thing is that your custom resource won’t be available in the list of resource types. So you can’t just click on the exported property and do New Character like you’d do with Animation, Material, etc.

But you can work around that pretty easily by saving it from a (temporary) script like this:

var res = preload("path/to/custom_resource.gd").new()
ResourceSaver.save("my_resource.tres", res)

You only have to do this once, after that you can just make copies the .tres.
Now you can load it in the inspector.
Also note that if you change the custom_resource.gd (e.g add a new export var), all the .res/tres files that are made from that script will be automatically updated as well. Very neat :wink:

Edit: Just saw that you’re creating an editor plugin. These have the ability to add custom types. So you might be able to do it without this workaround but I haven’t tried that yet.

Thank you for your answer! :slight_smile:
I actually already created a custom type for the character resource, and I can already create it from the resource menu at the top left of the inspector, I just do not know how to reference it for the export variable.
Same problem with your answer. It makes all sense to me, it is just that I do not know, how to call the saved resource. With built in resources, I can just pass that resource in, but I can not pass a custom type in.
It is probably a super simple solution and I am just not getting something here. :slight_smile:

BE-Never Games | 2016-09-20 16:54

Do note that this doesn’t work for subclasses of a gdscript. which makes sense but I wanted to point out…

extends Node

class MyResource:
    extends Resource
    var check = 0
	
func _ready():
    var res = MyResource.new()
    print(res.check) # prints 0 (yay)
    res.check = 1
    ResourceSaver.save("res://res.tres", res) # file doesn't get this check=1 property anywhere
    res = load("res://res.tres") # if you add it in manually, the check property doesn't get loaded onto the object.
    print(res.check) # errors because 'check' is an invalid index

E.g. the above shouldn’t work.
see Saving a Resource with a Script is not (de)serializing the script properties. · Issue #22518 · godotengine/godot · GitHub

Paul Gruenbacher | 2018-09-28 22:47

:bust_in_silhouette: Reply From: HarpyEagle

Like Hinsbart said you are right in that you need to useexport(Resource).

However you do not need to have to use ResourceSaver to create an initial resource file.

First you need a .gd script file that defines what your resource holds. For example,

#MyResource.gd
export(int) var some_data
export(String) var other_data

Then to use your resource, you will need to add an export(Resource) as mentioned before.

#SomeNodeScript.gd
export(Resource) var some_resource

In the inspector, select the <null> dropdown menu next to the export (in this example, it would appear as “Some Resource”) and select “New Resource”.

You should see a blank resource in the inspector, without any of your custom data fields yet. To add them, go to the script property and set it to your script, which in this example is “MyResource.gd”. The inspector won’t update right away, but if you refresh it by clicking on < and then >, your resource data will appear in the inspector.

You can then save this resource instance as .tres file to use elsewhere without having to select the script again.