How to use something like $gameActors.actor(1).skills()

Happy Holidays, everyone!

I have been experimenting and trying everything that I can to figure this out, but I am failing miserably. I have searched for solutions without any luck.

I see in the MZ Script Calls that you can get an array of things like party member names or, like in the title of the thread, a list of all the skills for a specific actor.

I have used arrays and I can make an array and extract data from it and display it.

But I just can't figure out how to show any values that are returned when I use the script like in the thread title.

So, for $gameActors.actor(1).skills(), if I understand this correctly, this is supposed to get an array of the skills that Actor 1 has. It says it Returns $dataSkill[], which I don't know how to access and use.

I am setting that to a Variable to start.

But when I try to associate what gets returned to a different Variable so I can display it and see what is happening, I get things like Object, Object.

Everything that I have tried is too long to list here.

I am sure that I am missing something very simple or missing a step.

For the sake of this question, lets say that I want to get a list of all the skills that a specific party member has. I want to see that list to confirm that it got what I expected. Then I want to use that list to present back to the player.

Real game example:

The player is presented with a choice of which party member will sacrifice a know skill. So the player selects the party member, then the skills that the party member knows is presented to the player. The player selects which skill they will sacrifice. Then that skill will be removed from that party member.

To do this, I think I need to get a list of the known skills for the specific party member, then associate those skills to variables so I can display the variables as choices for the player to select.

I just can't figure out how to use the script to get the array of the information and then get the information from the array to see and use.

I hope I am explaining this in a way that makes sense.

If it isn't too much trouble, could someone give me a very clear, specific example of how to do this?

Again, the only knowledge of Java Script that I have is what I have learned while trying to learn MZ.

Thanks for any help that you can offer!
To get a list of all the skills that a specific actor has, you can use the skills() method with the Game_Actor class. This method returns an array of $dataSkill objects representing the skills that the actor has learned.

Code:
// Get the actor object for the actor we want to get the skills for
let actor = $gameActors.actor(1);

// Get the array of skills for the actor
let skills = actor.skills();

// Iterate through the array of skills and display the name of each skill
for (let i = 0; i < skills.length; i++) {
  console.log(skills[i].name);
}

This example will get the actor object for actor 1, get the array of skills for that actor using the skills() method, and then iterate through the array of skills, displaying the name of each skill.

To display the skills to the player in the game, you can use the Window_ChoiceList class to create a choice list window and display the skills as choices.

Code:
// Create a new Window_ChoiceList object
let choiceWindow = new Window_ChoiceList();

// Set the choices to be the names of the skills
choiceWindow.setChoices(skills.map(skill => skill.name));

// Show the choice window
choiceWindow.show();

This will create a new Window_ChoiceList object, set the choices to be the names of the skills, and then show the choice window to the player.
As mentioned, $gameActors.actor(1).skills() returns an array of database records. Each record is an anonymous Object ({}) with various bits of information: ID, name, MP Cost, TP Cost, formula, effect list, etc. A full list of the available properties can be found on the Database tab of the MZ Reference sheet:
Alternatively you can check your project's Skills.json file directly (it's in the data subfolder) or explore records in the console like $gameActors.actor(1).skills()[0] (first entry).

For getting the player to pick one, I'd consider something like this (adapted from the example on the Script Call List):
JavaScript:
// Get skills of actor ID 1
const skills = $gameActors.actor(1).skills();
// Set skill names as choices (default 0; cancel branch)
$gameMessage.setChoices(skills.map(s => s.name), 0, -2);
// Set background: Window
$gameMessage.setChoiceBackground(0);
// Set position: Center
$gameMessage.setChoicePositionType(1);
// On choice, set variable #1 equal to:
// - the ID of the skill at the selected index
// - or 0 if cancelled
$gameMessage.setChoiceCallback(n => {
  $gameVariables.setValue(1, n === -2 ? 0 : skills[n].id);
});
// Wait for a choice to be selected
this.setWaitMode('message');
This makes use of the choice window that exists on the scene, and integrates properly with the game loop.
@OPFARM1 & @caethyril Thanks for your quick responses! I was WAAAYYYY off in what I was trying.

@OPFARM1 I copied and pasted the code, but it didn't work. It was probably because I didn't understand it and I probably did something wrong.

@caethyril I copied and pasted your code and it works perfectly!

Now, if you will indulge me another probably stupid question. "const skills" is setting a Variable in the game, correct? If that is true, how is that identified? Meaning, when I create a Variable using the UI, I am giving it a number for reference and then I can refer to that Variable using \V[#] or I can refer to it in the Event Commands for Control Variables. I can select it again via the UI rather than using a script.

By doing this directly with a script, does that mean that the "skills" Variable doesn't have a number for reference and I can't see it through the UI? But I could use it again?

Although I think I remember reading previously that "const" is used when you aren't going to change the value of the variable.

I apologize if this is a very basic question.

Thanks again!
(OPFARM's example does not work as-is, no worries.)

By doing this directly with a script, does that mean that the "skills" Variable doesn't have a number for reference and I can't see it through the UI? But I could use it again?
It's temporary, because the scope of the script call (an eval) is limited to the evaluation of that code. const, like let and to some extent var, declares a local variable rather than a global one. If you use any of those keywords to declare a variable in one of RPG Maker MZ's Script fields, it will only exist for the duration of that script call.

The skills array technically persists longer than that, because a reference remains in the choice callback function. After all its references are gone, it will automatically be "garbage collected", i.e. removed from memory.

You can use that same script call as many times as you like. Each eval constitutes a new scope, so the local variables do not conflict.

When you use Control Variables or a corresponding script call, you are technically setting a value in an array, _data, defined on the global object $gameVariables. That array also gets included in save data.

Hope that helps clarify things! 👋
@caethyril Yes, that is exceptionally helpful and makes sense the way you explained it!

Thanks again!

EDIT:

I am updating this in case it helps anyone else who looks at this thread and doesn't know how to proceed, like me!

It took me a bit to figure out the next part about what happens when the player makes their selection.

But here is a screenshot of what it looks like in the game, thanks to @caethyril, and a screenshot of the things I added to get the Skill ID and Name and to remove that Skill from the actor.

This wasn't anything like I thought it would look like, but it works like a charm and hopefully I learned a bit more about this stuff works.

Again, a huge thanks to @OPFARM1 and @caethyril for their help on this!

Select Skill to Lose Choices.png
Select Skill to Lose Event.png
Nice! 😂

Ideas:
  1. You may want to either change the cancel type from -2 (Branch) to -1 (Disallow) in this line:
    JavaScript:
    $gameMessage.setChoices(skills.map(s => s.name), 0, -2);
    ...or check "variable 218 = 0?" using a Conditional Branch command afterwards, since that's what happens when the player cancels.

  2. If you want to show a message at the same time, like "pick a skill to forget", I think you can do that by removing the setWaitMode line from the script call, then put a Show Text command immediately after the Script command. (Show Text automatically sets the wait mode to message: it has special handling for follow-up inputs like Show Choices, Input Number, or Select Item, but cannot check for scripted versions of these.)

  3. If you want to include the skill icons, you could try including an \i[x] text code in the choices part, e.g. swap this:

    skills.map(s => s.name)
    ...for this:

    skills.map(s => "\\i[" + s.iconIndex + "] " + s.name)
    (It's a "double" backslash here because single backslashes are used in JavaScript for escape notation.)

    The equivalent for your display variable (217) would look something like this:

    const s = $dataSkills[$gameVariables.value(218)]; "\x1bi[" + s.iconIndex + "] " + s.name
    (It has to be the special ESC character, \x1b here because that's what all text code backslashes have been replaced with, at the time of substituting variable values.)
@caethyril Thanks again for all your help! This additional information is very cool and I experimented with it. The one thing I couldn't figure out was this:

The equivalent for your display variable (217) would look something like this:

const s = $dataSkills[$gameVariables.value(218)]; "\x1bi[" + s.iconIndex + "] " + s.name

(It has to be the special ESC character, \x1b here because that's what all text code backslashes have been replaced with, at the time of substituting variable values.)
I couldn't figure out how to show that in a Show Text message. I tried a variety of things, but most of the time I kept getting something like "s" undefined or something like that. I couldn't figure out how to assign that to a Variable that could be displayed in a Show Text message. So I am clearly missing something.

However, I ended up achieving the same result...I think...where I show both the icon and the name of the skill that was lost.

I am attaching another set of screenshots in case it helps anyone.

Thanks again!

Select Skill to Lose Choice Acknowledged.png
Select Skill to Lose Event Updated.png
I couldn't figure out how to show that in a Show Text message. I tried a variety of things, but most of the time I kept getting something like "s" undefined or something like that. I couldn't figure out how to assign that to a Variable that could be displayed in a Show Text message. So I am clearly missing something.
In a Script box:
Code:
const s = $dataSkills[$gameVariables.value(218)]; 
$gameVariables.setValue(12, "\x1bi[" + s.iconIndex + "] " + s.name);
where 12 is the ID of the variable you want the string stored in.
In a Script box:
Code:
const s = $dataSkills[$gameVariables.value(218)];
$gameVariables.setValue(12, "\x1bi[" + s.iconIndex + "] " + s.name);
where 12 is the ID of the variable you want the string stored in.
Well, Son of a Biscuit Eater! Thanks, @ATT_Turan !

That works perfectly and I was WAAAYYYYYY off from how I was trying to use that.

Thanks again!
Alternatively you should just be able to enter it as a single line in Control Variables -> Script. Probably more intuitive to store the icon index and name in 2 separate variables like you did, though~

Happy RPG Making!
Alternatively you should just be able to enter it as a single line in Control Variables -> Script. Probably more intuitive to store the icon index and name in 2 separate variables like you did, though~

Happy RPG Making!
Hi, @caethyril ! I did try putting that exact script in a Control Variable from the Event Command and it kept saying that "s is not defined." But I was removing the first part because I thought it was only needed when using the Event Command -> Script option.

So I didn't realize I needed to keep the "const s = " part in the script. I thought you used that only if you are using the Event Command -> Script option without designating a Variable Number through the UI.

I tried copying and pasting the entire script, including the "const s = " that I was leaving out, and it works!

Thanks for following up and helping me learn that it does work both ways!

Original thread: https://forums.rpgmakerweb.com/threads/how-to-use-something-like-gameactors-actor-1-skills.153537/