MV Creating a Journal

I'm very new to Javascript and object-oriented programming, so forgive me if I sound really dumb.

I'm trying to create a journal to which players can add their own journal entries. The journal will contain the following information:
  1. Journal Entry Date (Example: 01/01/2023)
  2. States Affecting the Character at the Time (Example: Poisoned)
  3. Tasks Accomplished
Right now, I'm just trying to figure out the first part. To do this, I created a JSON file that is called JournalEntry.json. I created a new database file in the Data Manager to reference it. The Data in the JSON file looks like this.

Spoiler: JournalEntry.json
[
null,
{"id":1,"entryDate":"01/01/2023"},
{"id":2,"entryDate":"01/09/2023"},
{"id":3,"entryDate":"01/10/2023"}
]

I then created the following script.

Spoiler: Script
var jsonJournalEntry = $dataJournalEntry

jsonJournalEntry.push({"id": 4, "entryDate": "01/13/2023"});

Now this is working... sorta. If the game is running, it adds the entry and I can even get the NPCs to repeat the date back. However, when I close the game, the data resets and the new entry disappears.

The other thing I'm realizing as I work on this is that I need the data to save dynamically to the game and not to the primary JSON file. I thought maybe the DoubleX Dynamic Data plugin might help. However, I put in the following command and it isn't adding the new journal entry date the way I thought it would.

Spoiler: Dynamic Data
$gameSystem.setDynamicData("$dataJournalEntry", $dataJournalEntry[4])

So yeah. I feel like I'm moving in the right direction. However, I'm so new to all of this, that I feel like I'm swimming through pudding trying to figure it out.
to save the entry to the save file, you need to create the object,
if those are missing, it wont be saved when you close the game
or even save the game.

there are some plugins to add this, which I also use in my ULS

JavaScript:
    ShadowDragon.ULS.DM_mSC = DataManager.makeSaveContents;
        DataManager.makeSaveContents = function() {
            var contents = ShadowDragon.ULS.DM_mSC.apply(this, arguments);
                contents.ULSystem = $gameULSystem;
                contents.ulsData = $dataULSystem;
                return contents;
        };

    ShadowDragon.ULS.DM_eSC = DataManager.extractSaveContents;
        DataManager.extractSaveContents = function(contents) {
            ShadowDragon.ULS.DM_eSC.apply(this, arguments);
                $gameULSystem = contents.ULSystem;
                $dataULSystem = contents.ulsData;
        };

while those is from my ULS system you need to modify it to fit yours in order
to save the json file in a save file.
This is the object I created. I'm still trying to puzzle my way through your response. Like I said, I'm still very new to this and object-oriented programming is really hard for me to grasp.

Spoiler: Journal Object
//-----------------------------------------------------------------------------
// Game_JournalEntry
//
// The game object class for a journal entry.

function Game_JournalEntry() {
this.initialize.apply(this, arguments)
}

Game_JournalEntry.prototype.initialize = function () {
this.clear();
};

Game_JournalEntry.prototype.clear = function () {
this.data = [];
};

Game_JournalEntry.prototype.setValue = function (journalEntryID, value) {
if (journalEntryID > 0 && journalEntryID < $dataJournalEntry.journalEntry.length) {
this.data[journalEntryID] = value;
this.onChange();
}
};

Game_JournalEntry.prototype.onChange = function () {
$gameMap.requestRefresh();
}
This is the object I created. I'm still trying to puzzle my way through your response. Like I said, I'm still very new to this and object-oriented programming is really hard for me to grasp.

Spoiler: Journal Object
//-----------------------------------------------------------------------------
// Game_JournalEntry
//
// The game object class for a journal entry.

function Game_JournalEntry() {
this.initialize.apply(this, arguments)
}

Game_JournalEntry.prototype.initialize = function () {
this.clear();
};

Game_JournalEntry.prototype.clear = function () {
this.data = [];
};

Game_JournalEntry.prototype.setValue = function (journalEntryID, value) {
if (journalEntryID > 0 && journalEntryID < $dataJournalEntry.journalEntry.length) {
this.data[journalEntryID] = value;
this.onChange();
}
};

Game_JournalEntry.prototype.onChange = function () {
$gameMap.requestRefresh();
}

Basically, in the default system custom-created objects aren't saved as part of a save game. You have two paths to solving this:

1. Create your custom objects as part of Game_System, which gets saved along with all of its properties in the default engine.

2. Alias the default functions that create save game objects and add your custom one to them.
I'm very new to Javascript and object-oriented programming, so forgive me if I sound really dumb.

I'm trying to create a journal to which players can add their own journal entries. The journal will contain the following information:
  1. Journal Entry Date (Example: 01/01/2023)
  2. States Affecting the Character at the Time (Example: Poisoned)
  3. Tasks Accomplished
Right now, I'm just trying to figure out the first part. To do this, I created a JSON file that is called JournalEntry.json. I created a new database file in the Data Manager to reference it. The Data in the JSON file looks like this.

Spoiler: JournalEntry.json
[
null,
{"id":1,"entryDate":"01/01/2023"},
{"id":2,"entryDate":"01/09/2023"},
{"id":3,"entryDate":"01/10/2023"}
]

I then created the following script.

Spoiler: Script
var jsonJournalEntry = $dataJournalEntry

jsonJournalEntry.push({"id": 4, "entryDate": "01/13/2023"});

Now this is working... sorta. If the game is running, it adds the entry and I can even get the NPCs to repeat the date back. However, when I close the game, the data resets and the new entry disappears.

The other thing I'm realizing as I work on this is that I need the data to save dynamically to the game and not to the primary JSON file. I thought maybe the DoubleX Dynamic Data plugin might help. However, I put in the following command and it isn't adding the new journal entry date the way I thought it would.

Spoiler: Dynamic Data
$gameSystem.setDynamicData("$dataJournalEntry", $dataJournalEntry[4])

So yeah. I feel like I'm moving in the right direction. However, I'm so new to all of this, that I feel like I'm swimming through pudding trying to figure it out.
Handling data outside of the JS code is done with nwjs which is the node webkit RPGM uses to access the folders & data. Doing so requires a bit more advanced coding, though.

You'll have an easier time just handling all those things inside your game by having the data stored in your savegame.
The easiest way to do so uses my Plugin for Plugin Developers in my signature. (It's free)

With it, you can use one single line to register an object to be transferred via savegame.

Check out this example:
1673692212263.png

It does what you're doing (creating a blueprint) but uses class syntax instead of the cumbersome prototype syntax.
And then I simply use one line to register it via my Plugin's SaveManager and set the initialization to true.

This not only ensures that the entire object is transferred across savegames, it is also correctly reinitialized upon launching a game.
(If this isn't done, you could play the game, get a dozen journal entries, exit into the main menu, start a new game - and still have the dozen journal entries from the last savegame)

My plugin does all of that for you in a single line.
Handling data outside of the JS code is done with nwjs which is the node webkit RPGM uses to access the folders & data. Doing so requires a bit more advanced coding, though.

You'll have an easier time just handling all those things inside your game by having the data stored in your savegame.
The easiest way to do so uses my Plugin for Plugin Developers in my signature. (It's free)

With it, you can use one single line to register an object to be transferred via savegame.

Check out this example:
View attachment 250671
It does what you're doing (creating a blueprint) but uses class syntax instead of the cumbersome prototype syntax.
And then I simply use one line to register it via my Plugin's SaveManager and set the initialization to true.

This not only ensures that the entire object is transferred across savegames, it is also correctly reinitialized upon launching a game.
(If this isn't done, you could play the game, get a dozen journal entries, exit into the main menu, start a new game - and still have the dozen journal entries from the last savegame)

My plugin does all of that for you in a single line.
I feel like I'm so close to understanding what you're saying, but I'm just like... not. Let me try to put it into basic English and hopefully you can tell me if I'm on the right track.

constructor(): This is basically like turning a key in a car. It's starting the class up.

initialize(): If I do nothing else, the text of the new object created by this class will just say, "Default Text."

setText(text): This is the bread and butter of the thing. It's basically saying whatever the user says to set the text to, that's what the text will be.

getText(): This will display the text of the object that was set using the setText(text) command.

So if I wanted to use this in the game to create a date, I'm guessing it would look something like this:

PluginExample_SavegameManager.setText("01/28/2023")

And then to get one of the NPCs in the game to repeat it back, I would say something like this:

PluginExample_SavegameManager.getText()

So here's what I'm not really understanding. A class is basically like a collection of objects, in this case journal entries. How do I call up the object for January 28th that I just created?
I feel like I'm so close to understanding what you're saying, but I'm just like... not. Let me try to put it into basic English and hopefully you can tell me if I'm on the right track.

constructor(): This is basically like turning a key in a car. It's starting the class up.

initialize(): If I do nothing else, the text of the new object created by this class will just say, "Default Text."

setText(text): This is the bread and butter of the thing. It's basically saying whatever the user says to set the text to, that's what the text will be.

getText(): This will display the text of the object that was set using the setText(text) command.

So if I wanted to use this in the game to create a date, I'm guessing it would look something like this:

PluginExample_SavegameManager.setText("01/28/2023")

And then to get one of the NPCs in the game to repeat it back, I would say something like this:

PluginExample_SavegameManager.getText()

So here's what I'm not really understanding. A class is basically like a collection of objects, in this case journal entries. How do I call up the object for January 28th that I just created?
The plugin example I gave was an example of how you could store an object in savegames with just one line using my free Plugin for Plugin Developers.

The class that I made is just a template, you could design it any way you want.

e.g.
JavaScript:
class MyJournalExampleTemplate {
    // the constructor is automatically called when an object using this class is created
    constructor() {
        this._journalEntries = [""]; // first entry is set to be empty, so entry 1 has id 1
    }
    
    addEntry(date, text, states) {
        this._journalEntries.push({date, text, states})
    }
    
    getDate(id) {
        return this._journalEntries[id].date;
    }
    
    getText(id) {
        return this._journalEntries[id].text;
    }
    
    getStates(id) {
        return this._journalEntries[id].states;
    }
    
    findEntryIndex(date) {
        return this._journalEntries.findIndex((entry) => entry.date === date)
    }   
}
SaveManager.register("MyJournalExample", MyJournalExampleTemplate, true)

This is more like what you'd be using. In this example, we create a class that'll act as your journal. You can add entries which stores the date, text and states, and you can fetch data from an entry if you know the entry id. (e.g. 1 for the first entry)

If you don't know the entry id but you know the date, you can just use findEntryIndex() to get the id, and then use the other methods.

This last line:
JavaScript:
SaveManager.register("MyJournalExample", MyJournalExampleTemplate, true)
Requires my Plugin for Plugin Devs to work. It handles all the savegame stuff for you. It'll create an object called "MyJournalExample" based on the MyJournalExampleTemplate when a new game is launched, it'll automatically save it when the player saves the game and loads it when the player loads the game.

You can use an object like this to easily manage journal entries however you wish.

E.g.
JavaScript:
// adding an entry
MyJournalExample.addEntry("28.01.2023", "Today, I learned how to create classes!", ["confused"]);

JavaScript:
// finding an entry based on date
const id = MyJournalExample.findEntryIndex("28.01.2023")

// using the id to get the text and state
console.log(MyJournalExample.getText(id), MyJournalExample.getStates(id))

Returns this:
1674935575793.png

Original thread: https://forums.rpgmakerweb.com/threads/creating-a-journal.154073/