[Help] How to write to json files (data folder) ?

Status
Not open for further replies.

lordvalinar

Lord of the Damned
Veteran
Joined
Mar 31, 2013
Messages
176
Reaction score
31
First Language
English
Primarily Uses
== CONTEXT ==
I am creating a Grid Based Tactical System on the map and I have setup some scripts to write enemy data to put into Game_Troops; however, I do not know how to write it (overwrite) the Troops.json file.

I am not asking for people to do the plugin for me (just wanted to clear that up), I am merely asking what is the code to save the data to the Troops.json file?

(( I understand possibly using JSON.stringify($gameTroop) or JsonEx.stringify($gameTroop) perhaps? Or something with stringify in general )) But after that, I don't know if there's some HTTP or the DeepCopy functions are required, and so on. A little help would be appreciated.

P.S. I also apologize but I didn't know if this goes in here, or in plugin development since I am asking a little help on JS itself - even if the end goal is for the plugin.
 

ozubon

surströmming?
Veteran
Joined
Oct 16, 2018
Messages
178
Reaction score
165
First Language
English
Primarily Uses
RMMV
I use this method to write and overwrite json, hope it helps:
 

lordvalinar

Lord of the Damned
Veteran
Joined
Mar 31, 2013
Messages
176
Reaction score
31
First Language
English
Primarily Uses
EDIT: Alright tried it and it's a little finnicky... to use this I need to now find a way to get the game's current directory (or else I have to write it in all manually - and that would not be good for other users).

Currently looking in rpg_managers.js to see how they save/retrieve the data\ json files and see if I can get their methods for the directories. Still thank you for this huge help though :)
 
Last edited:

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
39,141
Reaction score
12,416
First Language
English
Primarily Uses
RMMV
I wouldn't write to the Troops.json file at all. That's MV's file, and the moment you do anything in the editor and save, all your changes will be gone. I would create a completely separate file.
 

lordvalinar

Lord of the Damned
Veteran
Joined
Mar 31, 2013
Messages
176
Reaction score
31
First Language
English
Primarily Uses
Well I won't be using the Troops tab in the database at all. When I have everything setup and running, it will be easy to setup enemies (they're only ever added to an existing Troop (troopId = mapId); think like the PSP game D&D Tactics, or Divinity: Original Sin, etc).

So overwriting Troops won't be a problem. Also problem solved, made a function that now saves to the Data/ folder:

JavaScript:
StorageManager.saveDataFile = function(src, json) {
    if (this.isLocalMode()) {
        var data = JSON.stringify(json);
        var fs = require('fs');
        var path = require('path');
        var base = path.dirname(process.mainModule.filename);
        //
        var dirPath = path.join(base, 'data/');
        var filePath = dirPath + src + ".json";
        if (!fs.existsSync(dirPath)) {
            fs.mkdirSync(dirPath);
        }
        fs.writeFileSync(filePath, data);
        console.log("saveDataFile:\n"+data); // DEBUG
    } else {
        // This section isn't complete
        console.log("saveDataFile: Not local");
    }
};
So above example would use:
var troop = {}; // construct the data
StorageManager.saveDataFile('Troops', troop);

If anyone wants the function by itself to construct enemy data (based on event, not battler) I can post that too. (Or wait until I finish the plugin lol)
 

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
39,141
Reaction score
12,416
First Language
English
Primarily Uses
RMMV
The entire database is saved every time you make changes, whether you change anything in that tab or not. So even though you're not using the Troops tab, your file will be overwritten.

If you're determining this as the game is played, why do you need to save the data at all?
 

lordvalinar

Lord of the Damned
Veteran
Joined
Mar 31, 2013
Messages
176
Reaction score
31
First Language
English
Primarily Uses
Oh! Now I see what you mean..

And I'm trying something out as i'm experimenting and trying to create this battle system. I'm trying to convert the normal battle system to on the map (I know other people have done this already, but I'm trying to make my own; hope to improve it, etc etc).

So I figure there's at least a few things that need to be changed:

- BattleManager
- Game_Map
- Game_Troop
- Scene_Battle

Possibly more... still in early development. Since the battle scene is no longer done in its own battle screen, I'm trying to process the events on the map that trigger (currently with Yanfly's proximity activate plugin) that will setup/add the enemy to a singular troop array (troopId = mapId).

Since currently Game_Troops is still using its functions like ".troop()" that gets $dataTroops[this._troopId], I figure I can mess with less overwrites as possible by just overwriting the Troops.json file.

Now the edits will happen -ingame- including cleanup, so I'm not worried about any edits in the engine overwriting it. It happens real-time. So that's why I save the data. However that being said, I think I see your point. I could just localize the data in the script without having to save a json file at all... since it would merely be cleaned out at the end of battle anyways.

EDIT: Any conditionals could be handled by the event's interpreter instead of the troop's as well
 

MushroomCake28

KAMO Studio
Global Mod
Joined
Nov 18, 2015
Messages
2,921
Reaction score
3,951
First Language
English
Primarily Uses
RMMV

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

 
Status
Not open for further replies.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

Please do not buy games from g2a.com. The sellers have total power over you. The website has many scam sellers. It looks cheap, but the key code didn't work.
A while back I was having fun making my own custom tiles, then I hit carpet... why is it so difficult!? Lol.
Weekend is finally here... what would you like to see (?):
1) Pokemon into zelda minish-cap style
or
2) Pokemon into zelda gbc style
or
3) Zelda into pokemon style

#= Choose ===============#
||__[ Full mockup ]_[ Charset ]__||
#=======================#
I know someday I'll start working on this...

I'm so happy that I got Steam achievements to work in my game, thanks in large part to the posts by other RPG Maker devs on this issue!

Forum statistics

Threads
97,938
Messages
948,038
Members
129,186
Latest member
marcelobem
Top