How to Randomize NPC spawn and get them to give skills?

I'm making an RPG roguelike and the idea is that after every few rooms you come across 2 or 3 allies who need to be freed. As thanks they'd teach you a skill but you only have time to learn from one of them. For each area the player is going to end up with 2 or 3 additional skills when they're done. Is there a way to make it random which npcs spawn when you enter the room or do I just have to make a bunch of identical rooms with different npcs as prisoners and let the player randomly enter with conditions to exclude certain rooms if the player had already learned an npc's skill from a previous room?

Also how do you make it so that the npc will give the player a skill after talking to them.

Also is it possible to make it so that you could let a character equip a weapon in their off hand? I want to make it so that the player can equip 2 weapons and attack twice but as a consequence their defense and magic defense would take a debuff for as long as they have a second weapon equipped.
So the best way is to use Random variables.

So when you enter a map or even before hand, you set the variable called "Random NPC"

Between a random number 0-30 or which ever you want.

So NPC1 is set to appear when the random value is 27
Page 1 Condition "Random NPC" At 27 put this as the NPC appearing.
Page 2 condition "Random NPC" at 28 Leave this blank.

So this would make NPC appear when the "Random NPC" is at 27, anything below it won't appear and for Page 2 is when it goes beyond the 27 value.

As for var conditions is = to or more.


So I'll do a few examples of NPC so you understand it.

NPC2 (Will only appear when "Random NPC" is 15)
Page 1 Condition "Random NPC" At 15 put this as the NPC appearing.
Page 2 condition "Random NPC" at 16 Leave this blank.

NPC3 (Will only appear when "Random NPC" is 54)
Page 1 Condition "Random NPC" At 54 put this as the NPC appearing.
Page 2 condition "Random NPC" at 55 Leave this blank.

NPC3 (Will only appear when "Random NPC" is 5)
Page 1 Condition "Random NPC" At 5 put this as the NPC appearing.
Page 2 condition "Random NPC" at 6 Leave this blank.


Hope this helps.

edit:

Oh of course Page 1 will give you the skill.
So the best way is to use Random variables.

So when you enter a map or even before hand, you set the variable called "Random NPC"

Between a random number 0-30 or which ever you want.

So NPC1 is set to appear when the random value is 27
Page 1 Condition "Random NPC" At 27 put this as the NPC appearing.
Page 2 condition "Random NPC" at 28 Leave this blank.

So this would make NPC appear when the "Random NPC" is at 27, anything below it won't appear and for Page 2 is when it goes beyond the 27 value.

As for var conditions is = to or more.


So I'll do a few examples of NPC so you understand it.

NPC2 (Will only appear when "Random NPC" is 15)
Page 1 Condition "Random NPC" At 15 put this as the NPC appearing.
Page 2 condition "Random NPC" at 16 Leave this blank.

NPC3 (Will only appear when "Random NPC" is 54)
Page 1 Condition "Random NPC" At 54 put this as the NPC appearing.
Page 2 condition "Random NPC" at 55 Leave this blank.

NPC3 (Will only appear when "Random NPC" is 5)
Page 1 Condition "Random NPC" At 5 put this as the NPC appearing.
Page 2 condition "Random NPC" at 6 Leave this blank.


Hope this helps.

edit:

Oh of course Page 1 will give you the skill.
Ok, so if I'm understanding this right, The first number is the minimum number that you need to roll is the number that will result in the NPC appearing. But what about the 2nd page? What does it do?

Also how do I make it so that after my player encounters an NPC that NPC will not be able to be encountered a second time?

The current set up is that after every 3 rooms, the player will encounter an NPC without fail. But how do I make it so that when the player encounters NPC2 for example, that the next time they encounter a skill NPC it is impossible for it to ever be NPC2 again.

I want to set it up like if you roll between a 1-10 you get NPC1, 11-20 is NPC2 so on and so forth but when one of them is gone it increases the odds for other NPCs increase to fill in the space.

So if 1-10 is NPC1, 11-20 is NPC2, and 21-30 is NPC3, then if the player encounters NPC2 then NPC1's odds are now 1-20 or NPC1's odds are 1-15 and NPC3's are 16-30.

Is it possible to do that?
Ok, so if I'm understanding this right, The first number is the minimum number that you need to roll is the number that will result in the NPC appearing. But what about the 2nd page? What does it do?

Also how do I make it so that after my player encounters an NPC that NPC will not be able to be encountered a second time?

The current set up is that after every 3 rooms, the player will encounter an NPC without fail. But how do I make it so that when the player encounters NPC2 for example, that the next time they encounter a skill NPC it is impossible for it to ever be NPC2 again.

I want to set it up like if you roll between a 1-10 you get NPC1, 11-20 is NPC2 so on and so forth but when one of them is gone it increases the odds for other NPCs increase to fill in the space.

So if 1-10 is NPC1, 11-20 is NPC2, and 21-30 is NPC3, then if the player encounters NPC2 then NPC1's odds are now 1-20 or NPC1's odds are 1-15 and NPC3's are 16-30.

Is it possible to do that?


it's very possible indeed.

So yeah your right, the reason why the lowest is to make the NPC appear but you need him to disappear when the number is higher. That's what page 2 is for. The moment it's 1 more higher it's gone.

IF the player is just encounter 1 NPC every 3 rooms then you'll need just 1 Variable.

Their might be a better way to do this but what you can do, and this depends on how many NPCs you plan on using.

Each NPC has a Switch, when that Skill is learnt, you turn their switch on

So "NPC Skill Learnt 1"
each time you enter a map, do a random variable, between 1 and the number of NPCs you plan on using with conditional branches for each roll.


IF it lands on 3, then it checks to see if "NPC Skill Learnt 3" is on (meaning the skill has been learnt) IF it is on, the roll a random again, until it lands on a NPC who's switch isn't on.
I would do this a little differently.

I would use an array to store the numbers of the NPCs you are using. If you have 10 NPCs, your array would be Variable # = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].

Then you could randomly splice a number from the array and use that as your Variable value or even switch number to activate the correct event page for that NPC. Your event for each map would pretty much be identical.

Since it is an array, the next time you splice a random number from the array, the number for the previous NPC that was already met won’t be available.

At least that is how I would approach it and you don’t need any conditional branch checks for later encounters.

There is an Event Command to learn a skill.

I can’t answer about the dual wield.

Hope that helps and good luck!

EDIT: Welcome to the forum!
it's very possible indeed.

So yeah your right, the reason why the lowest is to make the NPC appear but you need him to disappear when the number is higher. That's what page 2 is for. The moment it's 1 more higher it's gone.

IF the player is just encounter 1 NPC every 3 rooms then you'll need just 1 Variable.

Their might be a better way to do this but what you can do, and this depends on how many NPCs you plan on using.

Each NPC has a Switch, when that Skill is learnt, you turn their switch on

So "NPC Skill Learnt 1"
each time you enter a map, do a random variable, between 1 and the number of NPCs you plan on using with conditional branches for each roll.


IF it lands on 3, then it checks to see if "NPC Skill Learnt 3" is on (meaning the skill has been learnt) IF it is on, the roll a random again, until it lands on a NPC who's switch isn't on.
Thanks a lot for the help this definitely made things easier.
I would do this a little differently.

I would use an array to store the numbers of the NPCs you are using. If you have 10 NPCs, your array would be Variable # = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].

Then you could randomly splice a number from the array and use that as your Variable value or even switch number to activate the correct event page for that NPC. Your event for each map would pretty much be identical.

Since it is an array, the next time you splice a random number from the array, the number for the previous NPC that was already met won’t be available.

At least that is how I would approach it and you don’t need any conditional branch checks for later encounters.

There is an Event Command to learn a skill.

I can’t answer about the dual wield.

Hope that helps and good luck!

EDIT: Welcome to the forum!
How do I make arrays in MZ? Do I have to code that in?

Also thanks, I'm glad to be here
I have quite a few tutorials that involve using Arrays.

When you are in the Control Variable Event command, you select Script at the bottom of the window and then you put things inside brackets [ ]. You can put numbers or strings.

I gave you an example before of an array with the numbers 1 through 10. Elements of the array must be separated by commas.

Here is an example of an array containing strings:

["Hello", "Hi", "Good morning", "Greetings"]

You can extract the elements/contents of an array and store the number or string in a variable to use for many different things.

It is very easy and you can go a lot with arrays.

If you check out my YouTube channel, you can scroll through the tutorials and see some that actually reference arrays in the title. The oldest would be the most basic.

Using arrays isn't terribly difficult, but you should definitely have a good grasp of the basics of using variables, switches, etc. as well.

Hope that helps.
I have quite a few tutorials that involve using Arrays.

When you are in the Control Variable Event command, you select Script at the bottom of the window and then you put things inside brackets [ ]. You can put numbers or strings.

I gave you an example before of an array with the numbers 1 through 10. Elements of the array must be separated by commas.

Here is an example of an array containing strings:

["Hello", "Hi", "Good morning", "Greetings"]

You can extract the elements/contents of an array and store the number or string in a variable to use for many different things.

It is very easy and you can go a lot with arrays.

If you check out my YouTube channel, you can scroll through the tutorials and see some that actually reference arrays in the title. The oldest would be the most basic.

Using arrays isn't terribly difficult, but you should definitely have a good grasp of the basics of using variables, switches, etc. as well.

Hope that helps.
Your videos are a lot of help. Thanks a bunch! Question though, is it possible to change an array automatically?

So let's say I have "Array of NPCs" [1, 2, 3, 4, 5] where each number is a place holder for an NPC. I use a variable to randomly generate a number and I get 3.

From here is it possible for me to put 3 in another array that will check with the results of my random variable to make sure that the random number it gets doesn't result in something that happened before?

The idea would look kinda like this

-Start Loop

--Generate Random number

--Start another loop

---Check Acquired Skill Array to see if that number has shown up before

---If yes break out of the current loop and go generate another random number.

---If no then add +1 to a counter Variable then check for the next number in the sequence

--Loop End

--Check the counter variable

--If the counter variable is less than the length of the Check Acquired Skill Array

--If it is smaller than set the counter to 0 and let it loop and generate another random number

--If the counter is equal to it then break out of the loop

-End loop

-Add the random number to the Check Acquired Skill Array

-Run a loop check with the array containing all the NPCs and spawn the one the random number showed.

Would something like this work and if so, how do I make changes to an array like that or am I overcomplicating things way too much and the answer is probably really simple?
I would actually use this script.

Random Number Removed From An Array. You would use the Script Event Command option on tab 3.


random = Math.randomInt($gameVariables.value(49).length);

$gameVariables.setValue(51, $gameVariables.value(49)[random]);

$gameVariables.value(49).splice(random, 1);


Here is an explanation of this. $gameVariables.value(49) is your array (Variable 49) that you already established before this script. The $gameVariables.setValue(51, is referencing the Variable (51) that you will use to store the random number from the array. The last line with .splice is what removes the random value chosen from the array (Variable 49).

Of course you can use any Variables that you want and just change the script values.

So you have an array (Variable 49) that contains the elements/values [1, 2, 3, 4, 5]. The script randomly selects a value from that array and assigns the value to Variable 51. Let's say it selects 3. Now Variable 51 = 3. Then the script removes the value of 3 from the array (Variable 49) and now it contains [1, 2, 4, 5].

The next time you use this script, it will randomly select from the remaining 4 elements/values.

You can continue to do that until the array is empty.

You can then fill the array back up by just declaring it again. Variable 49 = [1, 2, 3, 4, 5] if you needed to do that.

If you want to experiment with this, just use the Text Command to check what is happening.

Use \V[49] to see the array contents. Use \V[51] to see the value of the variable.

You can do that before the script runs and then after so you can confirm the changes.

You can use the value of Variable 51 as a Condition for your Event pages.

I hope I explained that ok.
I would actually use this script.

Random Number Removed From An Array. You would use the Script Event Command option on tab 3.


random = Math.randomInt($gameVariables.value(49).length);

$gameVariables.setValue(51, $gameVariables.value(49)[random]);

$gameVariables.value(49).splice(random, 1);


Here is an explanation of this. $gameVariables.value(49) is your array (Variable 49) that you already established before this script. The $gameVariables.setValue(51, is referencing the Variable (51) that you will use to store the random number from the array. The last line with .splice is what removes the random value chosen from the array (Variable 49).

Of course you can use any Variables that you want and just change the script values.

So you have an array (Variable 49) that contains the elements/values [1, 2, 3, 4, 5]. The script randomly selects a value from that array and assigns the value to Variable 51. Let's say it selects 3. Now Variable 51 = 3. Then the script removes the value of 3 from the array (Variable 49) and now it contains [1, 2, 4, 5].

The next time you use this script, it will randomly select from the remaining 4 elements/values.

You can continue to do that until the array is empty.

You can then fill the array back up by just declaring it again. Variable 49 = [1, 2, 3, 4, 5] if you needed to do that.

If you want to experiment with this, just use the Text Command to check what is happening.

Use \V[49] to see the array contents. Use \V[51] to see the value of the variable.

You can do that before the script runs and then after so you can confirm the changes.

You can use the value of Variable 51 as a Condition for your Event pages.

I hope I explained that ok.
I changed from skill NPCs to skill books which are items instead of making a bunch of event NPCs (This just saves time). Is there a way for me to call on an item by plugging in the item ID?

So the random number I get is the same number as the item ID is there some sort of function where I can just put the ID in and it will add that item to the player's inventory? If so, how would I do that?
The MV/MZ Script Calls excel spreadsheet (which I refer to all the time) has entries for adding and removing items, weapons, and armor. Here they are:


// Give the party 5x Item ID 1
$gameParty.gainItem($dataItems[1], 5);

// Remove up to 1 of Item 12 from the party inventory
$gameParty.loseItem($dataItems[12], 1);

// Give the party 1x Weapon ID 1
$gameParty.gainItem($dataWeapons[1], 1);

// Remove up to 99 of Weapon 2, including equipped items
$gameParty.loseItem($dataWeapons[2], 99, true);

// Give the party 2x Armor ID 1
$gameParty.gainItem($dataArmors[1], 2);

// Remove up to 5 of Armor 2, ignoring equipped items
$gameParty.loseItem($dataArmors[2], 5);


I just tested this in MZ.

Where you see the numbers in the brackets "[1]" those are referencing the ID in the database. Usually, when you have a number like that in a script you can replace it with the value of a variable like this:

[$gameVariables.value(1)]

So you are changing it from a specific ID that you designate in the script (1) to the value of Variable 1 instead.

You could test this in a quick event. Look in your database and find an item, weapon, or armor that you want to try.

Have one Event use the script to add it and then look in your inventory. Then have another Event use the script to remove it and then look in your inventory.

Again, I just tested this using a random variable value. Here are the screenshots of my test and it worked fine. The test I did was with 3 types of potions that are IDs 7, 8, and 9 in the default database.

Spoiler: Add & Remove Script Test
Add&Remove1.pngAdd&Remove2.png

I hope this helps.
The MV/MZ Script Calls excel spreadsheet (which I refer to all the time) has entries for adding and removing items, weapons, and armor. Here they are:


// Give the party 5x Item ID 1
$gameParty.gainItem($dataItems[1], 5);

// Remove up to 1 of Item 12 from the party inventory
$gameParty.loseItem($dataItems[12], 1);

// Give the party 1x Weapon ID 1
$gameParty.gainItem($dataWeapons[1], 1);

// Remove up to 99 of Weapon 2, including equipped items
$gameParty.loseItem($dataWeapons[2], 99, true);

// Give the party 2x Armor ID 1
$gameParty.gainItem($dataArmors[1], 2);

// Remove up to 5 of Armor 2, ignoring equipped items
$gameParty.loseItem($dataArmors[2], 5);


I just tested this in MZ.

Where you see the numbers in the brackets "[1]" those are referencing the ID in the database. Usually, when you have a number like that in a script you can replace it with the value of a variable like this:

[$gameVariables.value(1)]

So you are changing it from a specific ID that you designate in the script (1) to the value of Variable 1 instead.

You could test this in a quick event. Look in your database and find an item, weapon, or armor that you want to try.

Have one Event use the script to add it and then look in your inventory. Then have another Event use the script to remove it and then look in your inventory.

Again, I just tested this using a random variable value. Here are the screenshots of my test and it worked fine. The test I did was with 3 types of potions that are IDs 7, 8, and 9 in the default database.

Spoiler: Add & Remove Script Test

I hope this helps.
It's helped a lot. This is the script that I ended up with for my skill chests.

random = Math.randomInt($gameVariables.value(6).length);



$gameVariables.setValue(8, $gameVariables.value(6)[random]);



$gameParty.gainItem($dataItems[$gameVariables.value(6)[random]], 1);



$gameVariables.value(6).splice(random, 1);

Where variable 6 is my array containing the ID numbers of the skill books and variable 8 contains the random number. Thanks a bunch for your help.

Original thread: https://forums.rpgmakerweb.com/threads/how-to-randomize-npc-spawn-and-get-them-to-give-skills.174430/