The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates. | Twitter

+5 votes

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! :)

in Engine by (305 points)

2 Answers

+4 votes

You already have part of the answer in your question :)
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/").new()"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 (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 ;)

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.

by (922 points)

Thank you for your answer! :)
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. :)

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 =
    print(res.check) # prints 0 (yay)
    res.check = 1"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.

+7 votes

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,
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.
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 "". 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.

by (35 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 Frequently asked questions and 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 [email protected] with your username.