[MV] Adding extra variables to Game_BattlerBase?

Status
Not open for further replies.

Mxrio

Warper
Member
Joined
Jan 10, 2020
Messages
2
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,369
Reaction score
1,537
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
478
Reaction score
210
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,369
Reaction score
1,537
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,316
Reaction score
1,196
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

Warper
Member
Joined
Jan 10, 2020
Messages
2
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,425
Reaction score
4,482
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

ESAMarathon on Twitch, now streaming "Eat Girl". Yep, that's the title of a game... Apparently it's a Pacman knockoff.... Which is of course the only logical conclusion one would get from a name like "Eat Girl". :kaopride: I can't believe anybody would think anything else! :kaoback:
Super stoked i just finished my first town in my project, by finished i mean i can always add more decorative aesthetics and the NPCs don't talk yet but the mapping is complete and all the important chess pieces are present!
My brain: Hey, I have an idea how to make the transition to the main story quest in The Wastes more natural!
Me: Good!
My brain: You need to remake the hotel you start out in, it's not realistic enough.
Me: Ok... This was unexpected, but I can do it.
My brain: Now make each hotel floor 5 times as large to match the main part. Oh, you also need to make a bunch of new npcs to fill in the space on these maps.
Me: Crap.
Should be able to release Haxe MV/MZ next weekend.
It look that somehow MZ tracks are messed up (for example battle4 is obviously a theme, castle2 is a ship, ship1 is a scene and so on..). Maybe they just named them after with some ambiguity.

Forum statistics

Threads
100,618
Messages
977,838
Members
132,228
Latest member
zhengdddddd
Top