JavaScript questions that don't deserve their own thread

ATT_Turan

Forewarner of the Black Wind
Veteran
Joined
Jul 2, 2014
Messages
7,555
Reaction score
5,324
First Language
English
Primarily Uses
RMMV
Hello, I'd like to sort actors' skills in Window_SkillList/BattleSkill by MP cost. Can someone help me? I tried something like SceneManager._scene._skillWindow.sort(function(a, b) ... but I still can't get it right.
That sort() syntax you're referencing is a JavaScript method for arrays. The skillWindow isn't an array, it's its own Object of type Window_SkillList.

You can try applying that to the portion of the window's data that is an array. Try modifying the makeItemList() method from rpg_windows.js:
Code:
Window_SkillList.prototype.makeItemList = function() {
    if (this._actor) {
        this._data = this._actor.skills().filter(function(item) {
            return this.includes(item);
        }, this);
        this._data.sort((a, b) => $dataSkills[a].mpCost-$dataSkills[b].mpCost);
    } else {
        this._data = [];
    }
};

See how that works.
 

poppicha

Villager
Member
Joined
Aug 30, 2015
Messages
28
Reaction score
16
First Language
Thai
Primarily Uses
RMMV
That sort() syntax you're referencing is a JavaScript method for arrays. The skillWindow isn't an array, it's its own Object of type Window_SkillList.

You can try applying that to the portion of the window's data that is an array. Try modifying the makeItemList() method from rpg_windows.js:
Code:
Window_SkillList.prototype.makeItemList = function() {
    if (this._actor) {
        this._data = this._actor.skills().filter(function(item) {
            return this.includes(item);
        }, this);
        this._data.sort((a, b) => $dataSkills[a].mpCost-$dataSkills[b].mpCost);
    } else {
        this._data = [];
    }
};

See how that works.
Thank you! It works like a charm! But I need to remove $dataSkills to be like this.
JavaScript:
this._data.sort((a, b) => a.mpCost - b.mpCost);
Anyway Thank you!!!
 

Flame_Effigy

Villager
Member
Joined
Dec 21, 2020
Messages
15
Reaction score
5
First Language
English
Primarily Uses
RMMV
What's the correct way to make a state change the affected actor's SV battle coodinates?
 

ATT_Turan

Forewarner of the Black Wind
Veteran
Joined
Jul 2, 2014
Messages
7,555
Reaction score
5,324
First Language
English
Primarily Uses
RMMV
What's the correct way to make a state change the affected actor's SV battle coodinates?
Try calling the startMove() method from a state using a Custom Apply Effect from Yanfly's Buffs & States Core.
 

Flame_Effigy

Villager
Member
Joined
Dec 21, 2020
Messages
15
Reaction score
5
First Language
English
Primarily Uses
RMMV
Would would change the actor's actual position? Or would that just change how much they step forward at the start of their turn?
Essentially I have a state and when applied I want the affected actor to move to a selected point on the screen and stay there until the state is removed.
 

Cynwale

Villager
Member
Joined
Nov 27, 2022
Messages
16
Reaction score
1
First Language
French
Primarily Uses
RMMV
Hello, I'm using the plugin VictorEngine - Fog and Overlay plugin for adding fogs to maps. I like this one because of the notetag script directly onto maps, I'm trying to have fog, but only on a certain part of the map.

Capture.PNG

Would it be possible or do I need another plugin?
 

poppicha

Villager
Member
Joined
Aug 30, 2015
Messages
28
Reaction score
16
First Language
Thai
Primarily Uses
RMMV
Hello, I want to do a sort array like this
JavaScript:
//Old
let arr = [0,15,2,0,1];
//to be like this
[15,2,1,0,0]

For sort 0 to last I already got it with this
JavaScript:
arr.sort(function(a, b) {
    return (a==0)-(b==0) ||-(a<b);
});
//but the order will become like this.
[1,2,15,0,0]

//I want it to be like this
[15,2,1,0,0]

Anyone have any suggestions?
 
Last edited:

Arthran

Veteran
Veteran
Joined
Jun 25, 2021
Messages
862
Reaction score
1,066
First Language
English
Primarily Uses
RMMZ
Hello, I want to do a sort array like this
JavaScript:
//Old
let arr = [0,15,2,0,1];
//to be like this
[15,2,1,0,0]

For sort 0 to last I already got it with this
JavaScript:
arr.sort(function(a, b) {
    return (a==0)-(b==0) ||-(a<b);
});
//but the order will become like this.
[1,2,15,0,0]

//I want it to be like this
[15,2,1,0,0]

Anyone have any suggestions?

For descending order, you could do:
JavaScript:
arr.sort((a, b) => b - a);
 

poppicha

Villager
Member
Joined
Aug 30, 2015
Messages
28
Reaction score
16
First Language
Thai
Primarily Uses
RMMV
Oh, Sorry for being not show clear example ;_;
I don't mean to sort decending order
this array in my game is character slot (actorId)
New array example is like this
JavaScript:
//the number is actorId & 0 means no actor in that slot
battleMember = [0,15,0,11,21,2,1,8];

//be like this.
[15,11,21,2,1,8,0,0];
Consider changing the order to the previous position but keep the same back/front order
something like that.
 

Arthran

Veteran
Veteran
Joined
Jun 25, 2021
Messages
862
Reaction score
1,066
First Language
English
Primarily Uses
RMMZ
Oh, Sorry for being not show clear example ;_;
I don't mean to sort decending order
this array in my game is character slot (actorId)
New array example is like this
JavaScript:
//the number is actorId & 0 means no actor in that slot
battleMember = [0,15,0,11,21,2,1,8];

//be like this.
[15,11,21,2,1,8,0,0];
Consider changing the order to the previous position but keep the same back/front order
something like that.
Sorry, I didn't read your original post carefully enough. If I'm understanding correctly, you want to keep the non-zero digits in the same order, but you want to move all the zeroes to the end? If so, this should work:
JavaScript:
battleMember.sort((a, b) => {
    if (a == 0)
        return 1;
    if (b == 0)
        return -1;
    return 0;
});

*edit* The code I had posted wasn't compatible with all browsers. Now it is fixed.
 
Last edited:

poppicha

Villager
Member
Joined
Aug 30, 2015
Messages
28
Reaction score
16
First Language
Thai
Primarily Uses
RMMV
Umm... It's not working...?
uummm.png
 

Arthran

Veteran
Veteran
Joined
Jun 25, 2021
Messages
862
Reaction score
1,066
First Language
English
Primarily Uses
RMMZ
Umm... It's not working...?
View attachment 247107
Sorry. The way I was doing it works fine on Firefox, but apparently it fails on Chrome. Try this one instead:
JavaScript:
battleMember.sort((a, b) => {
    if (a == 0)
        return 1;
    if (b == 0)
        return -1;
    return 0;
});

I'm going to edit my other post, to prevent anyone else from being mislead in the event that they stumble across it.
 
Last edited:

ATT_Turan

Forewarner of the Black Wind
Veteran
Joined
Jul 2, 2014
Messages
7,555
Reaction score
5,324
First Language
English
Primarily Uses
RMMV
Hello, I'm using the plugin...
Hi, there! From the first post in this thread:
If you need help getting a plugin working, please start a NEW thread and include a link to where you downloaded the plugin from.

You're not asking anything about JavaScript directly.

However,
Would it be possible or do I need another plugin?
Per its description, the Victor plugin creates an overlay, which is an image that gets applied to the screen. You would need to look for some other kind of effects plugin to display it to selective portions of the map.
 

Shashmurin

Villager
Member
Joined
Jan 7, 2023
Messages
6
Reaction score
0
First Language
Finnish
Primarily Uses
RMMZ
-Made a new topic as per topic rule

 
Last edited:

sleepy_sealion

Need to work harder!
Veteran
Joined
Jan 4, 2018
Messages
256
Reaction score
797
First Language
English
Primarily Uses
RMMV
Hi, I'm slowly getting back to things and I was going over some older scripts. I noticed a lot of the time, I will have script calls called from Scene_Base to do tasks for everything. I guess cause it's easier to call Scene_Base."function name" for script calls, and was wondering if that's really an optimal way to go about things?

Was also curious about how most people reference scene's like scene_battle. My work around was to have the scene set a "$sceneBattle" variable to itself when it starts up. And was wondering again if there might be a more optimal way to reference scenes in general. Sorry if it's a dumb question.
 

ATT_Turan

Forewarner of the Black Wind
Veteran
Joined
Jul 2, 2014
Messages
7,555
Reaction score
5,324
First Language
English
Primarily Uses
RMMV
I noticed a lot of the time, I will have script calls called from Scene_Base to do tasks for everything...was wondering if that's really an optimal way to go about things? Was also curious about how most people reference scene's like scene_battle.
It's not a dumb question, but I'm not sure I entirely understand what the question is. So I'll give a bit of an explanation, and hopefully it will answer your question, and if not it will allow you to reword yourself more clearly.

These words you're referencing (Scene_Base, Scene_Battle) are just JavaScript Objects. An Object is a data type you can use (declare variables of) that contains other things, like variables and methods (contained functions).

So, for example, if you create let thisScene = new Scene_Base you can then reference thisScene.x for that scene's x coordinate.

Scene_Battle, and all the other scenes in RPG Maker, are inherited from Scene_Base. That means that they will automatically have all of the properties of a Scene_Base unless you define a new version for that specific type.

So you could create an "x" method for Scene_Battle that didn't return x coordinates but some other value. In this specific example, that would be pretty silly, but it's the general idea.

You can open up the rpg_scenes.js file and just read through it to see examples. One of the best ones is the create() method. You'll see Scene_Base's create() method is empty, because you never actually create a scene of the type Scene_Base. All the other scenes have their own create() method that creates the specific windows that make up that scene.

So I suppose a possible answer to your question is you would rarely/never want to specifically call a function from Scene_Base. You would pretty much always want to use the method that's a part of the actual scene you're in now.
 

DrBuni

Veteran
Veteran
Joined
Dec 27, 2020
Messages
210
Reaction score
130
First Language
.
Primarily Uses
RMMV
Edit: Making a new thread as per thread rules.
 

Arthran

Veteran
Veteran
Joined
Jun 25, 2021
Messages
862
Reaction score
1,066
First Language
English
Primarily Uses
RMMZ
Hi, I'm slowly getting back to things and I was going over some older scripts. I noticed a lot of the time, I will have script calls called from Scene_Base to do tasks for everything. I guess cause it's easier to call Scene_Base."function name" for script calls, and was wondering if that's really an optimal way to go about things?

Was also curious about how most people reference scene's like scene_battle. My work around was to have the scene set a "$sceneBattle" variable to itself when it starts up. And was wondering again if there might be a more optimal way to reference scenes in general. Sorry if it's a dumb question.
Like ATT_Turan said, there shouldn't be a lot of scenarios where you're directly calling methods from Scene_Base, but without seeing actual examples of what you're talking about, it's hard to say for sure whether what you're doing is right or wrong. But generally speaking, the only time you should be doing that is when you're creating your own scene, and you want to add additional code to a method that was inherited from Scene_Base. For example:

JavaScript:
Scene_Title.prototype.update = function() {
    if (!this.isBusy()) {
        this._commandWindow.open();
    }
    Scene_Base.prototype.update.call(this);
};

As far as referencing the battle scene and whatnot goes, you can usually just use SceneManager._scene to reference the current scene, since any time you're going to be trying to reference an instantiated scene object, it's probably going to be the currently active scene. But it still really shouldn't be a super common thing for you to be referencing instantiated scene objects from outside of the scene itself (in which case, you'd just need to reference it with this).

If you're going to do things like setting it to a global $sceneBattle variable, then you also need to make sure that you clear that reference (i.e. $sceneBattle = null;) when the scene is done, otherwise the garbage collector won't be able to free up the memory that the scene was using.

SRD_HUDMAKER always spawns in a very unfortunate position. I tried moving it around with windows + shift + arrow keys, but it never leaves the top of the screen. The plugin basically became non-functional at this point. Even if I disconnect my second monitor, it still spawns weird in the main monitor. Please, help me.
A question regarding a specific plugin always deserves its own thread.
 
Last edited:

sleepy_sealion

Need to work harder!
Veteran
Joined
Jan 4, 2018
Messages
256
Reaction score
797
First Language
English
Primarily Uses
RMMV
Thanks for the tips guys! I guess I'll stop trying to use Scene_Base then for everything. For an example though, I was hoping I could use it as an quick way to code out stuff from the same place, Like for example:
Code:
Scene_Base.restoreMovementDefaults = function() {
 $gamePlayer._moveSpeed = 4;
 $gamePlayer._through = false;
 $gamePlayer._walkAnime = true;
};
Though I guess it would be simpler to just have $gamePlayer call that one in particular.

I was trying out just having functions that don't belong to an object, but I do remember hearing that it's not ideal. I think I'm just over complicating things again. But I guess the kind of goal was to create a "core" script that I could easily call all these functions and stuff.
Illustration2.png
 

Latest Threads

Latest Profile Posts

Slowly but surely the game dev streams accomplish the goal.... actually finishing the game. :LZSexcite:



Going to start in about 15 minutes or so for anybody interested. And of course you know you are, well at least you know now because it's been stated that you are, and obviously anything randomly stated on the Internet must be true right? :LZSwink:
Rag-Tag Team Unite!!!
Fantast4.png

The questionable end result of a fun shared desing challenge together with @spillycat xD
9 days left for Harold Jam... hope it's not too late to start gam mak
i think this girl has to be my favorite though :)
62702017_JJKXazrs2LInoZh.png

Forum statistics

Threads
129,750
Messages
1,204,819
Members
170,836
Latest member
BuckingLotus
Top