[MV] Adding extra variables to Game_BattlerBase?

Status
Not open for further replies.

Mxrio

Warper
Member
Joined
Jan 10, 2020
Messages
4
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,379
Reaction score
1,559
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
493
Reaction score
216
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,379
Reaction score
1,559
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,323
Reaction score
1,207
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
4
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,431
Reaction score
4,486
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

And back. Trying to work online with no internet for 1.5 days was...interesting. I felt like a character in a old Sierra game trying to find internet due to all the hoops I had to jump through just to get online for work.
grief... if MV didn't have certain plugins already, I think I'd upgrade to MZ. seeing like 10 MV plugins in 1 MZ one is hilariously convenient lol.
Have you already checkes out my mapping tutorial on the blog? I would love to hear your feedback and things you'd like to see in the future!

one of character for my upcoming game idk how to design o_O

Designing patches that can 'augmented' to clothing based armor. Such good fun. A bit time consuming due to the need to create image displays, but the patches are limited so it's not too tiring.
Note: lol, posting this made me find a couple mistakes.

Forum statistics

Threads
100,737
Messages
978,947
Members
132,359
Latest member
PhilKeepItReal
Top