Status
Not open for further replies.

Sepheyer

Veteran
Veteran
Joined
Jul 20, 2021
Messages
57
Reaction score
28
First Language
English
Primarily Uses
RMMZ
Let's say my NPCs are an array of objects. This way I create a living world: when the player enters a map, the game checks which NPCs are currently in that map and spawns character-events to illustrate these NPCs.

But, the real data for all NPCs, including those that are active off the map, is in an array of objects.

How can I save this array? Let's assume the data is:

JavaScript:
//This is pseudocode, not meant to be compilable

function serialize(NPCs){
   //Is there an API to embed that my_NPCs array of 10000 objects into a save file?
}

class cls_NPC{
   constructor(){
       this.attributes = default_attr_gen_function()
   }
}

let my_NPCs = []
for(let x = 0; x < 10000; x++){
    my_NPCs.push(new cls_NPC())
}
serialize(my_NPCs)
 
Last edited:

caethyril

^_^
Global Mod
Joined
Feb 21, 2018
Messages
2,833
Reaction score
2,170
First Language
EN
Primarily Uses
RMMZ
Game data can be one of two types:
  • Static
    • Should not change mid-playthrough.
    • Initialised once per session, on game boot (e.g. Scene_Boot#start).
    • Not included in save files.
    Core script example: $dataActors (database actor data).

  • Dynamic
    • May change mid-playthrough.
    • Initialised on new/load game (and game boot).
    • May be included in save files. For example, the core scripts do not save $gameTemp, $gameMessage, or $gameTroop.
    Core script example: $gameActors (current actor data).

To include (dynamic) data in save files, there are 3 relevant stages to consider:
  1. Initialisation: hook into DataManager.createGameObjects.
    This method runs on new/load game, and on game boot (after the database has loaded).

  2. Add to save data: hook into DataManager.makeSaveContents.
    This method returns an object to be stringified and saved to file.

  3. Load from saved data: hook into DataManager.extractSaveContents.
    This method accepts one argument: an object, the un-stringified save data.
    It applies after initialising the data through createGameObjects.
Template example (ES5 syntax):
JavaScript:
(function(alias) {
  DataManager.createGameObjects = function() {
    alias.apply(this, arguments);
    console.log("initialise data");
    // window.myData = { a: 0, b: 0 };   // example
  };
})(DataManager.createGameObjects);

(function(alias) {
  DataManager.makeSaveContents = function() {
    var contents = alias.apply(this, arguments);
    console.log("add save data");
    // contents.myData = window.myData;   // example
    return contents;
  };
})(DataManager.makeSaveContents);

(function(alias) {
  DataManager.extractSaveContents = function(contents) {
    alias.apply(this, arguments);
    console.log("extract save data");
    // if (contents.myData) window.myData = contents.myData;   // example
  };
})(DataManager.extractSaveContents);
(I'm not sure what the corresponding class sugar is for this.)

Alternatively, you can simply attach your data to an object that is already included in save files, e.g. $gameSystem. This is a bit more risky in terms of compatibility with other plugins, but much simpler to do! :kaoblush:

Also, some notes on the save format (stringification):
  • Avoid saving irrelevant data: JsonEx.stringify(new Array(10000)) yields a ~50 KB string, because all the empty entries get logged as null.

  • Certain data, e.g. Function objects or circular references, will be discarded unless you write some code to handle those cases. (Later versions of MV have a safeguard against circular references which has been dropped in MZ.) More details here:

  • Under current specifications, it looks like strings have maximum length ~1 GB, which shouldn't be a problem in this situation:
    MZ zips its save data after stringification, so the final save file size may be smaller in that case.

I think this all applies to MV as well, except where explicitly noted. :kaohi:
 

slimmmeiske2

Little Red Riding Hood
Global Mod
Joined
Sep 6, 2012
Messages
9,003
Reaction score
5,892
First Language
Dutch
Primarily Uses
RMXP

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.

Latest Threads

Latest Profile Posts

Maldra wrote on stramin's profile.
Hi regarding the 32 bit -> 64 bit
I am running on a 64 bit windows OS and tried doing this conversion. I converted all of the .exe files that I could find in the KADOKAWA\RPGMV folder using the 4gbpatch.exe file. However, after trying to access RPG Maker MV after this conversion, it simply will not load. Do you have any advice or any ideas?
****ing muscle memory: does anyone else habitually quit out of test play when they notice something they """HAVE""" to fix, in spite of the fact that you can now edit DURING test play?

...thousands of times...

...every day?
Does anyone recognize him? :kaocry:
pwkyiDa.jpg

And hi.
finally drew 3 of the enemies that appear as students from the high school that's in my game.
Finnuval wrote on ShuBun-Kin's profile.
Loving the avatar :D

Forum statistics

Threads
115,928
Messages
1,094,130
Members
151,187
Latest member
NaughtyPoncho
Top