Creating a Global Variable in JS

Hello!


I've been working on an evented, yet simple (in-theory) battle system in RPG Maker MV. While I initially had an issue dealing with math and how to make my character's ATK deal damage to a variable, my boyfriend came up with a solution for dealing with events and HP.


However, in RPG Maker VX ACE he could achieve this easily, as he understands basic RGSS3. This is what he did:


In Game_System he created 2 new objects to initialize after the default objects (enemy_hpmax & enemy_hpcurrent).

Spoiler
Create_Global_Variable_in_RGSS.png



Then he set up each event like so:

Spoiler
HP+Variable_Setup.png



He's essentially getting their event ID so that each time "damage"  is dealt to them, only THEIR "HP" depletes. Like so:

Spoiler
Dealing_HP_damage.png


As JS and RGSS3 are very different, I can't obviously "copy + paste" any of this. I did see a "Game_System" area in one of the .js files, but of course I wouldn't know what to do.


Would anyone like to help me convert all this into MV?


Thank you!


P.S. I wasn't sure where this should go, but since it's kind of about learning Javascript, I thought this made sense.
window.myvariablename = 5;


maybe?
Pretty straight forward, really.


// Initalize the Global Variable
// NOTE : The next line can be placed into a JS file and added as a plugin.
var $gameEventEnemy = new Object();


// Initalize and Set HP of Enemy
// NOTE : The following lines should be placed into each event during the initalization/autorun stage.
$gameEventEnemy[this._eventId] = new Object();
$gameEventEnemy[this._eventId].hp = 10;

// Subtract HP ...
$gameEventEnemy[this._eventId].hp -= 1;

// Add HP ...
$gameEventEnemy[this._eventId].hp += 1;

// Delete Enemy ...
delete $gameEventEnemy[this._eventId];
// Probably a good idea after the enemy is dead. Otherwise you will have left-over enemies lingering
// in the variable.


Now you have an object you can store enemies into! You can even be super fancy and pull data from the enemy database ...

Code:
// Create New Enemy
// The X and Y value are used for battlefield placement, and since we are not using any of that
// you can set each to zero. The 'enemyId' is the id of the enemy from the database.
//                                             (enemyId, x, y)
$gameEventEnemy[this._eventId] = new Game_Enemy(1,0,0);

// The above enemy is the default 'bat' enemy.
// Now you have full access to the enemy information in the database!
$gameEventEnemy[this._eventId].name();
// result >> 'Bat'
$gameEventEnemy[this._eventId].hp;
// result >> 200
$gameEventEnemy[this._eventId].agi;
// result >> 30
$gameEventEnemy[this._eventId].atk;
// result >> 30
$gameEventEnemy[this._eventId].gold();
// result >> (Gold Value Set in Database)
$gameEventEnemy[this._eventId].exp();
// result >> (EXP Value Set in Database)
$gameEventEnemy[this._eventId].makeDropItems();
// result >> (Array of Drop Items Set in Database)
Thanks for your informative answer @Exhydra!


I have (at least) one last question: How can I use


$gameEventEnemy[this._eventId].hp;


as a conditional branch for checking if the enemy has 0 or less than 0 HP?
Thanks for your informative answer @Exhydra!


I have (at least) one last question: How can I use



$gameEventEnemy[this._eventId].hp;


as a conditional branch for checking if the enemy has 0 or less than 0 HP?



Like so :

Code:
◆If:Script:$gameEventEnemy[this._eventId].hp <= 0;
  ◆
:Else
  ◆
:End
So, luckily there are no errors at all.


There happens to be an issue where the Event's "HP" isn't being depleted. o.o


For reference here are the event pages for the enemy:

Spoiler
Monster+Event_01.png


Spoiler
Monster+Event_02.png



And the common event it calls if you're Sylphia:

Spoiler
Common+Event_Sylphia.png



Thanks so much!
The '$gameEventEnemy' in your common event is using 'this._eventId' which identifies the event where the script is running. You will need to get the ID of event you are attacking before you can remove HP. You could work around the issue using the following :


[Event]


◆If:Script:this.startType(0); // Action Button
◆Control Variables:#0005 eventId = this._eventId
◆Comment://
: :// Common Events
: ://
◆
:End


[Common Event]


$gameEventEnemy[$gameVariables.value(5)].hp -= 3;






EDIT: Or I suppose you could just make a property within your global variable to save the event ID.


$gameEventEnemy.currentEnemy = this._eventId;

$gameEventEnemy[$gameEventEnemy.currentEnemy] -= 3;




EDIT II: An alternative to maintaining a global variable is to store the HP values on the event itself as such :


// Initalize and Set HP of Enemy
this.character(this._eventId)._hp = 10;

// Subtract HP ...
this.character(this._eventId)._hp -= 1;

// Add HP ...
this.character(this._eventId)._hp += 1;

// Save Current Enemy being Attacked ...
$gameVariables.setValue(yourVariableId, this._eventId);

// Get Current Enemy being Attacked to Deal Damage ...
$gameMap.event($gameVariables.value(yourVariableId))._hp -= 3;


If you are wondering what 'this' is referring to, in the case of an event or common event it refers to the interpreter (Game_Interpreter) running the commands and not the event itself. The interpreter keeps track of what event is currently running (_eventId), what the current map is (_mapId), and so on.  
At last I figured out how to get the HP to go down, thanks!


Since its related to my system here, maybe I can slip in this silly question:


Is there anyway to use the current party leader's ATK as a variable?


I know each Actor's ATK can be a variable, but I'm not sure how many characters will end up being playable by the end.


My plan is to have the current leader's ATK = the EXACT amount of HP depleted. But if I have to make a conditional branch for each party member checking if they're the leader and a variable checking each of their ATK...


My head my explode. 😛
Sure, you can use $gameParty.leader().atk to grab the leader's ATK. Or $gameParty.members()[partyPositionId].atk to get the ATK of other members.
Thanks so much!! Everything now works just as planned!! <333

Original thread: https://forums.rpgmakerweb.com/threads/creating-a-global-variable-in-js.67332/