JavaScript questions that don't deserve their own thread

Kind of hacking together several MV YEP tips and tricks here, but I've clearly missed something as it seems to be inoperative. Can someone diagnose it?

This is a passive state that, at combat start, picks a random buff and applies it to a random party member:
JavaScript:
<Custom Battle Effect>
// This is the number of turns the buffs and debuffs will last
var turns = 4;
// Create a pool for the parameters that can be changed
var allowedParams = [];
// Insert Param ID's you want to randomly add here:
allowedParams.push(2, 4, 6, 7);
// Make a copy of the pool
var result = allowedParams.slice();

// Initialize the parameter ID
var paramId;
// Loop through each of the copied pool's parameters
while (result.length > 0) {
  // Get a randomly looped parameter
  paramId = result[Math.floor(Math.random() * result.length)];
  // Remove it from the copied pool
  result.splice(result.indexOf(paramId), 1);
  var members = [];
    // Loop through each of the target's alive allies.
    for (var i = 0; i < target.friendsUnit().aliveMembers().length; ++i) {
      // Set the potential variable to the current looped ally.
      var potential = target.friendsUnit().aliveMembers()[i];
      // If the ally doesn't exist, move onto the next.
      if (!potential) continue;
      // If the ally's HP is at zero (hence dead), move onto the next.
      if (potential.hp <= 0) continue;
      // Now that all checks have been cleared, put the potential ally into the members array.
      members.push(potential);
    }
    // Get a random member of the members array.
    var member = members[Math.floor(Math.random() * members.length)];
    // Check if the random member exists.
    if (member) {member.addBuff(paramId, turns); }
break;
  }
}
</Custom Battle Effect>
There's also probably a simpler way of doing this but this one made most sense to me.
Hmmm, it's not 100% clear to me what you're attempting with that code, but if you just wanna add a random buff (selected from that list you give, not all params), I think it's just:

<Custom Battle Effect>
var turns = 4;
var allowedParams = [2, 4, 6, 7];
var aliveMembers = $gameParty.aliveMembers();
var battler = aliveMembers[Math.randomInt(aliveMembers.length)];
var paramId = allowedParams[Math.randomInt(allowedParams.length)];
battler.addBuff(paramId, turns);
</Custom Battle Effect>
Hmmm, it's not 100% clear to me what you're attempting with that code, but if you just wanna add a random buff (selected from that list you give, not all params), I think it's just:

<Custom Battle Effect>
var turns = 4;
var allowedParams = [2, 4, 6, 7];
var aliveMembers = $gameParty.aliveMembers();
var battler = aliveMembers[Math.randomInt(aliveMembers.length)];
var paramId = allowedParams[Math.randomInt(allowedParams.length)];
battler.addBuff(paramId, turns);
</Custom Battle Effect>
Thanks a lot, that works fine. Knew I was massively overcomplicating it. 😅
How can I prevent pageup and pagedown from changing options in the options window? I'm using the Adventure Menu plugin and changing the tabs on it changes the selected option as well.
Well if that plugin is still using the options window, you can check what the methods
Window_Options.prototype.cursorPagedown
Window_Options.prototype.cursorPageup
Window_Options.prototype.processPageup
Window_Options.prototype.processPagedown
are doing and overwrite them.

EDIT: or what handlers the scene is attaching to them
Hello! I have a javascript question in relation to writing a plugin.

I am currently attempting to translate a maze maker/room randomizer script i wrote in C# into javascript as a plugin, and there are a few core concepts I am unsure how i translate from C# to the plugin.

namely, my C# script consisted of 1 the main class, 3 structs (2 of which have some helper functions), and a custom enum for directions (north,east, south, west). following some of the other threads of plugin development, plus looking at the inner workings of the other plugins I've downloaded I have the basic shell of what I want, but don't really know how I am supposed to translate the structs (especially with their helper functions) or the enum to the javascript plugin, or even if that is possible.
This probably is complex enough for a dedicated thread, but briefly:
don't really know how I am supposed to translate the structs (especially with their helper functions)
You'd just make them into an object type. You could also use JavaScript's more complex class functionality, but you can just look at any of the data types that already exist in RPG Maker in rmmz_objects.js.

If you make a thread asking for more help, you'd need to give examples of what specifically you're having a hard time translating, as basically all of MZ's code is based around its custom data types and referencing their member properties and methods.

So if you're at all familiar with the codebase, it's not immediately apparent what the confusion would be, aside from changing your syntax from C# to JS.

or the enum
JavaScript doesn't have an enum equivalent, but why can't you just declare them? So where you might have
Code:
enum Months 
{
    January,
    February,
    March
};
you can simply say
Code:
const January = 0, February = 1, March = 2;
or if you want them encapsulated within a data type, you just declare them there.
Code:
BattleManager.initMembers = function()
{
    this._January = 0;
    this._February = 1;
    this._March = 2;
etc.
i am not the most familiar with the codebase or javascript just yet, I am kinda using this code porting instance to figure things out.

I'll prolly make a full thread for any further issues, but to start, using just the smallest struct as example, is this correct?

here is my mazedata struct (used in my system to be able to generate/hold multiple randomized layouts at once) in c#

C#:
public struct MazeData

{

    public string MazeName;

    public Vector2Int MazeSize;

    public bool LoopVertically;

    public bool LoopHorizontally;

    public List<MazeRoom> MazeRooms; //maze room is one of the other 2 structs in this system

    public int DefaultRooms;

    public MazeRoom[,] MazeLayout; //2d array of the maze

    public List<MazeConnection> MazeConnections; //maze connection is one of the other 2 structs in this system

    public List<MazeRoom> RoomsUnplaced;

    public List<MazeRoom> RoomsPlaced;

    public int MazeRem;

}

is this correct in translating it to javascript?

JavaScript:
//-----------------------------------------------------------------------------
// Maze_Data
//
// The object holding the main data for a maze

function Maze_Data() {
    this.initialize(...arguments);
}

Maze_Data.prototype.initialize = function() {
    this._name = "test";
    this._mapID = arguments.mapId; //I know rpgmaker maps have a map id, need to get that somehow...
    this._mazeWidth = 0; //i am pretty sure javascript does not have vector2?
    this._mazeHeight = 0;
    this._loopVertically = false;
    this._loopHorizontally = false;
    this._mazeRooms = [];
    this._DefaultRoom = 0;
    this._MazeLayout = [this._mazeWidth, this._mazeHeight]; //am unsure if this is how a 2d array is made or not
    this._MazeConnections = [];
    this._RoomsUnplaced = [];
    this._RoomsPlaced = [];

    this._MazeRem = 0;
};

if so, that should make translating the other 2 structs fairly easy. if not, what did I get wrong?

JavaScript doesn't have an enum equivalent, but why can't you just declare them? So where you might have
Code:
enum Months
{
January,
February,
March
};
you can simply say
Code:
const January = 0, February = 1, March = 2;
I think i can do that. the directional enum is used mostly for efficiency of a switch statement over 4 if-elses in my C# code, and for input for which direction to go in the maze (so the plugin command for going to the next room can just take the input of the current room ID + direction). so 4 const numbers like that should still work for what i need.

I am however currently unsure where to declare those consts in my plugin, since they do get used in both the main script (obviously) and some of the others structs helper scripts
i am not the most familiar with the codebase or javascript just yet, I am kinda using this code porting instance to figure things out.
I would suggest that actually familiarizing yourself with a new language and trying to learn about the codebase you're trying to integrate code into makes more sense as a first step, as opposed to trying to adapt existing code of any complexity.

As a basic example, if you don't know how RPG Maker works, handles map data and displays it graphically, how is your maze generator going to accomplish anything in the context of a game?

It might be more reasonable to do some small plugin that modifies a part of your game, or look at how the existing codebase and plugins for it are written.

I am however currently unsure where to declare those consts in my plugin, since they do get used in both the main script (obviously) and some of the others structs helper scripts
You can simply declare them outside of any other code blocks as in my first example above and they'll be global variables. I guess this is not a thing in C#, although it was in C/++.

There are other ways to do it by making them properties of the window or a custom namespace or your Maze_Data datatype.
I would suggest that actually familiarizing yourself with a new language and trying to learn about the codebase you're trying to integrate code into makes more sense as a first step, as opposed to trying to adapt existing code of any complexity.
I have to personally disagree. not because there is anything wrong with that way of learning, but it simply doesn't jive with how I learn or figure things out. to me, taking code of something I wrote and understand in one language, and figuring out how to make that exact same thing in another language is how I am trying to learn. it gives a distinct project, with actual motivation to complete it, and a clear end goal (successfully translating the functionality of the system from one state to another).

As a basic example, if you don't know how RPG Maker works, handles map data and displays it graphically, how is your maze generator going to accomplish anything in the context of a game?
after having made my C# prototype I kinda realized "maze generator" was maybe an inaccurate term for what i wanted. a better term was maybe a dungeon randomizer? specifically for allowing randomized player transfer calls without hard coding anything while still making a functional dungeon with the parameters I desire. so instead of using player transfer with the input data of the mapid of the destination map and the x,y coordinates, I can call my plugin and give it a direction*, and it grabs the current mapID, figures out what room is in the corrisponding direction in its 2D array, and go to that room, deriving the X,Y postions from region IDs

*(the enum originally, but i have already seen from other plugins I can make the parameters a hard list of set choices and then just connect those to those const to effectively keep the same functionality)

there are defininietely some parts in that setup I do not know how to do in rpgmaker yet, I won't pretend I do, but that is literally what I am in the forums asking about. things I don't know yet.

like literally, my next question for the Maze_Data datatype was the exact syntax for handing it a mapID in its constructor, and from that how to pull out the data I need from a map, since I know its possible, i just don't know how.

It might be more reasonable to do some small plugin that modifies a part of your game, or look at how the existing codebase and plugins for it are written
that is literally what I am doing tho? I don't conseve of this as a particually big plugin. like its big in terms of code, but its functionality and purpose is fairly simple.

and I have infact been looking at the codebase for the plugins to get as far as i have been now. I didn't realize I also could see like the main codebase till you mentioned checking rmmz_objects, but now that I know that I can look at that too.

You can simply declare them outside of any other code blocks as in my first example above and they'll be global variables. I guess this is not a thing in C#, although it was in C/++.
you can declare them in a class in C#. the way i have them written in my above C# code for the stuct is the same as a class.

I think the thing I am being thrown off is possibly scoping differences between C# and javascript. in C# declaring a random varible like an int or whatever outside the scope of something like a class or struct or what have you is kinda not a thing (or if it is a thing I ain't ever seen it).
Hi, is it possible at the begining of a plugin to load in a variable some data from the data files (ex: items.json).
And if yes, how ? ^^
Cause I tried to just console.log() the data and it didn't work. So I would like to know how to acces it.

I tried this console.log($dataEnemies)

Thank you.
I tried this console.log($dataEnemies)
That's slightly tricky. if youre trying to immediately access it when all the plugins are being initially loaded, then itll come up null. You normally want to alias DataManager.isDatabaseLoaded() and use it to run your script only when the database data has been loaded
Hopefully this is a quick one, just making sure I'm not being dumb, but if it is more complex than I think, I'll make a topic.

I have Eli_ExpByLevel and YEP_EnemyLevels. I want Eli_ExpByLevel to reference YEP's generated enemy level, since it's a random range based on the map and metatags in the enemy itself (but by default, Eli is a static metatag integer value). I want ExpByLevel because I want XP to vary based on comparitive player and enemy levels (it's flat for YEP).

YEP does provide an enemy.level JS function. Eli's get level code is:
Code:
Game_Enemy.prototype.getLevel = function(){
    if(Imported.Eli_EnemyClass && this._classId > 0){
        return this.getLevelByClass()
    }else{
        return this.getLevelByMeta()
    }
}
So if I change Imported.Eli_EnemyClass to Imported.YEP_EnemyLevels and then the first return to return this.enemy.level() That should be correct in retrieving that, right?

I may have to also delete the this_classId since I'm not 100% sure if YEP_X_EnemyBaseParam actually assigns a class or just bases stats on class curves (probably the latter, since skills are assigned seperately for AI Core).

Does that all sound right or am I missing something?
If it's for your own game, you don't need to keep any if imported checks etc. Just make it return what you want.

I think it's this.level (not this.enemy.level)
That's slightly tricky. if youre trying to immediately access it when all the plugins are being initially loaded, then itll come up null. You normally want to alias DataManager.isDatabaseLoaded() and use it to run your script only when the database data has been loaded
Thx a lot for your answer.
If it's for your own game, you don't need to keep any if imported checks etc. Just make it return what you want.

I think it's this.level (not this.enemy.level)
Ty, it was indeed this.level

I actually changed the entire lot to shuffle the metatag function in there just in case I need an override later, the final result being:
Code:
Game_Enemy.prototype.getLevel = function(){
    const meta = this.enemy().meta

    if(meta.hasOwnProperty('OverrideXPLevel')){
        return Number(Eli.Utils.convertEscapeVariablesOnly(meta.OverrideXPLevel))
    }else{
        return this.level
    }
}
Hi, a little problem with the aliasing, I must do something wrong I guess. I've follow the aliasing part from here.

So come up with this;

JavaScript:
var Test = Test || {};
Test.pluginName = "Test";
Test.parameters = PluginManager.parameters(Test.pluginName);

Test.DataBaseLoaded = DataManager.isDatabaseLoaded;

DataManager.isDatabaseLoaded = function() {
    Test.DataBaseLoaded.apply(this,arguments)
};
This doesn't work. Even without adding a console.log() for the pre or post. If I do, the console.log() loop and the game get in an infinite loading.

The override works fine but I would like to learn to alias properly ^^
The override works fine but I would like to learn to alias properly ^^
You can try this
Code:
var Test = Test || {};
Test.DataBaseLoaded = DataManager.isDatabaseLoaded;
DataManager.isDatabaseLoaded = function() {
    if (!Test.DataBaseLoaded.call(this)) return false;
    if (!Test._loaded) {
        console.log($dataEnemies); //do stuff here
        Test._loaded = true;
    }
    return true;
};
Thx a lot, I guess it didn't work cause the alias don't "return" correctly so it just keep getting load. Is that right ?
Do Yanfly's Move Route Core commands work with if statements?

I was using if (this.x == 19) {SELF SWITCH B: ON;} in a movement route, but it seems like that command is immediately parsed and executed regardless of the condition. I didn't see any commands for if statements in the documentation for the extension plugin either.

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