Reading / Writing JSON?

Are there certain functions provided by radiant to handle JSON files? Where can I find them?

I’m trying to manipulate JSON files/object in JS, so getting the file is as easy as "$.get(‘directory’). Then, when I want to write that file, I assume I’ll have to go into lua through a call_handler and then call some radiant functions. Is this correct?

1 Like

I’m not following what you’re asking, so I apologize ahead of time. But to me, it seems like you’re going the long way around modding Stonehearth. Have you looked through the smod folder to what exactly the json files are? They’re pretty open read.

@Wharp we don’t have a general function that allows writing to JSON because it’s super dangerous! What if you accidentally overwrite some game files? Instead we have a datastore that you can instantiate inside any lua component called __saved_variables or inside any lua class called self._sv that will save data for you. From javascript to save data to such a datastore you want to make a call handler that will direct you to a lua file, and then make a function on that lua file that will save your data (and extract it when you need it).

As an example, check out how we place items in the world. Start with stonehearth_client.js, and look at the function_placeItemOrItemType. This function does a, which calls stonehearth:choose_place_item_location.
Choose_place_item_location is defined as a function endpoint in manifest.json, and it’s implement in place_item_call_handler.lua. From place_item_call_handler.lua, we can get to something like town.lua, which has a self._sv inside of it that you can use to save things.

We do have C++ functions that will write to JSON directly but they’re buried explicitly under user_settings and building_template code, so that you can’t accidentally mess with them. :slight_smile:


@sdee, there are some shenanigans with saved variables in saved games. It seems SVs added by overriding initialize() function of an existing class via radiant.mixin() in mod’s server script are not actually saved (I get an error log attempting to add field "field_name" to sv table when it is locked! when I load saved game and all entities have a default value of that SV instead of what they should have saved). This means SVs is a fully functional datastore only for new classes (sad, because I need to modify existing ones instead).

Hahahaha yes, a bit unclear maybe: Reading is indeed very easy, it’s more the writing part I was troubled by. :smile:

Ahh, this is perfectly clear, and valuable to know! :slightly_smiling_face: You’re the best @sdee. But darn it, why do you guys have to make it so hard for us modders to make spaghetti out of your beautiful architecture?! :laughing:

Oooo are you making sure to call __saved_variables:mark_changed()? :slight_smile: I think this is required to tell the C++ to record the changes.

Yes, I call it and I still get the error. For components monkeypatching doesn’t seem to do the trick, I won’t even try with services (many of them are initialised before there’s even a chance to patch them).