JavaScript questions that don't deserve their own thread

Piyan Glupak

Veteran
Veteran
Joined
Nov 14, 2016
Messages
126
Reaction score
63
First Language
English
If I use:

this.addState(VAR);

to apply a state to an actor, do I need to take some action to refresh the actor's data structure?

I am writing a gender plugin that sets gender and applies states automatically to male and female actors. You can set gender either by notetag or by plugin command. (The idea of the plugin commands is for games that allow the player to customise their character at the start of a new game.)

When testing with an event that uses the plugin commands to change the gender of an actor, I found that the first change of gender for an actor always applies the state selected in the parameters after setting gender. Rlepeated changes of gender without moving the party don't. If I change an actor's gender, move the party a very short distance and return to the event, then change an actor's gender again, the state is applied as intended.

It seems to me that some kind of update or refresh happens whenever the party is moved. Is there anything that I could include to make adding the state more reliable?

The bit that I use to do the gender change is below:

Game_Actor.prototype.changeGender = function (newGender) { this.removeState(maleActorState); this.removeState(femaleActorState); this.gender = newGender; if (this.gender == 2) { this.addState(maleActorState); }; if (this.gender == 3) { this.addState(femaleActorState); }; };
 

ATT_Turan

Forewarner of the Black Wind
Veteran
Joined
Jul 2, 2014
Messages
2,833
Reaction score
1,659
First Language
English
Primarily Uses
RMMV
If I use:

this.addState(VAR);

to apply a state to an actor, do I need to take some action to refresh the actor's data structure?
Yes, we don't need the whole rest of the function - you should be able to just do this.refresh()
 

Piyan Glupak

Veteran
Veteran
Joined
Nov 14, 2016
Messages
126
Reaction score
63
First Language
English
@ATT_Turan Thank you for your reply. I appreciate your time and effort to try and help me.

I tried:

this.refresh();

after removing the old states that might apply to the actor, after adding states (inside and outside of the if statement brackets) and even:

$gameActors.actor(id).refresh();

in the plugin commands that call changeGender. Still the same, I am afraid.

I might have to spend a bit of time over the next few days looking at the default javascript files to see what I can find.

Thanks again for your reply.
 

Piyan Glupak

Veteran
Veteran
Joined
Nov 14, 2016
Messages
126
Reaction score
63
First Language
English
@ATT_Turan - Thanks for your reply, but it still the same, even with a $gamePlayer.refresh(); sandwiched between a pair of this.refresh(); before and after the old states are removed, and after the new state is added.

All other plugins are turned off on the project I use for testing and development. The male female states just show icons.

Edit: I have just tried making a new project in case the one that I have been using for development was an older version of MV. Still the same.

Edit2: This is a plugin that I can't put out for other people because I nicked a lot of code from other authors, particularly on the script calls for conditional branches and control variable editor commands. As it is for my own use, at worse, I can disable the plugin commands applying states, and only apply states when the player chooses "Okay" for "Are you happy with this character you have just created?". (My intention is to have other states applied depending upon choices the player makes during character creation.)

I will investigate further, and if I find anything out, mention it here, or on a later post if the forum has a time limit on editing posts. Thanks again to ATT_Turan for your help.

Edit3: I used a couple of events to compare the editor commands and script boxes. Using the editor commands to remove and add states always works, even if a series of changes are done within the same event on the same visit to the event.

With scripts, things are a little more complicated. Doing one change at a time for each visit to the event works. Code used to add state 11 to Harold, below, in spoiler if I can get the formatting right:

$gameActors.actor(1).addState(11);

$gameActors.actor(1).refresh();

$gamePlayer.refresh();

However, several changes to the state (applying and removing) whether in the same script boxes or multiple script boxes doesn't apply the state.

Edit 4 - Looks as if I may had done it! I tested it and the states went on as intended. Will report back if it doesn't prove reliable

I added:
this.clearResult();
to near the end of the function that changes an actor's gender. It now looks like:

[ICODE] Game_Actor.prototype.changeGender = function (newGender) { this.gender = newGender; if (this.gender == 1) { if (this.isStateAffected(femaleActorState)) this.removeState(femaleActorState); if (this.isStateAffected(maleActorState)) this.removeState(maleActorState); } if (this.gender == 2) { if (this.isStateAffected(femaleActorState)) this.removeState(femaleActorState); this.addState(maleActorState); }; if (this.gender == 3) { if (this.isStateAffected(maleActorState)) this.removeState(maleActorState); this.addState(femaleActorState); }; this.clearResult(); }; [/ICODE]
 
Last edited:

VoxSyreni

Veteran
Veteran
Joined
Nov 25, 2020
Messages
35
Reaction score
7
First Language
Danish
Primarily Uses
N/A
Hi guys, So I been using Yanfly Battle Core AI, great stuff, but I been wondering if there is a way to set the AI consistency by troop instead of by enemy?

Edit: I think I found my answer and I am sorry for posting inthe wrong thread by mistake.
 
Last edited:

ATT_Turan

Forewarner of the Black Wind
Veteran
Joined
Jul 2, 2014
Messages
2,833
Reaction score
1,659
First Language
English
Primarily Uses
RMMV
Hi guys, So I been using Yanfly Battle Core AI, great stuff, but I been wondering if there is a way to set the AI consistency by troop instead of by enemy?
That's not a JavaScript question (the answer doesn't have anything to do with code). Instead of posting in this thread, you'll get better responses making a thread in Plugin Support.

(although I'll tell you for free, everything you can do with the plugin is in its documentation. If it doesn't say so in there, then you can't. And how would you even put instructions into a troop? They don't have a note section.)
 

rpgLord69

Veteran
Veteran
Joined
Oct 23, 2021
Messages
34
Reaction score
14
First Language
Finnish
Primarily Uses
RMMZ
I have a parallax map and am in the process of adding region restictions. I started using the yanfly region restrictions plugin. I'm using MZ and I playtested the map and the plugin worked. But then I read some posts here that said that those yanfly plugins for mv don't work for mz. So does that mean that using that would cause some problems or crashes for my game later or what (since at least the map with region blocks was working for mz)?
Thanks!

Edit: Ooops, read above that this thread is for coding-questions, sorry.
 

Frostorm

[]D[][]V[][]D
Veteran
Joined
Feb 22, 2016
Messages
2,319
Reaction score
1,942
First Language
English
Primarily Uses
RMMV
Hi, where can I find all available "symbols" for the addCommand() function?
JavaScript:
Window_Command.prototype.addCommand = function(name, symbol, enabled, ext) {
    if (enabled === undefined) {
        enabled = true;
    }
    if (ext === undefined) {
        ext = null;
    }
    this._list.push({ name: name, symbol: symbol, enabled: enabled, ext: ext});
};

Because, for some reason, the symbol "guard" doesn't give me a working Guard command. Whereas the "attack" symbol works just fine though.
 

ATT_Turan

Forewarner of the Black Wind
Veteran
Joined
Jul 2, 2014
Messages
2,833
Reaction score
1,659
First Language
English
Primarily Uses
RMMV
Hi, where can I find all available "symbols" for the addCommand() function?
That's not how addCommand works - there is no "list of available symbols." The symbol parameter is just whatever text you want to be displayed. The first parameter, name, is a reference to the method that should be executed when the command is chosen.

You can do a search for it through rpg_windows.js to see how addCommand is used, including where it lists Guard.
 

TheoAllen

Self-proclaimed jack of all trades
Veteran
Joined
Mar 16, 2012
Messages
6,493
Reaction score
8,321
First Language
Indonesian
Primarily Uses
RMVXA
ATT_Turan is right, but not quite right, the symbol is just an identifier just like how you name a variable.

Let me give you a real example.
This is how the title command window adds their command.
JavaScript:
Window_TitleCommand.prototype.makeCommandList = function() {
    const continueEnabled = this.isContinueEnabled();
    this.addCommand(TextManager.newGame, "newGame");
    this.addCommand(TextManager.continue_, "continue", continueEnabled);
    this.addCommand(TextManager.options, "options");
};

The identifier for those three commands are "newGame", "continue", and "options" respectively.
Here is how they are used.
JavaScript:
Scene_Title.prototype.createCommandWindow = function() {
    const background = $dataSystem.titleCommandWindow.background;
    const rect = this.commandWindowRect();
    this._commandWindow = new Window_TitleCommand(rect);
    this._commandWindow.setBackgroundType(background);
    this._commandWindow.setHandler("newGame", this.commandNewGame.bind(this));
    this._commandWindow.setHandler("continue", this.commandContinue.bind(this));
    this._commandWindow.setHandler("options", this.commandOptions.bind(this));
    this.addWindow(this._commandWindow);
};
This part of the code means "if 'newGame' (command identifier) is being clicked, call command new game".
 

Frostorm

[]D[][]V[][]D
Veteran
Joined
Feb 22, 2016
Messages
2,319
Reaction score
1,942
First Language
English
Primarily Uses
RMMV
Ah, I understand now! Thanks, I was able to get my Guard command working as a result! :D
I thought the 1st value was for the text tho since it shows "TextManager.newGame", for example. As in, it'll use w/e value you put in the database's terms tab.
 

dopan

Veteran
Veteran
Joined
Mar 27, 2020
Messages
1,028
Reaction score
536
First Language
German
Primarily Uses
RMMV
Ah, I understand now! Thanks, I was able to get my Guard command working as a result! :D
just let me tell you for later eventing stuff in your project, put Game Switches on each Command so that you can enable/disable them whenever you want^^

example img from my srg core
Screenshot_1.png
 

ATT_Turan

Forewarner of the Black Wind
Veteran
Joined
Jul 2, 2014
Messages
2,833
Reaction score
1,659
First Language
English
Primarily Uses
RMMV
ATT_Turan is right, but not quite right, the symbol is just an identifier just like how you name a variable.
Not to argue, I'm just curious - how does that make my response not quite right? The symbol is not defined in any external list, it's what you choose to make it. It has to be consistent across any methods where you reference it, but there's not an objectively right or wrong name.

Or am I missing something?
 

TheoAllen

Self-proclaimed jack of all trades
Veteran
Joined
Mar 16, 2012
Messages
6,493
Reaction score
8,321
First Language
Indonesian
Primarily Uses
RMVXA
The first parameter, name, is a reference to the method that should be executed when the command is chosen.
This one right here. The handlers are located in a separate function.

To add to the complete usage of each parameter:
  • Name = label/text display (not method/function name as you mentioned it)
  • Symbol = identifier
  • Enabled = Boolean to enable/disable you from clicking it
  • Ext = Extended data, data attached to that particular command, in case if you want to use it for something. Most of the time, you don't need it.
 

Frostorm

[]D[][]V[][]D
Veteran
Joined
Feb 22, 2016
Messages
2,319
Reaction score
1,942
First Language
English
Primarily Uses
RMMV
Alright, I got another quick JS question. How do I make it so that, if an array has any duplicates, ignore that duplicate? So if an array returns the following: [apple, orange, orange, kiwi] I want to get rid of the extra "orange", if that makes sense...

Edit: This is the actual array I'm referring to. Notice how "attack" shows twice. This is because the actor is dual-wielding and each weapon is adding an instance of "attack".
1636309433625.png
 
Last edited:

ATT_Turan

Forewarner of the Black Wind
Veteran
Joined
Jul 2, 2014
Messages
2,833
Reaction score
1,659
First Language
English
Primarily Uses
RMMV
Alright, I got another quick JS question. How do I make it so that, if an array has any duplicates, ignore that duplicate?
I Googled "JavaScript array unique entries" and got this recommendation:
Code:
var unique = [...new Set(myArray)];
 

Frostorm

[]D[][]V[][]D
Veteran
Joined
Feb 22, 2016
Messages
2,319
Reaction score
1,942
First Language
English
Primarily Uses
RMMV
I Googled "JavaScript array unique entries" and got this recommendation:
Code:
var unique = [...new Set(myArray)];
In that case, would the following be valid?:
...assuming "array" is already defined and is the name of my array...
array = [...new Set(array)];
...or would that create a new array instead of modifying my existing "array"?

Edit: Hooray, it worked like a charm! I no longer have 2 "attack" commands!
1636311491999.png
1636311442608.png
Man, I feel like I'm on a roll lately lol. Got lots of JS stuff done that I probably wouldn't have been able to accomplish several months ago.
 
Last edited:

raffle

Veteran
Veteran
Joined
Oct 25, 2020
Messages
77
Reaction score
16
First Language
English
Primarily Uses
RMMV
Henlo again :LZSsmile:

I want to stop the mouse map movement but still use the mouse to interact with events.
Sadly if I disable the map movement I can't interact with any events at all, it seems like both functions are tied.

I tried using AS_InputSettings and YEP_TouchInputDisabler, along with some things people mentioned on the forums in the past where you remove a line from rpg_objects and/or rpg_scenes.
But every time the map movement is disabled, so is triggering events.

The only exception is TDDP_MouseSystemEX where you can add a notetag to events to enable the mouse triggering them, but this works in total disregard for the player's location, you can activate it across anywhere on the map whereas I want the player to be next to the event as if you were pressing the OK button.

Is there no simple way to achieve this?
 

Latest Threads

Latest Posts

Latest Profile Posts

Here's a medical question for ya: Why do I start lightly coughing after I drink coffee?
Well, someone "reviewed" my game. Didn't credit me anywhere in the video and spent 2 minutes insulting it. Going to ignore that and move on.
To cut down on map lag, I've been researching how to implement a lighting layer as a single map overlay instead of a bunch of individual lights. Hopefully I'll have a screenshot soon!
Imagine having a game broken up into a whole bunch of chapters? Like Chapter 11 should be about the party members going bankrupt.
Over 1/4th of the calendars are already out :eek: Have you already put something to a good use?

Forum statistics

Threads
117,263
Messages
1,105,937
Members
153,436
Latest member
TotalNoobAtRpgmaker
Top