Alternate Set Variable Script Call

Status
Not open for further replies.

Vis_Mage

Novice Magician
Veteran
Joined
Jul 28, 2013
Messages
451
Reaction score
163
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
660
Reaction score
251
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,510
Reaction score
992
First Language
EN
Primarily Uses
RMMV
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
451
Reaction score
163
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,510
Reaction score
992
First Language
EN
Primarily Uses
RMMV
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
451
Reaction score
163
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
6,158
Reaction score
4,296
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
21,781
Reaction score
10,996
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

Ami
4 days im not using my laptop to waiting someone to reinstall (sigh)
Stream will be live shortly with some Donkey Kong 64! Feel free to drop by!
Working on a test Boss for the new No Travel Game Jam
- Name Plugin by Moghunter -
Free RPG Maker Ready Charging Up Animation

Enjoy -No Credit Needed But Appreciated -
Download it at https://lvgames.itch.io/free-charging-up-pixel-animation-fx-rpg-maker-ready
:/ .... so, thought I’d go to the park to sit in the cool breeze and get out of the house a little. Nope, guess that’s quarantined too. Ok...

Forum statistics

Threads
95,490
Messages
929,547
Members
125,740
Latest member
Smeinela
Top