Iavra Self Variables

Description


Adds self variables to the game, that work similar to self switches.


Author


Iavra


Parameters


@param Container Name
@desc Name of the self variables container. Default: $gameSelfVariables
@default $gameSelfVariables

@param Plugin Command
@desc Name of the plugin command to manage self variables. Can't control whitespaces. Default: SelfVariable
@default SelfVariable




How to Use


Adds self variables to the game. These work similar to self switches and are uniquely identifies by a map id, an event id and a key (any string without whitespaces). This documentation assumes, that the plugin parameters "Container Name" and "Plugin Command" are set to their default values.


Basic access to self variables is done via script commands:


$gameSelfVariables.setValue([mapId, eventId, key], value);
$gameSelfVariables.value([mapId, eventId, key]);


To access a self variable of the current event, the following script command can be used:


$gameSelfVariables.get(this, key);
$gameSelfVariables.set(this, key, value);


Self Variables can also be modified via plugin commands. The following operations are available:


= Sets a self variable to a given value.
+ Adds a given value to a self variable.
- Subtracts a given value from a self variable.
/ Divides a self variable with a given value.
* Multiplies a self variable with a given value.
% Sets a self variable to the remainder when dividing it with a given value (mod).


To apply one of these operations, one of the following commands can be used:


SelfVariable key operation value // Invokes an operation with a given value.
SelfVariable key operation v[value] // Invokes an operation with the value contained in a given variable.
SelfVariable key operation self[value] // Invokes an operation with the value contained in a given self variable.
SelfVariable key operation (value1 ~ value2) // Invokes an operation with a random integer between 2 values.
SelfVariable key operation "value" // Evaluates the given expression as an integer and invokes the operation with it.


Further plugin commands are:


SelfVariable key abs // Sets a self variable to its absolute value.




Terms of Use


Free to use for both commercial and non-commercial games. Please give credit.


Download


http://pastebin.com/fGnQBBPB


Notes


The plugin originated from this request: http://forums.rpgmakerweb.com/index.php?/topic/51012-self-variables
This was actually one of my wishes for the next RPG Maker. Finally, something to minimize that extensive list of variables. Thanks a lot for this!
Added a "set" script command, that can be used to set a self variable on the current event.


Apart from the basic operations listed above, MV variables can also be set to game data, like "hp of actor 1" or "current map id". This is already possible using the script commands, but not via plugin commands. I could add this, if needed, since it's mainly a lot to write.


Performance will go down for these, though, since i have to first test for all basic functions, which involves a fair amount of regexes.
Local variables for events?! What is this madness?

This is so handy!
It doesn't seem to work with loaded games.

(New game: Trigger event & do stuff with SelfVariables. Do happy dance, because it works! Save game. Exit. // Load Game. Trigger same event => "Type Error - undefined is not a function".)

Any ideas?
Could you post the whole stack trace? (press F8 for console). Also, please post a screenshot of your event, so i can recreate it.


/edit: I fixed the issue. Looks like JSON.stringify/JSON.parse can't deal with classes defined in an anonymous function, so i had to export the Game_SelfVariables class to the public scope.
Just when I was about to post my screens XD /jk
Thanks for fixing it so fast! 🙂 Your script is awesome, btw.
A small update: Tsukihime's Custom Page Conditions has been updated to use a Game_Interpreter instance, instead of a custom function, so there's no need to use my ported plugin, anymore (you can still do so, though, it's not like it suddenly stopped working ^^).
Despite the fact that the help information says a key can be any string, the plugin doesn't seem to work unless I use an integer value for the key (in my case, I used the number 1). This added a layer of confusion to setup for this, as I was constantly plagued by undefined errors when I was using a string.


Is there a way to force the plugin to define itself on an old save? If I load an old save after installing this, it doesn't work. Is there a workaround?
It should work with any string, not just numbers. Since the plugin needs to store data in savefiles, it's not compatibe with saves created before using it.
Unfortunately it definitely doesn't work with a string.


with number.png


The above image causes a working event, which counts down 20 seconds real time before turning off it's self switch. the 'key' is the number 1.


with a 3.PNG


This is an attempt at using a string, the letter A. I had also tested with a number of words, in case single letter choices weren't viable for some reason.


This causes the following error when attempting to use it.


with A 1.png


with a 2.png


Interesting thing to note, is that is the same error it throws on an old save with or without the numbers. As you can imagine, it added some confusion to figuring out what the true cause of the issue was, when I was first trying it on an old save, and would get that error no matter what I put in, then attempted a fresh save to continue to get the error until I switched to using numbers.


Either way though, it works. Just a heads up, maybe there's a mistake in the code somewhere, or some documentation needs to change. 🙂


edit: I'm an idiot, strings need to be 'in single quotes' to work. My bad. Sorry for wasting your time XD
Hello @Iavra


Thanks for awesome plugin!





I have everything in place and working nice on the testmap, except when I copy my events out to the game-map using OrangeCustomEvents.


TypeError: Cannot read property "value" of undefined.


How can this be`?
😀
Please link to the other plugin and post a screenshot of the whole error message. There will always be incompatibilities between plugins, but i'll see if i can fix this.
Wow could you! Thanks! 


Link to plugin: http://forums.rpgmakerweb.com/index.php?/topic/46527-orange-custom-events/


Image from error:
 

Spoiler
Untitled-3.jpgerr01.jpg





I tried with moving you plugin at the top. But then I get this error. Posted it here if it helps

Spoiler
error2.jpg
Hey @Iavra, I'm using this and it's fantastic.

I just wanted to ask - I'm using it alongside GALV's event spawner, so that I can spawn enemies and objects which each have their own variable data (e.g. health). It works incredibly well.

The only concern I have is do you know how it will work re: garbage collection and cleanup? Once you "set" a variable on an event (which initialises it in memory), do you know at what point that data will be cleaned up? I'm concerned that as I spawn and despawn enemies, the memory footprint of the game could get larger and larger.

It would be really useful to have a "destroy all self variables" command or function that I could call, supplying the number of an event, which would orphan all the self-variables of that event so the garbage collector would clean them up on the next pass.

Or is this unnecessary? I'm not much of a scripter so I don't know if you already accounted for this.

Original thread: https://forums.rpgmakerweb.com/threads/iavra-self-variables.51106/