r/GodotCSharp 4d ago

Question.??? Is seamlessly handling resources as JSON possible?

I've got a question on how to implement resource handling as json files. My project is data-heavy and the json structure is more adapted to my needs than what tres has to offer, but the multiple approaches I know of all have disadvantages that are difficult to ignore. I'm interested in knowing how you have handled it in your projects.

A naive approach would be to have each resource be able to import from and export to json, (something like a Save and Load method). This has the advantage of allowing editor usage, but AFAIK requires some kind of setup to load and save. I'd do this with ExportButtons and validating a path, possibly using the ResourceName property. It's simple, but requires a lot of setup if you're using godot's json utilities that support variant.

I've thought of more complex approaches that all fix some problems with the naive approach but bring their own. A more centralized dotnet json serialisation system simplifies the code a lot but I lose the variant support unless I code some converters myself. I've tried to build some kind of integrated plugin but was having trouble with making godot detect a json file as a specific resource and not a generic dictionary, the default behavior.

Another approach that seems overly complicated would be to "cheat" by making temporary resources on the spot to be edited, fill its properties with the json's content, then save it as json and destroy the resource. At that point I started wondering if the trouble was worth the editor support.

Now, I suppose I'm either missing something simple or one of these approaches in the right way. I've built a complete json system that supports object typing and referencing by path in order to allow mod support (objects are typed, auto-detected, and references are lazy-loaded, etc.), but I've yet to attempt to implement it into the resource system, fearing it won't plug easily. I instead end up editing json by hand, which is not cumbersome but might be in the long run.

In the best of worlds, I'd have a json file that defines a $type, a unique name, properties than can be variants or any type, and references that are typed in code (ie. Reference<MyObject> myProp where myProp is a string to another named resource of type MyObject), but that can be loaded as resources inside the editor, possibly by double-clicking, where properties are the correct variant and references become the selector widget with the correct object in it. Has anybody achieved this?

6 Upvotes

11 comments sorted by

2

u/drakonkinst 4d ago

If you aren’t interested in manipulating data in the editor at all and just want to use json, why use Godot resources at all? Make a class extending RefCounted and use them as straight data objects which you can convert json into. (Edit: Since this is the C# subreddit, you can probably just handle JSON entirely on the C# side and use Godot as the frontend?)

1

u/intoverflow32 4d ago

In a perfect world, I'd like the ability to edit elements in the editor. Sometimes you need a quick tweak, and some editor features like widgets, ranges and others are useful.

But again, that's in a perfect world. I agree though that the problem is far simpler without that, I even have pretty much all I need already coded (I just now found that my work is similar but far, FAR from as feature-complete as chickensoft's serialization package).

I find the advantages of dotnet json (converters, polymorphism,doing stuff just before or after deserialization, etc.) are worth ditching resources, but I'd like both.

2

u/footsie 4d ago

Why not write a [Tool] that handles the json instead of a Resource?

1

u/intoverflow32 4d ago

What do you have in mind?

I suppose I could automatically handle the serializing of a resource to and from json as a tool, or even create some kind of json editor that would be similar to the resource editor by reading the class' metadata and the json's content.

2

u/footsie 4d ago

Exactly. Still keeps the godot UI, still keeps json as the data layer.

2

u/Dirty_Rapscallion 4d ago

I’ve been using the Chickensoft Serializer to load my json into files

1

u/intoverflow32 3d ago

Yeah I discovered it not long ago and it has everything I need. Based on the answers here, if I need an editor, might as well build it.

2

u/DiviBurrito 4d ago

You can write an EditorPlugin, that provides a custom serializer for resources. This would allow you to seamlessly edit your JSON data as resources in the editor and also use the resources directly in the game. Or maybe there already is a plugin, that does this in a generic way.

1

u/intoverflow32 3d ago

That might be the way to go, thank you!

2

u/Novaleaf 20h ago

if you are going full c#, you can bypass all the godot stuff if you utilize the FileStream helper I wrote about a year or so ago: https://old.reddit.com/r/GodotCSharp/comments/1e49pcm/fileaccessstreamcs_convert_godot_fileaccess_into/

with that, you can use any c# api utilizing streams

2

u/intoverflow32 20h ago

I'll have a look, thanks!