[MV] Adding extra variables to Game_BattlerBase?

Status
Not open for further replies.

Mxrio

Villager
Member
Joined
Jan 10, 2020
Messages
6
Reaction score
0
First Language
English
Primarily Uses
RMMV
I'm trying to add some extra variables that can be accessed in Damage Formulas and other code pieces via Battlers. I'm experienced in code but not making plug-ins or javascript specifically so I just modified rpg_objects.js for this attempt. Specifically, this is in Object.defineProperties() for Game_BattlerBase.
Screen Shot 2020-07-08 at 8.04.16 PM.png
I'm only trying to create two booleans as Battler variables that can be set to different values, either by method or just by direct access. I've already tried creating a method in case the variable was automatically a protected or private variable for whatever reason but they don't seem to change the .hr or .cr values.
Screen Shot 2020-07-08 at 8.13.44 PM.png
($gameSwitches calls are just debug tools, for whatever reason they don't have any effect. Ignore the missing semi-colon at the end of getHr() I just saw it now but still having issues.)
However I can't seem to get it working the way I want to, am I missing something?
I've looked up in the API how Object.defineProperties() works but I'm wondering if there's something I don't know.
 

Trihan

Speedy Scripter
Veteran
Joined
Apr 12, 2012
Messages
2,602
Reaction score
1,932
First Language
English
Primarily Uses
RMMV
Have you tried

Code:
hr: { get: function() { return false; }, writable: true }
?
 

Solar_Flare

Veteran
Veteran
Joined
Jun 6, 2020
Messages
523
Reaction score
230
First Language
English
Primarily Uses
RMMV
There's no reason to go to all this trouble with getters and setters and Object.defineProperties. Just assign them.

JavaScript:
Game_BattlerBase.prototype.hr = false;
Game_BattlerBase.prototype.cr = false;
You need to use Object.defineProperties if you want to make a read-only or write-only property, or if you want to make a property that does something special when you get or set it. If all you want is a property that you can test and set, assigning it is sufficient.

@Trihan - That's not correct - the writable flag only works with a simple value property, not with a getter. A getter+setter solution would look like this:

JavaScript:
hr: {get: function() {return something;}, set: function(something) {doSomething();}, configurable: true}
 

Trihan

Speedy Scripter
Veteran
Joined
Apr 12, 2012
Messages
2,602
Reaction score
1,932
First Language
English
Primarily Uses
RMMV
Fair enough, I was just doing a quick drive-by on this one while working so I had no idea whether what I wrote would work or not. XD
 

Aesica

undefined
Veteran
Joined
May 12, 2018
Messages
1,469
Reaction score
1,357
First Language
English
Primarily Uses
RMMV
OP you're making this way more complicated than it needs to be. You don't need to make any plugins or really do anything special here. By default, (assuming "this" is an actor or enemy) an undefined value will be coerced to false when checked in conditionals (if statements, etc), so in a sense, both of those already usable and will be treated as false (well, undefined) when used in if statements until you assign a value to them. So, before setting anything:

Code:
// behaviors and possible gotchas: assuming this.hr hasn't been set yet:
if (this.hr == false) x = 0; else x = 1; // x === 1 (beware, likely unintended result, don't check this way)
if (!!this.hr == false) x = 0; else x = 1; // x === 0 (!! will safely coerce undefined to false, but don't use this way either)
if (this.hr) x = 1; else x = 0; // x === 0 (undefined is auto-coerced to false, generally safe)
if (!!this.hr) x = 1; else x = 0; // x === 0 (undefined is force-coerced to false, this way is the safest)
In the above examples, the last one is the safest when you need undefined to be the actual value false, but otherwise, the second to last way is generally fine and what I usually use.

Edit: here's a practical example, as a sample damage formula~

Code:
a.hr ? 500 : 25
In this example, the skill will deal 25 damage initially. Once you assign "true" to the battler's hr, the damage jumps up to 500 and will stay there until you set its hr back to false
 

Mxrio

Villager
Member
Joined
Jan 10, 2020
Messages
6
Reaction score
0
First Language
English
Primarily Uses
RMMV
@Aesica The variable is never checked without being assigned a value first so I'm not worried about that, but thanks for the info.

Anyways I was able to directly assign values to .hr and .cr despite it being in defineProperties, though I'll probably do as @Solar_Flare suggested and assign it outside of the function anyways.
 

MushroomCake28

KAMO Studio
Global Mod
Joined
Nov 18, 2015
Messages
3,523
Reaction score
4,534
First Language
English
Primarily Uses
RMMV

I've moved this thread to Learning Javascript. Please be sure to post your threads in the correct forum next time. Thank you.


This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

 
Status
Not open for further replies.

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

Latest Threads

Latest Posts

Latest Profile Posts

Playing with colour ramps. The first set is the originals, the ones only facing forward in different colour schemes are the new colours.

I present... MAGIC (in progress)

No vaccine till at least spring 2021. Have not seen our parents since Dec 2019. But at least we can phone each other. How is everyone else's situation?

Forum statistics

Threads
102,976
Messages
996,434
Members
134,445
Latest member
CheckmatedKing
Top