JavaScript questions that don't deserve their own thread

ATT_Turan

Forewarner of the Black Wind
Veteran
Joined
Jul 2, 2014
Messages
4,588
Reaction score
2,931
First Language
English
Primarily Uses
RMMV
The code from ATT_Turan did not work, because the find function does not return the index of the object in the array, but the object itself.
Thanks for the correction.

(MV)
is there a script call that interprete the exact posicion in pixels
$gamePlayer and events have a screenX() and screenY() method that should be what you're looking for.
 

DarielZer0

Villager
Member
Joined
Oct 16, 2021
Messages
27
Reaction score
8
First Language
Spanish
Primarily Uses
RMMV
$gamePlayer and events have a screenX() and screenY() method that should be what you're looking for.
Hi,no,that still gives me the distance in grids,grids are 48x48 pixels and i am using a pixel movement plugin,for example a map of 13x17 grids have 624x816 pixels and i want to know if there is a way to store in a variable,for example,that i am exactly in the pixel 60 of the X axis
 

Eni

Demon Lord
Veteran
Joined
Oct 30, 2018
Messages
61
Reaction score
6
First Language
Swedish
Primarily Uses
RMMV
Hello, So I'm trying to learn how to make plugins and thought the best way to do so it by trying to create one myself. I understand that I need to use
Code:
pluginManager.registerCommand
to create parameter commands for my plugin and that each argument needs to have a type like number, string and boolean. But what if I wanted to have the user pick an option in a scrollable menu?

Lets say I create a plugin with an array and that array stores created quests made though one of the commands in order for them to edit one of them. So for ease of use you'd want to have the user pick the questname of the quest they wish to edit(Instead of having them type the name manually which may result in typos). Is that possible?
 

KotCR

Villager
Member
Joined
Oct 25, 2014
Messages
13
Reaction score
2
First Language
English
Primarily Uses
Greetings folks, could do with a bit of help with a bit of javascript code I have in one of my old projects.

I was playing through an old MV project of mine for old time's sake the other day, and I stumbled upon a javascript-code related crash; The dreaded "TypeError; Undefined is not a function" error.
The weird thing is, the code used to work no problems when I was originally making the project, so I don't know what happened. I tried disabling all plug-ins (not that there was many in it to begin with), but it hasn't helped. The error still occurs.

The code was essentially used to set-up a shop on an NPC with random items that come at a discounted price; And a certain amount of items are chosen from various item categories. I had the code as a Javascript function from the Event page options in editor.
The code is here:
JavaScript:
var goods = []; var item_dupe = []; var weap_dupe = []; var gear_dupe = []; var scroll_dupe = []; var i = 0;
while (i < 4) { var id = 1 + Math.randomInt(14); var c = Math.round($dataItems[id].price / 3 * 2); var n = item_dupe.includes(id); n == true ? i = i : item_dupe.push(id) && goods.push([0,id,1,c]) && i++; }
while (i < 6) { var id = 1 + Math.randomInt(46); var c = Math.round($dataWeapons[id].price / 3 * 2); var m = weap_dupe.includes(id); m == true ? i = i : weap_dupe.push(id) && goods.push([1,id,1,c]) && i++; }
while (i < 8) { var id = 1 + Math.randomInt(49); var c = Math.round($dataArmors[id].price / 3 * 2); var o = gear_dupe.includes(id); o == true ? i = i : gear_dupe.push(id) && goods.push([2,id,1,c]) && i++; }
while (i < 10) { var id = 15 + Math.randomInt(18); var c = Math.round($dataItems[id].price / 3 * 2); var s = scroll_dupe.includes(id); s == true ? i = i : scroll_dupe.push(id) && goods.push([0,id,1,c]) && i++; }
$gameVariables.setValue(2,goods);

To try and get to the bottom of what was now causing the error, I simplified it a bit to just focus on one category of item, like so:

JavaScript:
var goods = []; var item_dupe = []; var weap_dupe = []; var gear_dupe = []; var scroll_dupe = []; var i = 0;
while (i < 4) { var id = 1 + Math.randomInt(14); var c = Math.round($dataItems[id].price / 3 * 2); var n = item_dupe.includes(id); n == true ? i = i : item_dupe.push(id) && goods.push([0,id,1,c]) && i++; }
$gameVariables.setValue(2,goods);
And after removing pieces of the main code line one-by-one I've figured out that the error is probably caused by the parts of the code relating to the item_dupe array (and presumably this would apply to the weapon_dupe, gear_dupe and scroll_dupe arrays for their code lines too were they still included) that I used to prevent duplicate items being generated in the shop list. When I removed references and checks to that, the code stopped crashing my game.

My current code is:
JavaScript:
var goods = [];
var dupes_i = []; var dupes_w = []; var dupes_a = [];
var i = 0;
while (i < 4) { var id = 1 + Math.randomInt(14); var c = Math.round($dataItems[id].price / 3 * 2); goods.push([0,id,1,c]) && dupes_i.push(id) && i++; }
while (i < 6) { var id = 1 + Math.randomInt(46); var c = Math.round($dataWeapons[id].price / 3 * 2); goods.push([1,id,1,c]) && dupes_w.push(id) && i++; }
while (i < 8) { var id = 1 + Math.randomInt(49); var c = Math.round($dataArmors[id].price / 3 * 2); goods.push([2,id,1,c]) && dupes_a.push(id) && i++; }
while (i < 10) { var id = 15 + Math.randomInt(18); var c = Math.round($dataItems[id].price / 3 * 2); goods.push([0,id,1,c]) && dupes_i.push(id) && i++; }
$gameVariables.setValue(2,goods);
Which mostly works well, unfortunately, removing all references to the _dupes arrays conditional checks has meant duplicate items begin showing up in the shop again, and I'd prefer not to have to keep the duplication-protection removed entirely.

As JavaScript is hardly one of my strong points, with my understanding of the language and it's syntax being very basic; I'm not really sure exactly the best way to go reimplementing the duplication checks in a way that doesn't crash the game. I've tried several methods I've seen with google searches, but RPG Maker doesn't seem to like any of them (resulting in the crash again). Anyone able to help me out to fix up my code snippet to get it working as intended again?

Thankyou in advance for any help.
(Also sorry the code is so messy and compressed to just a few lines, necessary to make it fit into the script box in-engine).
 
Last edited:

ATT_Turan

Forewarner of the Black Wind
Veteran
Joined
Jul 2, 2014
Messages
4,588
Reaction score
2,931
First Language
English
Primarily Uses
RMMV
You have a few odd bits of code, but particularly this part at the end of each chunk:
Code:
goods.push([0,id,1,c]) && dupes_i.push(id) && i++;
You've made a conditional, a boolean operation, that's just sitting there. I don't see how the syntax would be responsible for your error, but I have no clue what you're trying to accomplish instead of using semicolons to make it three code statements.

The contents of the push call could be screwing with you. You don't want brackets around the arguments, because that makes it an array - you're turning goods into a matrix, where the first element is an array containing those four values, so comparing it to id (when you look for it with include()) would always fail.

Does the error stack (F8 -> Coneole) not provide you with the specific line that the code is failing in?
 

BurningOrca

Veteran
Veteran
Joined
Apr 14, 2019
Messages
282
Reaction score
259
First Language
German
Primarily Uses
RMMZ
@ATT_Turan Shop Goods in rpg are a matrix, so that part is ok.
I agree to replace && by ;, however after that {} needs to be placed around the three statements, that where connected with &&.

@KotCR
I think this code should work:
JavaScript:
var goods = [];
var dupes_i = []; var dupes_w = []; var dupes_a = [];
var i = 0;
while (i < 4)
{
    var id = 1 + Math.randomInt(14);
    var c = Math.round($dataItems[id].price / 3 * 2);
    if( !dupes_i.includes(id) )
    {
        goods.push([0,id,1,c]);
        dupes_i.push(id);
        i++;
    }
}
// Do the same with weapons, etc.
$gameVariables.setValue(2,goods);
I've put it on multiple lines, but you could put that back into a single line. However I could not figure out, why your original code did not work at all. I've put it into console and it did work for me!

Edit:
If this still does not work, then propably something is wrong with the includes function and it is undefined for some reason.
 

KotCR

Villager
Member
Joined
Oct 25, 2014
Messages
13
Reaction score
2
First Language
English
Primarily Uses
Hi guys, thanks for the assist, takes alot for me to ask for help (always the strong silent type lol), but I figured, as I spent all night yesterday on the issue, but finally had to go to bed, figured there was no harm in asking and seeing if anybody had any ideas. Gave me something to try out once I had slept and then got back home from work.

Anyway...

@ATT_Turan The code block you quoted does actually work without any errors. It's what I left it at last night (it's near what should be happening, if not exactly what I need to be happening); The only issue is that, unlike my previous code blocks (the ones that do crash the game), it doesn't include the check to avoid the code picking the same item twice when generating the shop's goods array (which is how RPG Maker generates it's shop lists).

I left the pushes to the dupe arrays in there basically to prevent me having to rewrite the code for if we did come up with a solution to include a check on the dupe array contents that doesn't crash the game. That's all; I know without the actual check into a dupe array they essentially do nothing of any value.

As for the && statements; Like I said sorry my javascript knowledge is limited; I was using those instead of ending the code statements with ; so that it'd be easier to integrate what I need it to do once we found the correct terminology for "if item is not in array do these multiple things else do this thing".

My experience with javascript, which honestly is mostly just from building battle damage formula for skills, is that if you use ; and move onto the next code statement within an if statement, it'll ignore everything except the code before the first ; after the ? check (or sometimes even return an "expected : instead of ;" sort of error), so I got into the habit of using && if I wanted to do multiple things so I could contain it before the ; bracket (for e.g. d > 1 ? thing && other thing : thing ; ), but that could just be a quirk with the way the battle skill formula interprets code or simply the way I write my code in those sections.

If you can get away with just using ; all the time though, it'd be good to know because it'd help me keep my code cleaner ^_^.

@BurningOrca

I've put it on multiple lines, but you could put that back into a single line. However I could not figure out, why your original code did not work at all. I've put it into console and it did work for me!
Yeah, it's so weird because like I said, the code used to work with no issue back when I originally created the project which was around half a decade ago now. Haven't really done anything to it since but it just seems to have stopped working for no discernible reason.

What's weird as well, is when I try to call the shop again (without regenerating the random item list), it would also cause a crash. But I got around that by copying the 2 variable to a new variable, and then using the new variable whenever the shop is recalled without regenerating the item list; the original shop call when it first generates the list is still done using variable 2 and that works okay though. It just all seems so inconsistent when years ago it was all working flawlessly.

I tried the code you just suggested, but it results in the same error. I also tried it in a fresh project (just incase the other equally kitbashed script I have elsewhere in this project to do with Escape chance when a certain character is in the party was interfering somehow), but both my old code and your new suggested code here cause the same crash (of course I adjusted the numbers to make sure it could only pick items from the database that actually exist in the fresh project).

The error code "undefined is not a function" in the console is similar in both cases

JavaScript:
TypeError: undefined is not a function
    at Game_Interpreter.eval (eval at <anonymous> (file:///E:/RPG%20Maker/RPGMaker%20MZ%20Projects/Code%20Test/js/rpg_objects.js:10500:10), <anonymous>:2:117)
    at Game_Interpreter.command355 (file:///E:/RPG%20Maker/RPGMaker%20MZ%20Projects/Code%20Test/js/rpg_objects.js:10500:5)
    at Game_Interpreter.executeCommand (file:///E:/RPG%20Maker/RPGMaker%20MZ%20Projects/Code%20Test/js/rpg_objects.js:8930:34)
    at Game_Interpreter.update (file:///E:/RPG%20Maker/RPGMaker%20MZ%20Projects/Code%20Test/js/rpg_objects.js:8838:19)
    at Game_Map.updateInterpreter (file:///E:/RPG%20Maker/RPGMaker%20MZ%20Projects/Code%20Test/js/rpg_objects.js:6115:27)
    at Game_Map.update (file:///E:/RPG%20Maker/RPGMaker%20MZ%20Projects/Code%20Test/js/rpg_objects.js:6022:14)
    at Scene_Map.updateMain (file:///E:/RPG%20Maker/RPGMaker%20MZ%20Projects/Code%20Test/js/rpg_scenes.js:608:14)
    at Scene_Map.updateMainMultiply (file:///E:/RPG%20Maker/RPGMaker%20MZ%20Projects/Code%20Test/js/rpg_scenes.js:600:10)
    at Scene_Map.update (file:///E:/RPG%20Maker/RPGMaker%20MZ%20Projects/Code%20Test/js/rpg_scenes.js:589:10)
    at Function.SceneManager.updateScene (file:///E:/RPG%20Maker/RPGMaker%20MZ%20Projects/Code%20Test/js/rpg_managers.js:2024:25)
Which makes me think you could be right; That for some reason the includes function is undefined in the current version of RPGMaker MV by default for some reason (even though it was included in earlier versions of the engine); In which case the question I suppose then becomes...how do we go about redefining it for the engine?

Or if that's not an easy fix, what alternative ways to code this system would there be?
Maybe rather than checking if an array has the item already and not using it, instead populate all items into an initial array, once each, then move them from that array into the shop array randomly (removing them from the old array in the process so they couldn't be picked again)?
Maybe something along those lines? Would that be possible to do without needing to use the includes function for arrays?
 
Last edited:

Sethorion

Veteran
Veteran
Joined
May 8, 2015
Messages
52
Reaction score
19
First Language
English
Primarily Uses
So you can do that with a few lines of code, but before going through that - you do know there are existing plugins to give you an on-map battle system? If you look for ABS you'll find plenty of stuff already written, which may save you considerable time.
Yeah, I'm aware of some ABS plugins. I want more control over the gameplay mechanics than they currently give me, however. I have a simple, but very specific, system in mind. I'm also learning how to manage large projects while I bulk up my resume, so making it myself will be a good experience.
 

Sethorion

Veteran
Veteran
Joined
May 8, 2015
Messages
52
Reaction score
19
First Language
English
Primarily Uses
So I figured out $gamePlayer and its methods, but I'm struggling to understand Game_Interpreter.prototype.command285. ('Get Location info') How do I use it? It seems to return a boolean, not a table on info for the given map location.
 

ATT_Turan

Forewarner of the Black Wind
Veteran
Joined
Jul 2, 2014
Messages
4,588
Reaction score
2,931
First Language
English
Primarily Uses
RMMV
My experience with javascript, which honestly is mostly just from building battle damage formula for skills, is that if you use ; and move onto the next code statement within an if statement, it'll ignore everything except the code before the first ; after the ? check (or sometimes even return an "expected : instead of ;" sort of error), so I got into the habit of using && if I wanted to do multiple things so I could contain it before the ; bracket (for e.g. d > 1 ? thing && other thing : thing ; ), but that could just be a quirk with the way the battle skill formula interprets code or simply the way I write my code in those sections.
That is...really confusingly worded. The portion of code I quoted was notthe conditional portion of an if statement, nor was it part of a ternary conditional, it was just a statement of code.

With the example you give, ternary operations are not intended to encapsulate multiple statements. If you have an if statement and then you want more than one code statement to be performed based on the result of that evaluation, you use braces:
Code:
if (d>1)
{
    thing;
    other thing;
}

And you can get rid of the line breaks to put that in a damage formula. Using && in random places is a bad habit that could easily make your code malfunction.

You kinda seemed to know this because you have braces around all of those code statements of your while loop, just that last bit is really weirdly written.

The only time I can think of that you'd want to use a boolean operator outside of an if/else condition is if you're checking whether a variable is initialized.



JavaScript:
TypeError: undefined is not a function
    at Game_Interpreter.eval (eval at <anonymous> (file:///E:/RPG%20Maker/RPGMaker%20MZ%20Projects/Code%20Test/js/rpg_objects.js:10500:10), <anonymous>:2:117)
    at Game_Interpreter.command355 (file:///E:/RPG%20Maker/RPGMaker%20MZ%20Projects/Code%20Test/js/rpg_objects.js:10500:5)
    at Game_Interpreter.executeCommand (file:///E:/RPG%20Maker/RPGMaker%20MZ%20Projects/Code%20Test/js/rpg_objects.js:8930:34)
    at Game_Interpreter.update (file:///E:/RPG%20Maker/RPGMaker%20MZ%20Projects/Code%20Test/js/rpg_objects.js:8838:19)
    at Game_Map.updateInterpreter (file:///E:/RPG%20Maker/RPGMaker%20MZ%20Projects/Code%20Test/js/rpg_objects.js:6115:27)
    at Game_Map.update (file:///E:/RPG%20Maker/RPGMaker%20MZ%20Projects/Code%20Test/js/rpg_objects.js:6022:14)
    at Scene_Map.updateMain (file:///E:/RPG%20Maker/RPGMaker%20MZ%20Projects/Code%20Test/js/rpg_scenes.js:608:14)
    at Scene_Map.updateMainMultiply (file:///E:/RPG%20Maker/RPGMaker%20MZ%20Projects/Code%20Test/js/rpg_scenes.js:600:10)
    at Scene_Map.update (file:///E:/RPG%20Maker/RPGMaker%20MZ%20Projects/Code%20Test/js/rpg_scenes.js:589:10)
    at Function.SceneManager.updateScene (file:///E:/RPG%20Maker/RPGMaker%20MZ%20Projects/Code%20Test/js/rpg_managers.js:2024:25)
Which makes me think you could be right; That for some reason the includes function is undefined
That error stack doesn't seem to reference the includes function at all.
in the current version of RPGMaker MV by default
Do you mean MZ? Because your filepath up there is referencing MZ.

So I figured out $gamePlayer and its methods, but I'm struggling to understand Game_Interpreter.prototype.command285. ('Get Location info') How do I use it? It seems to return a boolean, not a table on info for the given map location.
If you read through the rest of it, you can see where it's using elements of params (the argument(s) passed into the function) to set values in game variables.

Working your way up from the bottom line, the last thing it does is set game variable params[0] to value, so the first argument is the variable ID you'd want there.

The switch statement above that is looking at the next argument to determine what information about the target location is being placed in that variable. Etc. The function does a variety of different things depending on what you pass into it.
 

KotCR

Villager
Member
Joined
Oct 25, 2014
Messages
13
Reaction score
2
First Language
English
Primarily Uses
Do you mean MZ? Because your filepath up there is referencing MZ.
Nah, I mean MV. I don't have an MV project folder anymore seems for the most part I just use MZ these days (and the project I'm trying to fix is from an old storage device). I just created a fresh project to test BurningOrca's suggested code snippet in MV that I created in the same folder I use for my MZ projects, if that makes sense. Which is where that error is from. But the error that comes up in the console on the original project is similar to that; I just tested the code in a fresh project second which is why that's the one I quoted.

The portion of code I quoted was notthe conditional portion of an if statement, nor was it part of a ternary conditional, it was just a statement of code.
No I know that, but the part of the code you quoted was the tail-end of the working code that I ended up with (the complete version of which does not include the duplication check), not the original code that was crashing the game. Basically I'm saying, my bad coding standards being hard to read aside perhaps, that nothing in the part of the code you quoted is causing any issues.

That error stack doesn't seem to reference the includes function at all.
Once again, my understanding of these things is limited, but I was under the impression it doesn't reference the includes function because it doesn't think it exists? It just complains about an "undefined" right at the start, with "undefined" being the includes function; Then it's checking all those JS methods/files in the console lines and not finding it in any of them?

Unsure if that interpretation is correct, but what I do know, is that when you remove any attempts to use the includes function in the code, then it seems to work. Unfortunately, originally I was using the includes method to check the dupe arrays' contents to see if it was okay to include a specific item in the shop's array. Which at one point worked fine but apparently doesn't anymore.
 
Last edited:

ATT_Turan

Forewarner of the Black Wind
Veteran
Joined
Jul 2, 2014
Messages
4,588
Reaction score
2,931
First Language
English
Primarily Uses
RMMV
Unsure if that interpretation is correct, but what I do know, is that when you remove any attempts to use the includes function in the code, then it seems to work. Unfortunately, originally I was using the includes method to check the dupe arrays' contents to see if it was okay to include a specific item in the shop's array. Which at one point worked fine but apparently doesn't anymore.
Well, that's easy to test. Play test your game, hit F8, go to the console, and type in:
Code:
var test=[1, 2, 3];
test.includes(2);

The Array.includes() method is a basic JavaScript function, so if your game isn't recognizing it, you have some corrupted libraries or something. Try some of this stuff in a new project, or uninstall/reinstall the editor...I'm not sure what else you could have on your system that would cause that.
 

Sethorion

Veteran
Veteran
Joined
May 8, 2015
Messages
52
Reaction score
19
First Language
English
Primarily Uses
If you read through the rest of it, you can see where it's using elements of params (the argument(s) passed into the function) to set values in game variables.

Working your way up from the bottom line, the last thing it does is set game variable params[0] to value, so the first argument is the variable ID you'd want there.

The switch statement above that is looking at the next argument to determine what information about the target location is being placed in that variable. Etc. The function does a variety of different things depending on what you pass into it.
Ah, that makes sense. Rather than alter the information for that location, how can I just retrieve information? Like the events that are there, for instance? Eventually, command285 might be all I need, but only if that's all I have to work with.
 

ATT_Turan

Forewarner of the Black Wind
Veteran
Joined
Jul 2, 2014
Messages
4,588
Reaction score
2,931
First Language
English
Primarily Uses
RMMV
Ah, that makes sense. Rather than alter the information for that location, how can I just retrieve information?
I think you're misunderstanding something - that's all the function does, is retrieve information. It does not allow you to alter anything on the tile.
Like the events that are there, for instance?
It's right there in the switch statement for params[1]: case 1: // Event ID

So if you did $gameMap._interpreter.command285(9, 1, 0, 56, 42); it would put the event ID present on tile 56, 42 into game variable 9.
 

Sethorion

Veteran
Veteran
Joined
May 8, 2015
Messages
52
Reaction score
19
First Language
English
Primarily Uses
Ah, that helps to understand. I'm pretty rough on my understanding of js still.

Also, I can't see how to get character battler stats (including HP) from $gamePlayer. If that's not smart, I can just initialize those stats directly into $gamePlayer at launch. I'm a little worried about plugin users who might want to use the built-in stats sytem&UI though.

EDIT: I just now figured out that I can use $gameParty.leader() to get the main player actor, lol. That will help.
 
Last edited:

Candacis

Pixel Pusher
Restaff
Joined
May 2, 2012
Messages
1,751
Reaction score
2,804
First Language
German
Primarily Uses
I know it exists, but I can't find it again.
I want to display a picture above my player, like 48px above the head.
Now I have the 2 variables for screen x of player and screen y of player. But I think, I need to modify the screen y and have a script there that modifies that value so that the picture isn't centered exactly on the player's sprite but above it.
Can anyone quickly help me out?
 

ATT_Turan

Forewarner of the Black Wind
Veteran
Joined
Jul 2, 2014
Messages
4,588
Reaction score
2,931
First Language
English
Primarily Uses
RMMV
Now I have the 2 variables for screen x of player and screen y of player. But I think, I need to modify the screen y and have a script there that modifies that value so that the picture isn't centered exactly on the player's sprite but above it.
There's no need for anything involving JavaScript that I can see - if you have the player's X and Y in variables, just use event commands to subtract whatever amount from the Y variable to make it appear higher on the screen (0, 0 is the upper-left corner).
 

Candacis

Pixel Pusher
Restaff
Joined
May 2, 2012
Messages
1,751
Reaction score
2,804
First Language
German
Primarily Uses
Thanks, I guess that could work, but I thought there was a script call that I could use so that I have it directly when I first set the variable. So, I don't have to adjust it separatly.
 

Latest Threads

Latest Profile Posts

John 3:16 “For God so loved the world, that he gave his only Son, that whoever believes in him should not perish but have eternal life.
Happy treason day, Colonial friends <3

I am officially amused. One of the NPCs in my project is basically meant as the guy you check in with to point you where you should go next, including if you’ve been away for a bit and have forgotten.

I can conclusively say he does his job nicely. I was out of town over the weekend. I had forgotten what I was working on, so I checked what I had last done with him for a reminder. ^.^
While talking about anime with a friend got the idea of doing this. :kaopride:
4m6nhlF.jpg

Character: Mirajane Strauss from Fairy Tail.

Forum statistics

Threads
123,340
Messages
1,155,886
Members
161,769
Latest member
CrewMimo
Top