JavaScript questions that don't deserve their own thread

Is there a simple way to hide the item command in battle if it is sealed? Would I need a script for that?
You would need a plugin (scripts are for the earlier RPG Makers that use Ruby) such as Victor Engine's Command Replace.
So this is prolly a simple thing for many, but im bad at this stuff.

Using Himeworks weapon damage and the suggested formula is something like
10 * (Math.randomInt(5) + 1) which is fine ofc, but I tried to find an even simpler way for my simple brain and ended up with Math.randomInt(5) + 15. I tested it and it seems fine.

So the question is really just, is this an ok way to do 15-20 damage? Is the suggested way superior in some way I'm not aware of?

Would suck to have to go back and change it all in the long run 😛
@Willibab
The suggested formula will give you either 10, 20, 30, 40 or 50 damage.

If you want to deal 15-20 damage, you actually came close, but
its actually
"Math.randomInt(6) + 15"
instead of
"Math.randomInt(5) + 15"

Otherwise you'll end up with 15-19 damage instead.
When I first started using RPG Maker MV a couple of years ago, I wanted to put an external array in a variable. @Lanzy graciously wrote me a plugin that would do that. Essentially there are 2 lines, no parameters.
Code:
var $dataList = null;
DataManager._databaseFiles.push({name: '$dataList', src: 'List.json'});
Then all it needs is a simple assignment to a variable
◆Control Variables:#0001 arrayFromFile = $dataList

and a dialogue box will verify the contents
Contents of variable01 = \v[1]
shows
Contents of variable01 = dog,bird,cat,bird,horse

For reference, here's a link to Lanzy's Code plugin posted earlier in this same thread.

The plugin works great in MV, but not in MZ. Turning the plugin on in MZ prevents MZ from loading, giving this error (Note, this is a vanilla project, one map, one event, no plugins except Lanzy's.)

View attachment 220810

Console shows

View attachment 220811

Since I'm using the exact same plugin and .json file from MV, I have to assume MZ either doesn't support this or that the MZ terminology is slightly different.

tldr: Can someone translate MV's
Code:
var $dataList = null;
DataManager._databaseFiles.push({name: '$dataList', src: 'List.json'});
to MZ? As always, thank you to any and all who respond.

As an aside, I was loading the external file within the game itself using
Code:
var fs = require('fs');
:      :var wordList = fs.readFileSync("./data/wordListAbridged.dat", 'utf-8');
:      :$gameVariables.setValue(1, wordList);
but, apparently, if I deploy for web, the browser refuses to load the file and this is for a game I made for my grandchildren who use Chrome tablets, not Windows, so I really want to go the .json route if I can.

Edit-I was able to accomplish loading the word list by copying and pasting the file contents using variable = script, then running that event with autorun when the game starts. Works well enough, seems that the variable had no trouble holding 1700+ words, but I'd still like to know if there's an equivalent of DataManager._databaseFiles for MZ. Thanks again.
Hey there,

since I recently bought MZ I thought I'd give it a try and see if my script still works the way it did in MV. And so far I didn't have any issues. MZ uses exactly the same DataManager structure as in MV.

The error occurs when you use a primitive data type like strings or numbers. That means that something might be wrong with the json file you stored in the data folder. Can you post the json file your are using? That is, if you haven't solved the problem already.
Is there a simple way to hide the item command in battle if it is sealed? Would I need a script for that?

During battle calls this script and the item command disappears.

JavaScript:
SceneManager._scene._actorCommandWindow._list.pop();
SceneManager._scene._actorCommandWindow.clearItem(3);

If you want it to return use:

JavaScript:
SceneManager._scene._actorCommandWindow.refresh();

You can also have it check if item command is enabled and if not, then have it removed

JavaScript:
let actorWindow = SceneManager._scene._actorCommandWindow;
actorWindow.refresh();

if (!actorWindow._list[3].enabled) {
    actorWindow._list.pop();
    actorWindow.clearItem(3);
}

Edit:
Sorry for double post, was too tired to realize
During battle calls this script and the item command disappears.

JavaScript:
SceneManager._scene._actorCommandWindow._list.pop();
SceneManager._scene._actorCommandWindow.clearItem(3);

If you want it to return use:

JavaScript:
SceneManager._scene._actorCommandWindow.refresh();

You can also have it check if item command is enabled and if not, then have it removed

JavaScript:
let actorWindow = SceneManager._scene._actorCommandWindow;
actorWindow.refresh();

if (!actorWindow._list[3].enabled) {
    actorWindow._list.pop();
    actorWindow.clearItem(3);
}

Edit:
Sorry for double post, was too tired to realize
Thank you. I will try those. I guess i would run those in troops?
Script that let an event face the player's direction?
Thank you. I will try those. I guess i would run those in troops?
I didn't test this, but try this plugin. It should run the script every time you reached the input phase of the battle.

The plugin looks like this
JavaScript:
BattleManager_alias_startInput = BattleManager.startInput;
BattleManager.startInput = function() {
    BattleManager_alias_startInput.call(this);
    let actorWindow = SceneManager._scene._actorCommandWindow;
   
    actorWindow.refresh();
    if (actorWindow._list[3] && !actorWindow._list[3].enabled) {
        actorWindow._list.pop();
        actorWindow.clearItem(3);
    }
}

Script that let an event face the player's direction?
You don't need a script for that. Just go to the event -> Route... -> Turn Toward Player
Can someone tell me what is wrong with this script code. I searched the forums and found this in a similar topic (I only edited some numbers and names). The idea is trying to use it in a battle script to get random skills at the start of the turn. In this testing phase I tried to set it to randomly select 3 skills out of 5 possilbe.

EDIT: I'm using MZ

JavaScript:
var skills = [12, 13, 14, 15, 16], vIds = [1, 2, 3];
for (var n = 0; n < vIds.length; n++) {
  if (skills.length === 0) break;
  var ix = Math.randomInt(skills.length);
  $gameParty.leader().learnSkill(vIds[n], skills.splice(ix, 1)[0]);
}
Can someone tell me what is wrong with this script code. I searched the forums and found this in a similar topic (I only edited some numbers and names). The idea is trying to use it in a battle script to get random skills at the start of the turn. In this testing phase I tried to set it to randomly select 3 skills out of 5 possilbe.

EDIT: I'm using MZ

JavaScript:
var skills = [12, 13, 14, 15, 16], vIds = [1, 2, 3];
for (var n = 0; n < vIds.length; n++) {
  if (skills.length === 0) break;
  var ix = Math.randomInt(skills.length);
  $gameParty.leader().learnSkill(vIds[n], skills.splice(ix, 1)[0]);
}

Don't know what exactly isn't working, but one thing that does stick out is the learnSkill method.
If you look at it:
JavaScript:
Game_Actor.prototype.learnSkill = function (skillId) {
    if (!this.isLearnedSkill(skillId)) {
        this._skills.push(skillId);
        this._skills.sort(function (a, b) {
            return a - b;
        });
    }
};

It only takes one argument. But in your code it takes 2. What are the vIds for? And why splice skills? Are they supposed to be assigned only once? Also in your example you are missing a curly bracket at the end. Probably copy&paste mistake.
Don't know what exactly isn't working, but one thing that does stick out is the learnSkill method.
If you look at it:
JavaScript:
Game_Actor.prototype.learnSkill = function (skillId) {
    if (!this.isLearnedSkill(skillId)) {
        this._skills.push(skillId);
        this._skills.sort(function (a, b) {
            return a - b;
        });
    }
};

It only takes one argument. But in your code it takes 2. What are the vIds for? And why splice skills? Are they supposed to be assigned only once? Also in your example you are missing a curly bracket at the end. Probably copy&paste mistake.

Not working as in my character isn't gaining the skills. And I don't know anything about coding 😀

I just copied that code from a different topic where it was talking about picking something out of an array (and using it for some other purpose than gaining skills). But since the purpose was kind of similar, I just thought that I could stick that learnSkill thing inside there (obviously not, haha).

Some of that code is meant for making sure that if it picks something, it can't be picked again. So for example if it picks skill number 13, then that number is taken out of the array when it picks the next one (or that's what I read).
Not working as in my character isn't gaining the skills. And I don't know anything about coding 😀

I just copied that code from a different topic where it was talking about picking something out of an array (and using it for some other purpose than gaining skills). But since the purpose was kind of similar, I just thought that I could stick that learnSkill thing inside there (obviously not, haha).

Some of that code is meant for making sure that if it picks something, it can't be picked again. So for example if it picks skill number 13, then that number is taken out of the array when it picks the next one (or that's what I read).
Oh ok, gotcha.

Try this:
JavaScript:
var skills = [12, 13, 14, 15, 16]
var randomSkillId = 0;

for (var n = 1; n <= $gameParty.size(); n++) {
    if (skills.length === 0) break;
    randomSkillId = Math.randomInt(skills.length);
    $gameActors._data[n].learnSkill(skills[randomSkillId]);
    skills.splice(randomSkillId, 1);
}
Oh ok, gotcha.

Try this:
JavaScript:
var skills = [12, 13, 14, 15, 16]
var randomSkillId = 0;

for (var n = 1; n <= $gameParty.size(); n++) {
    if (skills.length === 0) break;
    randomSkillId = Math.randomInt(skills.length);
    $gameActors._data[n].learnSkill(skills[randomSkillId]);
    skills.splice(randomSkillId, 1);
}

EDIT:
Ok, I think I got it to work now. So I changed that n <= $gameParty.size() to n <= 3 (and changed that gameActors part to party leader). Thanks for the help!

/////////////

Now (after changing that gameActors part to party leader thing) I'm getting one skill per turn. Maybe I wasn't clear enough, when I said pick 3 skills out of 5, I meant pick all 3 skills for just one character. So I'm testing a solo battle with just 1 character, and she is getting a number (3) of random skills from a pool (5 for test purposes) at the beginning of a turn. As I said, I don't understand code, but I see your code uses something called gameParty.size, so maybe that code would allocate skills to multiple party members?
Hey there,

since I recently bought MZ I thought I'd give it a try and see if my script still works the way it did in MV. And so far I didn't have any issues. MZ uses exactly the same DataManager structure as in MV.

The error occurs when you use a primitive data type like strings or numbers. That means that something might be wrong with the json file you stored in the data folder. Can you post the json file your are using? That is, if you haven't solved the problem already.
I tried it again with the same error. I replaced the file with a simple ["dog","bird","cat"] file and it loaded as expected, so I'm sure you're right that there's something wrong with my new text file. Hardcoding the file contents into the variable works really well, so it's not a huge deal. To make this new file, I took a 5 letter word list with a few thousand words, manually removed uncommon and/or obscure words, added quotes around each word, inserted commas, etc. It's entirely likely I corrupted the file in some way.
Thanks for weighing in. 🙂
One of my plugins supports javascript and I was wondering if there was a piece of code I could use to make the gab window smaller it's too big and there isn't a setting for it
Has anyone made an Object to Object mapping of RPG Maker MZ or any other quick reference resources that I can use for plugin development. It would expedite my development process.
EDIT:
Ok, I think I got it to work now. So I changed that n <= $gameParty.size() to n <= 3 (and changed that gameActors part to party leader). Thanks for the help!

/////////////

Now (after changing that gameActors part to party leader thing) I'm getting one skill per turn.
Yeah, looks like there was a misunderstanding with the replacement code you were given.
Code:
var skills = [12, 13, 14, 15, 16];
var randomSkillId;

for (var n = 1; n <= 3; n++) {
    randomSkillId = Math.randomInt(skills.length);
    $gameParty.leader().learnSkill(skills[randomSkillId]);
    skills.splice(randomSkillId, 1);
}

I have no idea what's going on in that original snippet you posted, but this one will make the party leader learn 3 skills randomly chosen from the 5 (or however many) you type into the skills array on the first line.

One of my plugins supports javascript and I was wondering if there was a piece of code I could use to make the gab window smaller it's too big and there isn't a setting for it
This should be its own thread in Plugin Support, wherein you provide a link to the plugin in question. Just referencing a "gab window" doesn't mean anything, code-wise, and it's a little pointless to toss suggestions at you without seeing the code that makes the window in the first place.
Hey, quick question. Is there a script call I can use to set Actor X (ID equal to a variable) to party slot 2? The actor is already in the party.
Is there a script call I can use to set Actor X (ID equal to a variable) to party slot 2?
No, you can't directly set an actor to a place in the party - what would happen to any actor already in that spot? And the rest of the party?

There is a $gameParty.swapOrder() function, so you have to use a little logic with it. The easiest thing to do would be:
Code:
$gameParty.swapOrder(1, $gameParty.members().indexOf($gameActors.actor(X)));
where X is the ID of the actor you want to move. That will swap that actor with whichever was already in position 2.
where X is the ID of the actor you want to move. That will swap that actor with whichever was already in position 2.
Ah, I probably didn't phrase that very clearly. Swapping the actor in the second slot with actor X is what I was hoping to do, and the script call works like a charm. Thank you! 🙏

Original thread: https://forums.rpgmakerweb.com/threads/javascript-questions-that-dont-deserve-their-own-thread.46341/