The title is self-explainatory. Is it possible to change the value of a variable using a command in the map notes, that runs when you enter a map? Maybe using a plugin?
Is it possible to set a variable through the map notes?
RPG Maker MV SupportYes, it's possible, but it would be more practical to simply create a map event in a parallel process that is deleted when triggered.
But if you find the effect too slow for events that depend on this event, this plugin might solve the problem. I wrote this without testing it because I'm not at home.
In the map notes:
Example (variable 15, set value 500):
But if you find the effect too slow for events that depend on this event, this plugin might solve the problem. I wrote this without testing it because I'm not at home.
JavaScript:
(function () {
const _GP_transfer = Game_Player.prototype.performTransfer;
Game_Player.prototype.performTransfer = function() {
if (this.isTransferring()) {
if ($dataMap.meta.setVariable) {
const setvar = $dataMap.meta.setVariable.split(",");
$gameVariables.setValue(Number(setvar[0])|0,Number(setvar[1])|0); /* By Caethyril*/
}
}
_GP_transfer.call(this);
}
})();
In the map notes:
<setVariable: ID Variable, Value>Example (variable 15, set value 500):
<setVariable:15,500>Thank you so much for your time.
I tried running the plugin and it gives me this error when I run the game
It seems to be a conflict with some plugin; this addRect function is related to graphs. When I get home I'll test it and confirm if I wrote anything wrong.
I see the error: the
_GP_transfer.call(this) line is misplaced. It should be moved up one line.I just tested it, and the error is different:
From what I saw, I ended up placing the function outside of the prototype. Now it's fixed. The function error may have broken another plugin not mentioned by the OP.
TypeErrorthis.isTransferring is not a functionFrom what I saw, I ended up placing the function outside of the prototype. Now it's fixed. The function error may have broken another plugin not mentioned by the OP.
I would also recommend converting the split values from string to number, e.g.
JavaScript:
$gameVariables.setValue(setvar[0] | 0, setvar[1] | 0);
would that not only work with lower numbers as big numbers can end up negatives?
wouldn't it be saver to use
|| 0 if it can be string?that logical OR wouldnt produce a number unless the string was empty. if the bitwise OR ends up presenting a problem, casting it into a number with Number() should still work regardless
(Number(setvar[0]))?Number(setvar[0]):0,(Number(setvar[1]))?Number(setvar[1]):0;
Or:
Please disregard... my lack of attention.
No worries!
@ShadowDragon - like Robro implied,
Number(v)or+vreturns a number,NaNif non-numeric.- May return a non-integer value.
parseInt(v, 10)returns an integer (truncated), orNaNif non-numeric.- Does not recognise notations like
0b10or1e6.
- Does not recognise notations like
v | 0returns a 32-bit integer (truncated),0if non-numeric.- Only correct for values in the range
-2³¹ ≤ v ≤ 2³¹ - 1.
- Only correct for values in the range
isNaN(v) ? 0 : Math.trunc(Number(v))returns an integer (truncated), 0 if non-numeric.- Clunky but doesn't have the range limit of
v | 0(if that's a concern).
- Clunky but doesn't have the range limit of
@ShadowDragon - like Robro implied,
|| is logical OR, | is bitwise OR.Original thread: https://forums.rpgmakerweb.com/threads/is-it-possible-to-set-a-variable-through-the-map-notes.182392/