• Godot HelpProgramming
  • Inheriting from Resource class to create a new usable Resource object (C# Scriptable Object)

Hello,

Godot Engine v3.1.beta1.mono.official MacOS

In gdScript if I have a node and attach a script that includes a small code block extends Resource class_name SuperObject export(string)var name=bestDayIs

Save both the node and script as SuperObject

I can then right click in the file system -> new resource and quick search SuperObject.

How do I accomplish this in C#? I've tried inheriting Resource, Reference Object attaching to various nodes or no node. example of a test

`using Godot; using System;

public class SuperObj2 : Resource { [Export] public string State; }`

Is there an attribute that is required? Something to tell the editor to allow this class to be usable as a resource?

EDIT

Based on this article: https://docs.godotengine.org/en/3.0/getting_started/scripting/c_sharp/c_sharp_basics.html It states, "Writing editor plugins and tool scripts in C# is not yet supported". Being new to Godot I am not certain of the terminology. This might be what I'm trying to accomplish and can't yet be done.

2 years later

Sorry for ressurrecting this post. But since it appears on Google's first page, I want to help future folks with the same issue.

Creating custom resources in C# is still not supported but there is a workaround: Create your scriptable object as usual, class SuperObj: Resource.

Now, when creating your resource, just create a base Resource file and attach that script to it.

To load the ScriptableObject, normally you could load it with GD.Load() and cast it to your class: PlayerData data = GD.Load("res://Data/player.tres") as PlayerData;

But for some reason, Godot can't make that cast. Instead, you can use Resource.Get() to access your properties with a string as parameter:

var data = GD.Load("res://Data/player.tres");
GD.Print(data.Get("hp").ToString());

That's a workaround, I don't know if this is the best approach, but it is the best I could find. :)~~

@castroclucas said: Sorry for ressurrecting this post.

This forum, as of now at least, does not have any rule against this so no worries.

@castroclucas said: But since it appears on Google's first page, I want to help future folks with the same issue.

And this is always appreciated. Also removes the topic from the list of unanswered questions. ;)

2 years later