Knight Steele
Knight Steele

Reputation: 365

List of structs in inspector Godot C#

In Godot, using C#, I'm trying to do something that I'd assume would be super simple, but I can't seem to get it to work the way I would like.

I have struct SnapPoint that simply stores a Vector3 and a string.

public struct SnapPoint
{
    public Vector3 Position;
    public string Name;

}

I then want to have an exported list of these SnapPoints that I can add to and remove from in the inspector. I've seen controls like this on other nodes, for example, the CSG nodes, so I assumed it would be possible.

I've tried a few approaches. Some either didn't work or didn't work the way I'd like.

My first thought was to just export a list of these structs, but that doesn't work. Using C# collections or arrays results in a build error (The type of exported property is not supported) while using godot collections results in a Varient type error. I've also tried adding [System.Serializable] above the struct, though that didn't do anything. I also tried changing from a struct to a class, and that didn't help either.

So, I looked into using resources instead. I created a SnapPoint resource and have an export for that resource type. This works...but means I have to create a new resource for every snap point I have and is a bit of a pain.

I looked into writing a custom plugin with a custom inspector and that approach looked like it could get me what I was looking for, but seemed overly verbose for something as simple as this. Perhaps it isn't simple, but it sure seems like it should be in my mind. I'm fine doing this, I just wanted to see if anyone knew of a better approach before I went down that path.

So, ultimately, is there an easy way to get the functionality I'm looking for or is this a limitation of Godot's inspector? Or am I missing something fundamentally? I come from Unity and within their inspector this task is trivial.

Upvotes: 0

Views: 563

Answers (1)

Theraot
Theraot

Reputation: 40295

As you found out, you are be able to export an array of a custom class that extends Resource.

Currently there is no way to make the inspector work with struct types.


An approach that have worked for me to ease working with arrays of a custom resource is to make the script run in the editor (i.e. with [Tool]) and have a setter defined for the exported property which replaces any nulls with a new instance. This way, when you add a new element to the array in the inspector the setter will create a new instance of the custom resource class to fill new the new slot in the array.

Upvotes: 1

Related Questions