Storing data in a plugin and placing it in variables

Hey guys,

Id like to know how I can extract data that i put in a plugin, and transfer it to a in-game variable. For example if I'd make a plugin that contains:
Enemy 1

Hp 300
Strength 129
Defense 503
agility 300


Enemy 2

Hp 122
Strength 555
Defense 332
agility 123

How would i go about storing the 129 from the strength of enemy 1 in variable 40 for example?
Thanks in advance!
It depends on how you're putting it in the plugin.

I would not personally put that data in a plugin as parameters. I would put it on the enemy tab as metadata (a note) and make the plugin reference the metadata. Otherwise, every time you add a new enemy, you're going to have to go and change the plugin to add new parameters for the new enemy. Messy.
It depends on how you're putting it in the plugin.

I would not personally put that data in a plugin as parameters. I would put it on the enemy tab as metadata (a note) and make the plugin reference the metadata. Otherwise, every time you add a new enemy, you're going to have to go and change the plugin to add new parameters for the new enemy. Messy.
Im actually not using the regular battle system, and am basically just experimenting with rpg maker mv to expand my knowledge, and for now i only wish to know how to use a plugin as a place where you can grab data from, and put it into variables. Because i literally have no idea how i would even achieve that. My best way of learning is by examples, thats why i asked for an example on how that would look, so i can go from there. I would assume it would look like something like this:
Plugin =
enemy
var e1hp = 300
var e1strength = 129
var e1defense = 503
var e1agility = 300

and then the script call would be something like:
script: gameVariables.value(40) === var e1hp

So im basically asking for an example on how this pseudo code would look in working state, and how the scriptcall to add that data to a variable would look. does that make sense?
Agree with Shaz that this is a messy approach. However, since you want to use it as a learning exercise, I'll explain the basic theory.
It can easily be done by declaring/defining variables in the global scope. However be warned that this is considered a bad practice, but, you're learning so that's OK for now.

If you're on a current version of MV (1.6.0+) you can use const and let to declare a variable (they are slightly different but don't worry about that for now). If you're using an old version of MV (1.5.2 or less), then you have to use the var keyword to declare a variable.

Also get familiar with the '=' symbols.
= is for assignment

== loose equality comparison (avoid if you can)
=== strict equality comparison (use this one instead)

Now to your plugin. I will assume you already know how to use a text editor and how to activate a plugin in your game.

Code:
let e1hp = 300;
let e1strength = 129;
let e1defense = 503;
let e1agility = 300;

Then you just use an Event Command to set the variable to that value.
2018-04-17 09_50_24-Control Variables.png
Agree with Shaz that this is a messy approach. However, since you want to use it as a learning exercise, I'll explain the basic theory.
It can easily be done by declaring/defining variables in the global scope. However be warned that this is considered a bad practice, but, you're learning so that's OK for now.

If you're on a current version of MV (1.6.0+) you can use const and let to declare a variable (they are slightly different but don't worry about that for now). If you're using an old version of MV (1.5.2 or less), then you have to use the var keyword to declare a variable.

Also get familiar with the '=' symbols.
= is for assignment

== loose equality comparison (avoid if you can)
=== strict equality comparison (use this one instead)

Now to your plugin. I will assume you already know how to use a text editor and how to activate a plugin in your game.

Code:
let e1hp = 300;
let e1strength = 129;
let e1defense = 503;
let e1agility = 300;

Then you just use an Event Command to set the variable to that value.
View attachment 87577
Thanks mate, thats what i was looking for. Maybe its not that useful in many situations, but im slowly trying to get a grasp on scripting, and how to translate it to rpg maker, so im slowly putting the puzzle together with bits of information and examples like this.
Sounds good.

Now that you have seen how variables with primitive values work, here are some more examples involving more advanced types of variables.

Spoiler: Object Literals
A Javascript Object is a data structure with key&value pairs. Object literals use the curly-brace notation {}. An example of an object literal could be:
Code:
const pokemon = { name: "Pikachu", id: 25, type: "Lightning"};

Take your example now, it could be easily organized into an object:
Code:
const e1 = { hp: 300, strength: 129, defense: 503, agility: 300};

console.log( e1.hp ); //this is 300
console.log( e1.defense ); //this is 503
Then in your Control Variable event command, you would use the Script object and set the variable to e1.hp
upload_2018-4-18_9-46-1.png

Spoiler: Arrays of Objects
An Array is a special type of object, which is just an ordered list of variables. Arrays use the square-bracket notation [], and the list starts at zero. An example of an array could be:
Code:
const groceryList = ["eggs", "milk", "flour", "apples"];

console.log( groceryList[0] ); //this is eggs
console.log( groceryList[2] ); //this is flour

Each item in the list can be a primitive value, like "eggs" (string) or 42 (number). An item in the list can also be an object (and because arrays are a special type of object, you could have arrays of arrays).

In your case, the array could look something like this:
Code:
const enemies = [];
//You can add items to an array by using its index number
enemies[0] = { hp: 300, strength: 129, defense: 503, agility: 300 };
//Or you can add items to an array by using push
enemies.push( { hp: 122, strength: 555, defense: 332, agility: 123} );

console.log( enemies[0].hp ); //this is 300
console.log( enemies[0].defense ); //this is 503
console.log( enemies[1].strength ); //this is 555
In this way, all of your enemies could be kept inside a single Array (an ordered list). This is similar to how RPG Maker stores the enemies.
upload_2018-4-18_9-57-47.png

Spoiler: Scoping
As I said before, doing it this way will pollute the Global Scope. This is a common cause of incompatibility and errors, if two different plugins try to use the same variable they could overwrite it or use the wrong data. If global scope is required (like in this case) then it's better to wrap the variables in a unique namespace to protect it.

For example:
Code:
const BizNiz = {};
BizNiz.enemies = [];
BizNiz.enemies.push( { hp: 300, strength: 129, defense: 503, agility: 300 } );
BizNiz.enemies.push( { hp: 150, strength: 64, defense: 251, agility: 150 } );

console.log( BizNiz.enemies[1].defense ); //this is 251
It's more to type every time, but the trade-off is fewer errors and incompatibilities.
upload_2018-4-18_9-58-11.png
@Aloe Guvner Thanks for the very elaborate tutorial bro - also i appreciate the examples you added. And ill go through the links youve added too when i find some more time.
Also, so if i implement it with an unique namespace ''BizNiz'' for example, would you still call it a messy approach?
Using a unique namespace would be good enough for now. What it's doing is making sure you don't accidentally name something the same as what's already in RPG Maker, for example if you called your data "$dataEnemies", the game might crash because that already exists and you'd be overwriting it.

Part of it is how the event editor interacts with the code behind it, it's necessary to have your data somewhat Global so that you can access it from events.

Original thread: https://forums.rpgmakerweb.com/threads/storing-data-in-a-plugin-and-placing-it-in-variables.94085/