Script to identify if a party slot is empty.

ZombieKidzRule

Note to Self: Patience Newbie...Patience.
Veteran
Joined
Jan 9, 2022
Messages
561
Reaction score
493
First Language
English
Primarily Uses
RMMZ
So, this is really stumping me. I have searched every variation of questions I can think of and tried different scripts I found in the RPG Maker MZ Script Call Reference list. But nothing works. I probably found the answer, but just don't know how to use it correctly.

What I am trying to do is use the \P[ ] for Choice Options. Then I am using a script to check those choices against the Party Member Name or Actor ID using a Conditional Branch.

The problem I ran into was if you create the event with 5 \P[ ] choices, but then the party is only 3, 2 of the choices are blank. Which is fine, but if you select one of those choices, you get an error.

So, I made the event go through all of the options based on how many party members there are. From 1 to 5. That works, but it is really long.

I was thinking there should be a way to check to see if the specific \P[ ] choice is actually empty. Like the event is set to show \P 1-5, but there are only 3 party members. If the player selected the 4th or 5th choice that is blank, the event will check to see if that slot in the party is empty and will then do something without giving an error.

If was trying to find a way to basically say if the 4th Party slot is empty, then do this. I tried using stuff I found about JS and null values, but nothing worked.

Here is a screenshot of part of the event that shows the adjudication of the choice selection.


Choices Screenshot.png

What I am trying to do is if there is no \P[4] or \P[5], instead of getting an error if the player selects those, it displays a message.
This is all just testing the functionality and NOT what I am trying to do in a game. Showing text is the easiest way to test if it is doing what I want.

A real application for this would be selecting which party member is doing something. Like speaking, using a skill, using an item, etc. But I want to build it so it works no matter how many party members you have.

Right now, what I am doing is determining the party size, then going through all the options. If the party size is 1, don't show any choices. If the party size is 2, show two choices of \P[1] and \P[2], etc. And each \P[ ] option then goes through and checks to determine which actorID is associated.

And this allows the player to change the formation of the party and the choices still work.

I am hoping there is an easier way to do this.

But I am still very new to RPG Maker and Java Script. I struggle to figure out the script calls and how to properly implement them.

I hope I have explained what I am trying to do in a way that makes sense.

Any help is greatly appreciated.

Thanks!
 

Trihan

Speedy Scripter
Veteran
Joined
Apr 12, 2012
Messages
5,782
Reaction score
5,597
First Language
English
Primarily Uses
RMMZ
There are plugins that let you add show conditions to choices that might help with this, but for your immediate purposes what you could do is check $gameParty.size() being greater than or equal to X.
 

caethyril

^_^
Global Mod
Joined
Feb 21, 2018
Messages
4,341
Reaction score
3,381
First Language
EN
Primarily Uses
RMMZ
For the first 8 members of the party you can check their actor ID without scripting:
  • Control Variables -> Game Data -> Party -> Member #N
  • Conditional Branch -> Variable
The actor ID will be 0 if that party member doesn't exist. E.g.

◆Control Variables:#0036 check = Actor ID of party member #4 ◆If:check = 0 ◆Text:None, None, Window, Bottom : :Party member #4 does not exist at the moment. ◆ :Else ◆Text:None, None, Window, Bottom : :Party member #4 has actor ID \v[36]. ◆ :End
Another option, like Trihan says, is to just check party size (Control Variables -> Game Data -> Other).
 

ZombieKidzRule

Note to Self: Patience Newbie...Patience.
Veteran
Joined
Jan 9, 2022
Messages
561
Reaction score
493
First Language
English
Primarily Uses
RMMZ
Thank you both so much for your quick replies! I honestly didn't understand how your solutions helped at first, but then I figured it out. I am just a little slow.

Apparently, I was trying to find a complicated solution when a simple option existed!

Here is a screenshot of what I inserted and it works perfectly.

Thanks!

Party Member Size Screenshot.png
 

Rlyeh

Villager
Member
Joined
Nov 27, 2022
Messages
10
Reaction score
9
First Language
English
Primarily Uses
RMMZ
If you're using scripts, you could put _actorId in a variable

◆Control Variables:#0001 ActorId = $gameParty.members()[0]._actorId
◆Text:None, None, Window, Bottom
: :Hello, \N[\V[1]]

Or
◆Control Variables:#0001 Name = $gameParty.members()[0]._name
◆Text:None, None, Window, Bottom
: :Hello, \V[1]
 
Last edited:

caethyril

^_^
Global Mod
Joined
Feb 21, 2018
Messages
4,341
Reaction score
3,381
First Language
EN
Primarily Uses
RMMZ
@ZombieKidzRule - I don't see why you need all those branches inside each choice. Can't you just use Hello, \P[1]? :kaoswt:

If you're using scripts, you could put _actorId in a variable
To clarify, that's exactly what Control Variables -> Game Data -> Party -> Member #X does. It checks the party member in that position and returns their actor ID, or 0 if there's no-one currently in that position.
 

ZombieKidzRule

Note to Self: Patience Newbie...Patience.
Veteran
Joined
Jan 9, 2022
Messages
561
Reaction score
493
First Language
English
Primarily Uses
RMMZ
@Rlyeh Thanks for your reply! I have done that before.

My issue with this was in using the Party Member Names as Show Choices options and then what happens when your party gets larger or smaller and party members change positions.

It seems like everything I try to make leads to more questions and more complex events!

I am having a blast!

@caethyril The simple text message in my example is only to check that the event is doing what I want.

In a real application, I would be using the Show Choices options to determine which party member is going to do something, like use a skill, or use an item, or talk to an NPC. Then, based on that party member selected by the player, different Common Events would be called.

Sorry if my simple test text was making this look like I was making it very complicated to say Hello to a specific party member!
 
Last edited:

Rlyeh

Villager
Member
Joined
Nov 27, 2022
Messages
10
Reaction score
9
First Language
English
Primarily Uses
RMMZ
@Rlyeh Thanks for your reply! I have done that before.

My issue with this was in using the Party Member Names as Show Choices options and then what happens when your party gets larger or smaller and party members change positions.
Never used them but there are conditional show choice plugins like this one

Also script calls for handling choices
$gameMessage.setChoices(choices, defaultIndex, cancelIndex);
$gameMessage.setChoiceBackground(bgType);
$gameMessage.setChoicePositionType(posType);
$gameMessage.setChoiceCallback(callback);
interpreter.setWaitMode('message');

from https://forums.rpgmakerweb.com/index.php?threads/rpg-maker-mv-mz-script-call-list.46456/
 
Last edited:

ZombieKidzRule

Note to Self: Patience Newbie...Patience.
Veteran
Joined
Jan 9, 2022
Messages
561
Reaction score
493
First Language
English
Primarily Uses
RMMZ
@Rlyeh Thanks for this!

I haven't started exploring plugins yet because I am trying to see what I can do without them first. But plugin research is definitely on my to do list!

The Excel resources are awesome, but they are a little hard for me to understand. I reference them and experiment, trying to figure things out, but not knowing anything about scripting or how Java Script is incorporated into the RPG Maker interface is a bit challenging.

For instance, I bought a book called Java Script for Kids, thinking it would be fairly simple. And it seems simple enough to understand what is going on. But what is shown in the book doesn't work the same way in RPG Maker as it does just working in a browser. People who understand more than me probably understand why that is true and how to modify it, but I am still trying to figure it out!

I just keep plugging away and experimenting. I usually try to find any way to do what I want and then try to find better, more efficient ways to get the same result.

Thanks again for your help!
 

caethyril

^_^
Global Mod
Joined
Feb 21, 2018
Messages
4,341
Reaction score
3,381
First Language
EN
Primarily Uses
RMMZ
The Script Call Reference expects a basic familiarity with JavaScript. The Script Call List is more beginner-friendly, but you'll still want some basic JS knowledge if you want to do much more than copy+paste some of its examples.

You're familiar with the English language, but that doesn't mean you're necessarily familiar with a lot of English literature. A similar thing applies to JavaScript vs the RMMV core scripts: the latter is built on the former. (Fortunately there's a lot less to learn in the code case, because programming doesn't deal with a whole world of experience!)

In a real application, I would be using the Show Choices options to determine which party member is going to do something, like use a skill, or use an item, or talk to an NPC. Then, based on that party member selected by the player, different Common Events would be called.
Ah, that makes sense. However, I still don't see the need for scripting here, since you can store the actor ID in a variable (like in my example) then check if it's equal to 1, 2, 3, etc in those branches. As mentioned, the event command also checks if there's an actor in that position, so you wouldn't even need the party size check. Also, this means you only look up the actor reference once, and don't use an eval to do so.

It's your game, though! Happy RPG Making~ :kaojoy:
 

Rlyeh

Villager
Member
Joined
Nov 27, 2022
Messages
10
Reaction score
9
First Language
English
Primarily Uses
RMMZ
I played around with the script, and came up with something like this-

JavaScript:
choices = [];
$gameParty.members().forEach(m => choices.push(m.name()));
choices.push("Nevermind");
$gameMessage.setChoices(choices, 0, choices.length-1);
$gameMessage.setChoiceBackground(1);
$gameMessage.setChoicePositionType(1);
$gameMessage.setChoiceCallback(n => {
  $gameVariables.setValue(1, n!=choices.length-1?n:-1);
});
this.setWaitMode('message');

Variable 1 contains the choice or -1 if "Nevermind" / Cancel was selected
 
Last edited:

ZombieKidzRule

Note to Self: Patience Newbie...Patience.
Veteran
Joined
Jan 9, 2022
Messages
561
Reaction score
493
First Language
English
Primarily Uses
RMMZ
@caethyril Thank you so much for your patience and understanding! I finally understand what you are saying about using Variables instead of a bunch of Script calls! And I figured it out while I was trying to incorporate what @Rlyeh showed above.

@Rlyeh A huge thank you for that JS. I copied and pasted it and it worked perfectly! Before I saw this I was looking at the reference resource list and going...yeah...I don't understand how to do that. But seeing it scripted out and then performing in the game engine, I understand it a bit better. I don't understand everything, mind you, but I am getting there.

Just to make sure I am understanding, I still have to do a series of Conditional Branches to make sure I identify the correct Actor that has been selected in the choice and then move forward based on that specific Actor.

I am attaching a screenshot. The Text box event command is just a test and it would be replaced with actual eventing for that specific actor. Such as checking their abilities, skills, attributes for bonuses to apply to a role play skill check or calling an actor specific Common Event, or something similar. And not to just tell the player their name! :LZSlol:

Again, thank you both for being so patient and willing to help!

EDIT: Ok, so, WOW! This works great! I just updated my test event to accommodate up to 8 Party Members and it worked perfectly. I changed the formation and changed the number of party members and everything was great. This has been EXCEPTIONALLY helpful!

ChoicesScriptTest.png
 
Last edited:

Rlyeh

Villager
Member
Joined
Nov 27, 2022
Messages
10
Reaction score
9
First Language
English
Primarily Uses
RMMZ
Just to make sure I am understanding, I still have to do a series of Conditional Branches to make sure I identify the correct Actor that has been selected in the choice and then move forward based on that specific Actor.
Assuming your ChoiceSelection variable isn't -1, '$gameParty.members()[$gameVariables.value(106)]._actorId' will give you the actor ID.

For example-

◆Script:choices = [];
: :$gameParty.members().forEach(m => choices.push(m.name()));
: :choices.push("Nevermind");
: :$gameMessage.setChoices(choices, 0, choices.length-1);
: :$gameMessage.setChoiceBackground(1);
: :$gameMessage.setChoicePositionType(1);
: :$gameMessage.setChoiceCallback(n => {
: : $gameVariables.setValue(106, n!=choices.length-1?n:-1);
: :});
: :this.setWaitMode('message');
◆If:ChoiceSelection ≠ -1
◆Control Variables:#0107 ActorID = $gameParty.members()[$gameVariables.value(106)]._actorId
◆Text:None, None, Window, Bottom
: :Hello, \N[\V[107]]

:End
 
Last edited:

ZombieKidzRule

Note to Self: Patience Newbie...Patience.
Veteran
Joined
Jan 9, 2022
Messages
561
Reaction score
493
First Language
English
Primarily Uses
RMMZ
Assuming your ChoiceSelection variable isn't -1, '$gameParty.members()[$gameVariables.value(106)]._actorId' will give you the actor ID.
Well, good grief! That certainly makes it easier!

It took me a few minutes to figure out how to do the Nevermind option, but I just tested everything successfully. A lot less Conditional Branches and only using 1 Variable.

Here is an updated screenshot. Thank you so much for your continued help to make this simpler and more efficient!

ChoicesScriptUpdated.png
 

caethyril

^_^
Global Mod
Joined
Feb 21, 2018
Messages
4,341
Reaction score
3,381
First Language
EN
Primarily Uses
RMMZ
Looks good! I would suggest changing this line, though:
JavaScript:
choices = [];
...to this:
JavaScript:
const choices = [];
Without a declaration keyword like const (or let or var), the choices variable will be global, i.e. it will be accessible from any code in the game and it will remain after the script call ends. That name doesn't clash with anything in the core scripts, but it's good practice to avoid making global variables unless necessary. :kaohi:
 

ZombieKidzRule

Note to Self: Patience Newbie...Patience.
Veteran
Joined
Jan 9, 2022
Messages
561
Reaction score
493
First Language
English
Primarily Uses
RMMZ
@caethyril Thanks for pointing that out!

EDIT:

@caethyril & @Rlyeh For full transparency, I have a YouTube channel where I post RPG Maker MZ tutorials of things that I have learned how to do and I think will help someone else. I try to give shout outs on certain videos when more experienced folks have provided assistance in what I was able to do. Sometimes that is general, like the experienced users on the RPGMAKERWEB forum, or folks who have made basic tutorial series that helped me get a foundation of understanding.

I also try to make it clear that what I am showing is just one way to do something that I verified through testing. It probably isn't the best or most efficient way, but it seems to work. And I am always looking for ways to make things better.

I plan on posting a video related to this thread because I think it might help other people and I plan to give you both specific credit for your assistance. But also make it clear that neither of you "signed off on" or "verified" the final product and if there are any issues, it is solely on me and my inexperience.

I try to be clear that I am still learning, I don't have any programming experience, and my videos are just trying to help people who are on the same journey. I am very appreciative when I can find a tutorial or post about something I am trying to do. Sometimes posts from older versions or RPG Maker actually point me in the right direction. So I am just trying to give back and help, while trying not to give the impression that I am an expert by any sense. And also not trying to make tutorials for things that people can already readily find.

Please let me know if you have any concerns with being mentioned/referenced in such a video. The last thing I want to do is offend anyone.

Here is a link to the RPG Maker MZ Playlist on my YouTube channel if you want to see the types of videos that I post. And again, thank you so much for being willing to help!

 
Last edited:

Rlyeh

Villager
Member
Joined
Nov 27, 2022
Messages
10
Reaction score
9
First Language
English
Primarily Uses
RMMZ
Without a declaration keyword like const (or let or var), the choices variable will be global, i.e. it will be accessible from any code in the game and it will remain after the script call ends. That name doesn't clash with anything in the core scripts, but it's good practice to avoid making global variables unless necessary. :kaohi:
Thanks. Just learning JavaScript but I got the impression var also makes function or global variables regardless of blocks.
 

caethyril

^_^
Global Mod
Joined
Feb 21, 2018
Messages
4,341
Reaction score
3,381
First Language
EN
Primarily Uses
RMMZ
@ZombieKidzRule - I'm OK with that. :kaohi:

@Rlyeh - I just skim-read the page you linked and it all looks correct to me. I think what you're missing is that in RMMV/Z, all Script commands/fields are handled as evals:
An eval basically constitutes a temporary function scope. [Edit: looks like it's actually pretty complicated, see following posts.]
 
Last edited:

caethyril

^_^
Global Mod
Joined
Feb 21, 2018
Messages
4,341
Reaction score
3,381
First Language
EN
Primarily Uses
RMMZ
An eval basically constitutes a temporary function scope.
You can test it, var introduces a global even in eval if not in a function.
Oh, neat! I just looked it up and apparently there's a lot of complication there. Seems that var will persist into the enclosing scope, but that will only be the global scope if the eval is run in the global scope (which can happen if it's called indirectly...?). Details:

By default all of RMMZ's Script commands are processed by the Game_Interpreter#command355 method, and the eval is called directly, therefore any var declared in the eval ends up local to that function. Test case:

◆Script:var test = 'hello'; ◆Script:console.log(test);
Yields "test is not defined", like your global let example.

A similar thing applies to RMMZ's other Script fields: Move Route -> Script, Control Variables -> Script, Conditional Branch -> Script, and Skill/Item damage formulae (I think that's all of them). You can still define globals in an eval implicitly (by omitting the var/let/const) or explicitly (by defining it as a member of the global object: window or globalThis).
 

Latest Profile Posts

So I managed to scare my chief. Between shifts I had an appointment with my psychiatrist, so prior to it I'd told him "If I ain't coming today, they've kept me there.". Then as I was asleep, my phone died. So I overslept and through the entire time I was unreachable.
I spent so much time toiling in VX Ace frankensteining scripts together only for MV to come along and just... have everything.

The overworld tool system in particular I thought was gonna take me a million years to recreate, but nope. Thanks Moghunter.
Going to go live way earlier than usual tonight, pretty much in 20 minutes, purely because I'm bored. :LZSlol:

So... I'm gonna be a parent... XD

Forum statistics

Threads
128,378
Messages
1,194,061
Members
168,877
Latest member
eternaldes
Top