RMMZ Default Variable Values with a Script (RMMZ)

Javo

Veteran
Veteran
Joined
Jun 25, 2020
Messages
33
Reaction score
4
First Language
Spanish
Primarily Uses
RMMZ
Greetings:

Time ago, I found at the forums the following Script which allows (at MV), to configure the default values for variables:

Code:
(function() {
    var alias_DataManager_setupNewGame = DataManager.setupNewGame;
    DataManager.setupNewGame = function() {
       alias_DataManager_setupNewGame.call(this);
       $gameVariables.setValue(ID, VALUE);
       $gameVariables.setValue(ID, VALUE);
       //and so on...
    };
})();
(source: https://forums.rpgmakerweb.com/index.php?threads/default-variable-values.93205/ )

As you can imagine, I've tried this at MZ and it just not work; could anyone be so kind to help me to "translate" this to MZ?
Thanks a lot in advanced for your will to help :)
 
Last edited:

Andar

Veteran
Veteran
Joined
Mar 5, 2013
Messages
30,958
Reaction score
7,465
First Language
German
Primarily Uses
RMMV
That code will work nowhere because it contains placeholders.
Can you give us the real code that you tried to run so that we know if you made a mistake in replacing the placeholder values with real values? Because at first guess it should have worked.
 

Javo

Veteran
Veteran
Joined
Jun 25, 2020
Messages
33
Reaction score
4
First Language
Spanish
Primarily Uses
RMMZ
Greetings @Andar
Thanks for the reply..

The actual code I'm using as Script (saved as .js), is:

Code:
(function() {
    var alias_DataManager_setupNewGame = DataManager.setupNewGame;
    DataManager.setupNewGame = function() {
       alias_DataManager_setupNewGame.call(this);
       $gameVariables.setValue(0001, 0); // minutes_initialization
       $gameVariables.setValue(0002, 9); // full_sunlight (9:00 a.m.)
       $gameVariables.setValue(0003, 89); // start day: march_21st
    };
})();
 

Andar

Veteran
Veteran
Joined
Mar 5, 2013
Messages
30,958
Reaction score
7,465
First Language
German
Primarily Uses
RMMV
yes, that is wrong - NEVER use leading zeroes in programming numbers.

The IDs are 1, 2, 3 - they are NOT 0001 etc as only the editor uses that format for ID displays.

and all variables are automatically set to zero, so the first line is not needed.

If those corrections don't work, then you'll have to wait for someone with RMMZ-experience to check the code.
 

Javo

Veteran
Veteran
Joined
Jun 25, 2020
Messages
33
Reaction score
4
First Language
Spanish
Primarily Uses
RMMZ
I've done the corrections and, unfortunately, the fix didn't work. Here it goes the actual code:

Code:
(function() {
    var alias_DataManager_setupNewGame = DataManager.setupNewGame;
    DataManager.setupNewGame = function() {
       alias_DataManager_setupNewGame.call(this);
       $gameVariables.setValue(1, 0); // minutes_initialization
       $gameVariables.setValue(2, 9); // full_sunlight (9:00 a.m.)
       $gameVariables.setValue(3, 89); // start day: march_21st
    };
})();
(I've closed and re-opened RPG Maker btw)...

Let's hope someone could come with an idea about this issue. Thanks for the advice @Andar :)
 

Ahuramazda

Veteran
Veteran
Joined
Nov 9, 2012
Messages
253
Reaction score
118
First Language
English
Primarily Uses
RMMZ
Unless you have something else re-writing that function after that plugin section is called that should be working. I'm using something similar to set up about 50 variables and switches in my MZ game automatically and its working just fine.

the only difference is in mine I make the changes in a different location because I have a custom skip-title plugin I made. Try to see if this one will work for you? Just tested it myself in a clean project and works fine :D

Code:
const _aliasNewGame = Scene_Title.prototype.commandNewGame
Scene_Title.prototype.commandNewGame = function() {
    _aliasNewGame.call(this);
    // change variables/switches here.
    $gameVariables.setValue(1, 0); // minutes_initialization
    $gameVariables.setValue(2, 9); // full_sunlight (9:00 a.m.)
    $gameVariables.setValue(3, 89); // start day: march_21st
};
 

Javo

Veteran
Veteran
Joined
Jun 25, 2020
Messages
33
Reaction score
4
First Language
Spanish
Primarily Uses
RMMZ
Greetings @Ahuramazda :

I've used your approach and the result is the same: the variables are not initialized.

Attached to this reply I've put screens of both the Variable definitions and the testing debugging monitor [F9] (don't know its formal name n.n'); the third screen is where the Script is located (in case that could be a source of problems).

As it could be seeing in the debug, the variables are still not initialized; any ideas?
 

Attachments

Last edited:

Ahuramazda

Veteran
Veteran
Joined
Nov 9, 2012
Messages
253
Reaction score
118
First Language
English
Primarily Uses
RMMZ
Ummmmm....

Did you copy my code exactly or did you try placing it in that crazy bracket setup?
Code:
(function () {
    // blah
}());
I never use functions like that and my code section works without that extra stuff, but not if you did use it.

Just tested it with both versions of the code and that's exactly the issue. Copy/paste my code bit from previous post exactly and it should work, and honestly that's probably why the original code isn't working either.

Made it for you so you can just place it in your project if I sound confusing. working on code over here myself for a save screen re-write, so my minds everywhere atm. You can name it to whatever you want.
 

Attachments

caethyril

^_^
Veteran
Joined
Feb 21, 2018
Messages
1,949
Reaction score
1,399
First Language
EN
Primarily Uses
RMMZ
I would typically alias DataManager.createGameObjects for this, rather than setupNewGame, but the cited examples all look OK to me...

Are you starting from a new game? If not, variable values will remain as they were when the save was made.

Have you added InitializeGlobalVar to your project via the Plugin Manager?

I'm pretty sure that you don't need a plugin here! :kaoswt: You can initialise variable values by simply having a "do once" Autorun event on your starting map. E.g. a map event with two pages:
  1. Condition: (None), Trigger: Autorun, Contents:
    Code:
    ◆Comment:Initialise game values~
    ◆Control Variables:#0002 Hour of Day = 9
    ◆Control Variables:#0003 Day of Year = 89
    ◆Comment:Toggle to event page 2 (inactive).
    ◆Control Self Switch:A = ON
    (Note that the default starting value for variables is 0.)

  2. Condition: Self-Switch A is ON, Trigger: Action Button, Contents: (None).
 

Javo

Veteran
Veteran
Joined
Jun 25, 2020
Messages
33
Reaction score
4
First Language
Spanish
Primarily Uses
RMMZ
Yes, I did copy-paste your code (please see the attached image)..
 

Attachments

Nolonar

Veteran
Veteran
Joined
Feb 18, 2018
Messages
160
Reaction score
232
First Language
French, German
Primarily Uses
RMMZ
Just to make sure, but did you add InitializeGlobalVar to the plugin list of your plugin manager, and is it turned on?
 

Ahuramazda

Veteran
Veteran
Joined
Nov 9, 2012
Messages
253
Reaction score
118
First Language
English
Primarily Uses
RMMZ
In that case then I have no idea where to go besides @caethyril's idea for a auto-run event on the first map of the game o_O

Edit: Wait... didn't even pay attention there was no screenshot of the plugin manager before, yeah... if they aren't loaded into the plugin manager they will not have an effect even if they are in the plugins folder.
 

Andar

Veteran
Veteran
Joined
Mar 5, 2013
Messages
30,958
Reaction score
7,465
First Language
German
Primarily Uses
RMMV
@Javo
Why do you show screenshots of the plugin folder instead of the plugin manager?
As the poster above said placing a file into the folder does NOT use that file - you need to activate it inside the plugin manager inside the project editor before it is used.

Please show us a screenshot of your plugin manager window to make sure the file is correctly installed.
 

Javo

Veteran
Veteran
Joined
Jun 25, 2020
Messages
33
Reaction score
4
First Language
Spanish
Primarily Uses
RMMZ
Greetings @caethyril , welcome to the discussion :)

I would typically alias DataManager.createGameObjects for this, rather than setupNewGame, but the cited examples all look OK to me...

Are you starting from a new game? If not, variable values will remain as they were when the save was made.
Yes, I'm making my test from a new game, but thanks for considering the possibility of a previous saved values.

Have you added InitializeGlobalVar to your project via the Plugin Manager?
No, I've put it "manually"... is it the possible problem?... and YES, it is! I've done the edit -> select plug in: set to "on" and it worked! I see that the "old practices" (aka, MV), has got its consequences n.n

So, this is it: now it all works as intended :)

I'm pretty sure that you don't need a plugin here! :kaoswt: You can initialise variable values by simply having a "do once" Autorun event on your starting map. E.g. a map event with two pages:
  1. Condition: (None), Trigger: Autorun, Contents:
    Code:
    ◆Comment:Initialise game values~
    ◆Control Variables:#0002 Hour of Day = 9
    ◆Control Variables:#0003 Day of Year = 89
    ◆Comment:Toggle to event page 2 (inactive).
    ◆Control Self Switch:A = ON
    (Note that the default starting value for variables is 0.)

  2. Condition: Self-Switch A is ON, Trigger: Action Button, Contents: (None).
I was willing to do it as a script for practicing purposes (and being more of "inner configuration": you know, programmer gimmicks xD ). I'm using a "once go auto run event" for triggering my default events, though.

Thanks a lot for the help! I'm more than thankful for the gentle and detailed help this forum gives to all the community.


p.s. I've put the plug-in manager screen for graphical purposes :)
 

Attachments

Javo

Veteran
Veteran
Joined
Jun 25, 2020
Messages
33
Reaction score
4
First Language
Spanish
Primarily Uses
RMMZ
Btw... for the sake of clarification:

The "000" in the name variables make no difference; it all seems that MZ keeps managing variable names as an "alias" of the real variable name, so, the next code works without any problem:

Code:
const _aliasNewGame = Scene_Title.prototype.commandNewGame
Scene_Title.prototype.commandNewGame = function() {
    _aliasNewGame.call(this);
    // change variables/switches here.
    $gameVariables.setValue(0001, 0); // minutes_initialization
    $gameVariables.setValue(0002, 9); // full_sunlight (9:00 a.m.)
    $gameVariables.setValue(0003, 89); // start day: march_21st
};
And the "old code" used at MV, in the same fashion, worked as well:

Code:
(function() {
    var alias_DataManager_setupNewGame = DataManager.setupNewGame;
    DataManager.setupNewGame = function() {
       alias_DataManager_setupNewGame.call(this);
       $gameVariables.setValue(0001, 0); // minutes_initialization
       $gameVariables.setValue(0002, 9); // full_sunlight (9:00 a.m.)
       $gameVariables.setValue(0003, 89); // start day: march_21st
    };
})();
I hope this information is of use for anyone else coming to this thread and whish to learn with all of us about all the RPG Maker gimmicks...

Salutes to all :)
 
Last edited:

Nolonar

Veteran
Veteran
Joined
Feb 18, 2018
Messages
160
Reaction score
232
First Language
French, German
Primarily Uses
RMMZ
Btw... for the sake of clarification:

The "000" in the name variables make no difference; it all seems that MZ keeps managing variable names as an "alias" of the real variable name, so, the next code works without any problem:
WARNING!

It worked in this particular instance, but @Andar is correct, do not EVER use leading zeroes (unless you know what you're doing). There's a good reason for that.

Try the following numbers:

10
010
0x10 (this is supposed to be the letter x)
0b10

They're all different numbers, because they're interpreted as belonging to different number systems.

10 is decimal, it's the system most people are used to. Digits go from 0 to 9.
010 is octal. Digits go from 0 to 7, so 010 is actually 8 in decimal.
0x10 is hexadecimal. Digits go from 0 to F (15), so 0x10 is actually 16 in decimal.
0b10 is binary. Digits go from 0 to 1, so 0b10 is actually 2 in decimal.

0001, 0002, and 0003 worked in your example, because 1, 2, and 3 are the same in both octal and decimal.
Since 0008 and 0009 are not valid octals, JavaScript would interpret them as decimal 8 and 9, but 0010 is valid octal and is interpreted as 8 (again).
 

caethyril

^_^
Veteran
Joined
Feb 21, 2018
Messages
1,949
Reaction score
1,399
First Language
EN
Primarily Uses
RMMZ
Great! :kaojoy:

Re the octal stuff: in ES6+ (i.e. MV v1.6+ or any version of MZ) the shorthand 0N octal representation (e.g. 0001) has been deprecated: use the formal 0o or 0O prefix instead, e.g. 0o1. Strict mode execution will throw an error on encountering the shorthand, else it will default to decimal interpretation. Examples to try in the console:
JavaScript:
08;  // 8
09;  // 9
010;  // 8
0o8;  // SyntaxError: Invalid or unexpected token

(() => {
  'use strict';
  08;
})();  // SyntaxError: Decimals with leading zeros are not allowed in strict mode.
(0o8 is invalid because octal only uses digits 0~7.)

Edit @Nolonar: oh wait you're right, when not in strict mode it still interprets 010 => 8. :kaoback:
 
Last edited:

Javo

Veteran
Veteran
Joined
Jun 25, 2020
Messages
33
Reaction score
4
First Language
Spanish
Primarily Uses
RMMZ
Greetings @Nolonar , thanks for coming in:

So, the new lesson here is that the code parser identifies the notation used and, based on it, manage the numeric system: that's a great thing to know!
No further more zero's leading again! :)
 

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

Latest Threads

Latest Posts

Latest Profile Posts

Going to submit to this game jam...struggling with the time crunch...
Have to keep compromising...
Just released a new OST for my game and a new place to purchase it!

Ah, things to do when you finish a game.
Ami
Me: It's dark cloudy! I hope it's Rain…
(2-3 Hours Later,Changed To Sunny)
Me: :kaolivid:
EpicFILE wrote on Dr_Bonehead's profile.
Dr_Bonehead? You're in this forum too? :D
Take a listen to this beautiful 1975 supermarket music. :D

Somehow it makes me think that the actual real world is still at 1970s. What we experience now is just a futuristic dream. :)

Forum statistics

Threads
104,537
Messages
1,007,006
Members
136,031
Latest member
fontschoice
Top