Creating a Global Variable in JS

StrawberrySmiles

The Talking Fruit
Veteran
Joined
Nov 23, 2012
Messages
1,768
Reaction score
576
First Language
English
Primarily Uses
RMMV
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).




Then he set up each event like so:




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



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.
 

Kyuukon

主人公
Veteran
Joined
Aug 22, 2013
Messages
2,216
Reaction score
1,078
First Language
Spanish
Primarily Uses
RMMV
window.myvariablename = 5;


maybe?
 

Ossra

Formerly Exhydra
Veteran
Joined
Aug 21, 2013
Messages
1,076
Reaction score
856
First Language
English
Primarily Uses
RMMV
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)
 
Last edited by a moderator:

StrawberrySmiles

The Talking Fruit
Veteran
Joined
Nov 23, 2012
Messages
1,768
Reaction score
576
First Language
English
Primarily Uses
RMMV
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?
 

Ossra

Formerly Exhydra
Veteran
Joined
Aug 21, 2013
Messages
1,076
Reaction score
856
First Language
English
Primarily Uses
RMMV
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
 

StrawberrySmiles

The Talking Fruit
Veteran
Joined
Nov 23, 2012
Messages
1,768
Reaction score
576
First Language
English
Primarily Uses
RMMV
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:






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




Thanks so much!
 

Ossra

Formerly Exhydra
Veteran
Joined
Aug 21, 2013
Messages
1,076
Reaction score
856
First Language
English
Primarily Uses
RMMV
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.  
 
Last edited by a moderator:

StrawberrySmiles

The Talking Fruit
Veteran
Joined
Nov 23, 2012
Messages
1,768
Reaction score
576
First Language
English
Primarily Uses
RMMV
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. :p
 

Ossra

Formerly Exhydra
Veteran
Joined
Aug 21, 2013
Messages
1,076
Reaction score
856
First Language
English
Primarily Uses
RMMV
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.
 

StrawberrySmiles

The Talking Fruit
Veteran
Joined
Nov 23, 2012
Messages
1,768
Reaction score
576
First Language
English
Primarily Uses
RMMV
Thanks so much!! Everything now works just as planned!! <333
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

Our latest feature is an interview with... me?!

People4_2 (Capelet off and on) added!

Just beat the last of us 2 last night and starting jedi: fallen order right now, both use unreal engine & when I say i knew 80% of jedi's buttons right away because they were the same buttons as TLOU2 its ridiculous, even the same narrow hallway crawl and barely-made-it jump they do. Unreal Engine is just big budget RPG Maker the way they make games nearly identical at its core lol.
Can someone recommend some fun story-heavy RPGs to me? Coming up with good gameplay is a nightmare! I was thinking of making some gameplay platforming-based, but that doesn't work well in RPG form*. I also was thinking of removing battles, but that would be too much like OneShot. I don't even know how to make good puzzles!
one bad plugin combo later and one of my followers is moonwalking off the screen on his own... I didn't even more yet on the new map lol.

Forum statistics

Threads
106,035
Messages
1,018,455
Members
137,821
Latest member
Capterson
Top