Alternate Set Variable Script Call

Status
Not open for further replies.

Vis_Mage

Novice Magician
Veteran
Joined
Jul 28, 2013
Messages
553
Reaction score
186
First Language
English
Primarily Uses
RMMV
Hey there,

Could someone please help me with an odd, but presumably simple mini plugin? What I'm hoping to do is create an alternate set variable script call with the same syntax as Ace. For example, this is the command in Ace:

$game_variables[1533] = "Text here!"

Ideally, the plugin would be able to read these as is, and perform the MV equivalent of this script call.

The reason behind this being that, after years of putting it off, I finally made the leap from Ace to MV, using the Ace to MV project converter. Most things transfer over fine, but script calls from Ace do not work in MV. I have hundreds of these commands throughout the project, and am hoping this would be an easy to write up solution that would save having to go through nearly every event in the project.
 

Maliki79

Veteran
Veteran
Joined
Mar 13, 2012
Messages
769
Reaction score
336
First Language
English
Primarily Uses
N/A
$gameVariables.setValue(x, y);

With x being the variable id
and Y being the value.

In the case above, you'd do:
$gameVariables.setValue(1533, "Text here!");
 

caethyril

^_^
Veteran
Joined
Feb 21, 2018
Messages
1,959
Reaction score
1,406
First Language
EN
Primarily Uses
RMMZ
A plugin could work, but I think the best solution would be to do a search-and-replace in your project's data files. Some text-editors offer regular expressions for this, which can be very useful! (Learning curve can be steep, though.)

For instance, in VS Code, you can press Ctrl+F to show the search box, then press the button with the .* icon to allow regular expressions. I just did some quick tests with these lines, seemed to work OK:
  • Find: \$game_variables\[(\d+)\]\s*=\s*(.*)
  • Replace: $gameVariables.setValue($1, $2)
Some explanation in case you're unfamiliar with regular expressions: \d+ means "one or more digits", \s* means "zero or more spaces", .* means "zero or more characters". Brackets () "capture" any matches between them; $1 means "the first captured value". :kaophew:

Otherwise:
  • Like Maliki says, the "safe" way to set a variable in RMMV is (following your example):
    JavaScript:
    $gameVariables.setValue(1533, "Text here!");
    I think this conversion would be awkward to do with a plugin, since as far as I can tell JS does not provide the ability to define custom getters for arrays. :kaoslp:

  • The "unsafe" version (i.e. it won't round numbers or check for map/event refresh) is:
    JavaScript:
    $gameVariables._data[1533] = "Text here!";
    This would be easy to implement: just stick the following code in a .js file and import as a plugin:
    JavaScript:
    $game_variables = $gameVariables._data;
    Then a script call like $game_variables[1533] = "Text here!" should get mapped correctly to the $gameVariables._data array and produce the expected results, no further work needed~

    Edit: actually, this causes an error. :kaoback: See my follow-up post for a correction:
    Oh, whoops, my mistake! $gameVariables isn't declared until afterwards, that's why. :kaoslp:

    Try this instead, I tested it this time~
    JavaScript:
    (function(alias) {
      Game_Variables.prototype.initialize = function() {
        alias.apply(this, arguments);
        $game_variables = this._data;
      };
    })(Game_Variables.prototype.initialize);
 
Last edited:

Vis_Mage

Novice Magician
Veteran
Joined
Jul 28, 2013
Messages
553
Reaction score
186
First Language
English
Primarily Uses
RMMV
Thank you both for your help! :)

@caethyril I tried creating a plugin using the line of code you provided, although I seem to be running into a bit of trouble. I get the following error in the console:
Uncaught TypeError: Cannot read property '_data' of null
 

caethyril

^_^
Veteran
Joined
Feb 21, 2018
Messages
1,959
Reaction score
1,406
First Language
EN
Primarily Uses
RMMZ
Oh, whoops, my mistake! $gameVariables isn't declared until afterwards, that's why. :kaoslp:

Try this instead, I tested it this time~
JavaScript:
(function(alias) {
  Game_Variables.prototype.initialize = function() {
    alias.apply(this, arguments);
    $game_variables = this._data;
  };
})(Game_Variables.prototype.initialize);
 

Vis_Mage

Novice Magician
Veteran
Joined
Jul 28, 2013
Messages
553
Reaction score
186
First Language
English
Primarily Uses
RMMV
Thank you very much, all seems to be working perfectly! :kaothx:
 

slimmmeiske2

Little Red Riding Hood
Global Mod
Joined
Sep 6, 2012
Messages
7,651
Reaction score
5,168
First Language
Dutch
Primarily Uses
RMXP
@Vis_Mage If your problem is solved, please report your thread and we'll close it.
 

Kes

Veteran
Veteran
Joined
Aug 3, 2012
Messages
22,290
Reaction score
11,698
First Language
English
Primarily Uses
RMVXA
[CLOSED].[/CLOSED]
 
Status
Not open for further replies.

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

Latest Threads

Latest Posts

Latest Profile Posts

Hey, guys! Do you want to hear a riddle? If so, go to this link!
Omg, 'Marsha' took over the RPGWeb twitter for a bit and its the most adorable thing ever! :LZSblush: I like it

"Gennady" Gen na Dee reptiloid scientist
I wonder how well Procedural Map generation would work in RPG Maker MZ... Well, only one way to find out! Hehe!

Forum statistics

Threads
104,634
Messages
1,007,746
Members
136,121
Latest member
Erovene
Top