Alternate Set Variable Script Call

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.
$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!");
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". 😮‍💨

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. 😴

  • 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. 👋 See my follow-up post for a correction:
    Oh, whoops, my mistake! $gameVariables isn't declared until afterwards, that's why. 😴

    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);
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
Oh, whoops, my mistake! $gameVariables isn't declared until afterwards, that's why. 😴

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);
Thank you very much, all seems to be working perfectly! 🙏
@Vis_Mage If your problem is solved, please report your thread and we'll close it.
Thread closed

Original thread: https://forums.rpgmakerweb.com/threads/alternate-set-variable-script-call.118296/