Status
Not open for further replies.

Mxrio

Villager
Member
Joined
Jan 10, 2020
Messages
7
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
3,372
Reaction score
2,488
First Language
English
Primarily Uses
RMMZ
Have you tried

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

Solar_Flare

Veteran
Veteran
Joined
Jun 6, 2020
Messages
564
Reaction score
242
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
3,372
Reaction score
2,488
First Language
English
Primarily Uses
RMMZ
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,786
Reaction score
1,697
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
7
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,905
Reaction score
4,831
First Language
EN, FR
Primarily Uses
RMMZ

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.

Latest Threads

Latest Posts

Latest Profile Posts

image_2021-06-15_212613.png
Looks a bit.....kid friendly. Then again, I suppose that does with with a lot of retro themes... Crap, already spotted a flaw xD
What a stupid week so far! I was really short on sleep yesterday thanks to my back hurting and not falling asleep fast enough, now today I am 2 hours late for work because my damn alarm clock changed from the alarm setting, to the radio setting which I always keep muted.
I introduced "booby trap" as another indirect way to fight supernatural threats in the game.
But, some of my players are using it to blow up random citizens on the street.
Not sure how "creative" they can go with it. :kaojoy:
Oh, boy. I'm now having dreams about Unity.

Forum statistics

Threads
112,259
Messages
1,066,882
Members
145,877
Latest member
aone_umisato
Top