Adding Properties to RPG Objects

Wavelength

MSD Strong
Global Mod
Joined
Jul 22, 2014
Messages
5,624
Reaction score
5,107
First Language
English
Primarily Uses
RMVXA
Hi guys!  I'm starting to make the jump from Ruby to JavaScript scripting, and while I think I'm grasping the language okay, I'm having a rough time making heads or tails of how everything is organized in the RPG Maker scripts.  Hopefully someone that's already jumped these hurdles can give me a leg up here. :)

I decided to start with a simple feature that's already been done a couple times - adding HP costs to skills.  When I simply forced a 20HP cost onto each skill it went smoothly, but it fell apart when I tried to add different HP costs to each skill using notetags.

Below is the code I've written.  The current, erroneous behavior of this script is that when I hit playtest, the game will say "Now Loading..." and freeze there forever at runtime.  The title screen won't even appear.  I have three Immediately Invoked Functions (which seem to the way RPG Maker does this stuff by default, so I followed suit):

  • The first function is attempting to alias the paySkillCost method to make battlers using skills lose HP equal to the cost.  I think this is correct, because when I replaced the "if" branch with a simple this._hp -= 20 command and removed the second and third functions, the script works fine.
  • The second function is attempting to alias the isDatabaseLoaded function so that the third function will be called once when New Game is selected.  I think I am doing something wrong here, because the "Now Loading..." problem won't happen if I comment out this second function.
  • The third function is attempting to iterate through each Skill in $dataSkills (which I assume represents the skills in the database), create a new property called health_cost for the skill, see if the Skill's note contains "hp cost:", and if so, pull whatever integer comes after it as the skill's HP cost.  I'm not sure whether anything is incorrect here, but when I added most of this code to an evented "Script..." command on the map, the integer did seem to be pulled out of a hard-coded string perfectly.
(function() {    var pay_cost_with_hp = Game_BattlerBase.prototype.paySkillCost;    Game_BattlerBase.prototype.paySkillCost = function(skill) {        pay_cost_with_hp.call(this, skill);        if (skill.health_cost !== null) {            this._hp -= skill.health_cost;        }    }}());(function() {    var db_loaded_with_costs = DataManager.isDatabaseLoaded;    DataManager.isDatabaseLoaded = function() {        db_loaded_with_costs.call(this);        // define_skill_hp_costs.call(this);    }}());function define_skill_hp_costs() {    for (i = 1; i < ($dataSkills).length; i++) {        $dataSkills.health_cost = null        var myVal = ($dataSkills.note);        var matcher = /hp cost:/;        if (myVal.match(matcher)) {            var data_set = myVal.split(/:/);            var gotten_int = parseInt(data_set[1]);            if ((gotten_int !== NaN) && (gotten_int !== null)) {                $dataSkills.health_cost = gotten_int;            }        }    }}So, can you see what I'm doing wrong?  I can't understand why the game hangs forever during the loading process when I try to run it with this script.

While I'd greatly appreciate help cleaning up this code, I'd also really appreciate any tips you can offer me on creating new parameters for database items.  Am I generally using the right approach to add an "HP Cost" parameter to skills, or should I be doing something entire different to add this parameter to each skill?  This is certainly one of the most important parts of RPG Maker scripting and it's frustrating me that I can't get it right.

Thank you so much to whoever can help me break through this roadblock!!
 
Last edited by a moderator:

Tsukihime

Veteran
Veteran
Joined
Jun 30, 2012
Messages
8,564
Reaction score
3,846
First Language
English
Press F8 to open console. Are there any error messages?
 

Wavelength

MSD Strong
Global Mod
Joined
Jul 22, 2014
Messages
5,624
Reaction score
5,107
First Language
English
Primarily Uses
RMVXA
Press F8 to open console. Are there any error messages?
Thanks for the idea!  I'm completely new to this Console, so let me know if I'm doing something incorrectly, but I pressed F8 as soon as the "Now Loading..." message came up (right after hitting Playtest), and the Console tab showed no errors - just a line that I could type on.
 

Tsukihime

Veteran
Veteran
Joined
Jun 30, 2012
Messages
8,564
Reaction score
3,846
First Language
English
I didn't actually look at your code when I wrote that. I was hoping it was just an error on your part and you could figure it out after seeing it lol

Anyways here's the issue

(function() { var db_loaded_with_costs = DataManager.isDatabaseLoaded; DataManager.isDatabaseLoaded = function() { db_loaded_with_costs.call(this); // define_skill_hp_costs.call(this); }}());Originally, that method returns a boolean.However, when you aliased it and called the alias, you're not actually returning a value.

Javascript functions return null if there were no explicit return statements.

This is in contrast to Ruby, which simply assumed the last line will be returned (presumably taken from functional programming approach)

And in Javascript, "null" evaluates to boolean false in a condition, so your method is basically saying it's never loaded.

Some people may have picked up the habit of omitting return statements in Ruby, which can lead to dangerous things like this in JS.

...

Anyways at least I think that's what the problem is lol
 
Last edited by a moderator:

Wavelength

MSD Strong
Global Mod
Joined
Jul 22, 2014
Messages
5,624
Reaction score
5,107
First Language
English
Primarily Uses
RMVXA
Thank you so much, Hime!!  That was extremely helpful and it got me through this. :D

Based on your advice I looked into the original method, and instead of simply calling that method, now I call it implicitly by setting a variable equal to the method call.  Finally, after the extra steps are handled, I return the value that was returned by the call to the original method. (This is an attempt to capture the original value of 'true' or 'false' for when the rest of the system setup needs to know this value - is this going to properly preserve the value of the original True/False return?)

The corrected second function:

(function() { var db_loaded_with_costs = DataManager.isDatabaseLoaded; DataManager.isDatabaseLoaded = function() { ret_value = db_loaded_with_costs.call(this); define_skill_hp_costs.call(this); return ret_value; }}());Under this new paradigm, the game loads properly, and a few simple tests are showing the HP Cost notetags correctly resulting in HP loss when the corresponding abilities are used in combat.

I really appreciate it!  You've helped me get good at Ruby and now you're helping me learn JavaScript.
 
Last edited by a moderator:

DarknessFalls

Rpg Maker Jesus - JS Dev.
Veteran
Joined
Jun 7, 2013
Messages
1,393
Reaction score
210
First Language
English
Hi guys!  I'm starting to make the jump from Ruby to JavaScript scripting, and while I think I'm grasping the language okay, I'm having a rough time making heads or tails of how everything is organized in the RPG Maker scripts.  Hopefully someone that's already jumped these hurdles can give me a leg up here. :)

I decided to start with a simple feature that's already been done a couple times - adding HP costs to skills.  When I simply forced a 20HP cost onto each skill it went smoothly, but it fell apart when I tried to add different HP costs to each skill using notetags.

Below is the code I've written.  The current, erroneous behavior of this script is that when I hit playtest, the game will say "Now Loading..." and freeze there forever at runtime.  The title screen won't even appear.  I have three Immediately Invoked Functions (which seem to the way RPG Maker does this stuff by default, so I followed suit):

  •  
  • The first function is attempting to alias the paySkillCost method to make battlers using skills lose HP equal to the cost.  I think this is correct, because when I replaced the "if" branch with a simple this._hp -= 20 command and removed the second and third functions, the script works fine.
  •  
  • The second function is attempting to alias the isDatabaseLoaded function so that the third function will be called once when New Game is selected.  I think I am doing something wrong here, because the "Now Loading..." problem won't happen if I comment out this second function.
  •  
  • The third function is attempting to iterate through each Skill in $dataSkills (which I assume represents the skills in the database), create a new property called health_cost for the skill, see if the Skill's note contains "hp cost:", and if so, pull whatever integer comes after it as the skill's HP cost.  I'm not sure whether anything is incorrect here, but when I added most of this code to an evented "Script..." command on the map, the integer did seem to be pulled out of a hard-coded string perfectly.
  •  

(function() {    var pay_cost_with_hp = Game_BattlerBase.prototype.paySkillCost;    Game_BattlerBase.prototype.paySkillCost = function(skill) {        pay_cost_with_hp.call(this, skill);        if (skill.health_cost !== null) {            this._hp -= skill.health_cost;        }    }}());(function() {    var db_loaded_with_costs = DataManager.isDatabaseLoaded;    DataManager.isDatabaseLoaded = function() {        db_loaded_with_costs.call(this);        // define_skill_hp_costs.call(this);    }}());function define_skill_hp_costs() {    for (i = 1; i < ($dataSkills).length; i++) {        $dataSkills.health_cost = null        var myVal = ($dataSkills.note);        var matcher = /hp cost:/;        if (myVal.match(matcher)) {            var data_set = myVal.split(/:/);            var gotten_int = parseInt(data_set[1]);            if ((gotten_int !== NaN) && (gotten_int !== null)) {                $dataSkills.health_cost = gotten_int;            }        }    }}So, can you see what I'm doing wrong?  I can't understand why the game hangs forever during the loading process when I try to run it with this script.

While I'd greatly appreciate help cleaning up this code, I'd also really appreciate any tips you can offer me on creating new parameters for database items.  Am I generally using the right approach to add an "HP Cost" parameter to skills, or should I be doing something entire different to add this parameter to each skill?  This is certainly one of the most important parts of RPG Maker scripting and it's frustrating me that I can't get it right.

Thank you so much to whoever can help me break through this roadblock!!


They have kept everything almost the same from ruby toe javascript including class names and method names. The use of 

(function() {})Is one issues, you use this else where and because of that the concept of "this" is scoped differently across all of them. You need one instance of 

(function() {})in order to keep scope of "this"
 

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

Latest Threads

Latest Posts

Latest Profile Posts

I should realize that error was produced by a outdated version of MZ so that's why it pop up like that
Ami
i can't wait to drink some ice after struggling with my illness in 9 days. 9 days is really bad for me,i can't focus with my shop and even can't do something with my project
How many hours have you got in mz so far?

A bit of a "sparkle" update to the lower portion of the world map. :LZSexcite:
attack on titan final season is airing tomorrow, I'm excited and scared at the same time!

Forum statistics

Threads
105,883
Messages
1,017,232
Members
137,607
Latest member
Maddo
Top