[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,382
Reaction score
1,566
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
502
Reaction score
220
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,382
Reaction score
1,566
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,339
Reaction score
1,225
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,437
Reaction score
4,491
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

The MZ first look stream was quite good. Looks like we get a lot of generator parts right off the bat this time around. Lots of cool clothes, helmets, facial marks, etc. Looking forward to playing around with it. :)
Any tips on how to share resources in here? Should I upload them in a image hosting site or Google Drive?
That moment when you realize your game size without the sound is 51.1 MB.... But with it (unoptimized as it is) it's 156 MB.... I guess it's time to do a bit of sound optimizing. :kaoswt: Also I learned that deployed for Windows it's 308MB... Apparently MV adds a pretty hefty helping of stuff on deployment. :kaoback:
I wanna try making my game into an app because that sounds cool but I am so lost on how to :kaocry:
I found some tutorial but they all seem to use an older android studio(2.3.3)? I think the current version is 4.0.1?
is there an updated tutorial or do I need to find a way to get 2.3.3.?

Forum statistics

Threads
100,821
Messages
979,936
Members
132,465
Latest member
GachaPringle
Top