Select Random NPC, but do not Repeat when selected

Hi Guys,


Just wanted to ask a very stupid question..


So, at the beginning of the game there are 3 Identical statues. When the player touches the statue it will turn into one of three NPC's


1) Dwarf


2) Prince


3) Frog


The problem is that, if I use just RANDOM variables for each statue, there is a possibility that, the next statue might have the same NPC as the previous statue. Each statue should turn into a NPC, that is different from one another.


I guess my question is, how can I delete a Variable, once it is randomly selected? And how do you do this, lets say if I have 10 statues? 


Thanks in advance, arigatou gozaimasu!
You could make random variables on the first statue between 1~3. If its for example 3 it turns into a frog, the variable is still saved. Then you can make a system (which you must have on all the statues of course).


If variable was 3 (because its still saved) make it random between 1~2


If variable was 2 make it random between 1~2 and if it choses 2 you make the variable=3


If variable was 1 make it random between 2~3


Hope this helps (or I'm confusing).
I think you can use the Switches also to trigger if that Statue is already taken.


and 1 variable only.



Try this one:


variable_Statue.Random();


if (variable_Statue == 1 and Dwarf_switch == OFF){


    Dwarf_switch = ON;


}


else if  (variable_Statue == 2 and Prince _switch == OFF){


    Prince _switch = ON;


}


else{


  text.print(all of the statue is activated);


};


you can add more (else if).


Note: this is not a script call just use the command to this. This is a simple algorithm. hahaha!


for me it's a good example.


And you cannot delete the value on the variable just set the value into 0.
I think you can use the Switches also to trigger if that Statue is already taken.


and 1 variable only.



Try this one:


variable_Statue.Random();


if (variable_Statue == 1 and Dwarf_switch == OFF){


    Dwarf_switch = ON;


}


else if  (variable_Statue == 2 and Prince _switch == OFF){


    Prince _switch = ON;


}


else{


  text.print(all of the statue is activated);


};


you can add more (else if).


Note: this is not a script call just use the command to this. This is a simple algorithm. hahaha!


for me it's a good example.


And you cannot delete the value on the variable just set the value into 0.



Thank you for the reply. How about if its more than 3? Say like 10 statues? Then its going to become very complex? Any ideas? Sorry for the questions, I am new to this. I read something about Arrays, that could handle like 100 random variables, is it applicable to my scenario?
You could make random variables on the first statue between 1~3. If its for example 3 it turns into a frog, the variable is still saved. Then you can make a system (which you must have on all the statues of course).


If variable was 3 (because its still saved) make it random between 1~2


If variable was 2 make it random between 1~2 and if it choses 2 you make the variable=3


If variable was 1 make it random between 2~3


Hope this helps (or I'm confusing).



Thanks for the reply. What if I have 10 statues? IS there a way to simplify this?
Thank you for the reply. How about if its more than 3? Say like 10 statues? Then its going to become very complex? Any ideas? Sorry for the questions, I am new to this. I read something about Arrays, that could handle like 100 random variables, is it applicable to my scenario?



Add more conditions. I think you should need a plugin for that. but if you don't want you can do it manually by setting it like if you have 10 statues set 10 conditions, 5 statues > 5 conditions. just like that.
You don't need a plugin (or script in this case, as you're asking about Ace).  You mentioned in your PM that you found something on this already, but it didn't work when you tried it.  Can you post a link to that thread, and let us know what happened when you tried it?  Screenshots of the events that handle the arrays would also help us figure out where the issue is.


What I'm thinking is one event that will run once only, to set up all the NPCs available, then a common event to select an available NPC from the list, remove it, and set a variable to that NPC's "id".  Then your statue event will have one page for each possible NPC, each one conditioned by the variable being equal to or greater than a certain value (the NPC's "id") as well as an initial page for the statue, which will call the common event.  All of the statues will be exactly the same, except the variable used to condition the pages - no two statues will share the same variable id.


If you need more help, I'll go into further detail after you provide that link I requested.
tushime1976, please avoid double posting, as it is against the forum rules. You can review our forum rules here. Thank you.


If you want to add something, then edit your previous post.


Also, it is not necessary to quote whole posts when replying.  The convention @username at the beginning of your reply is enough to make it clear who you are answering.  This also makes scrolling down the page easier and quicker.


Thank you.
You don't need a plugin (or script in this case, as you're asking about Ace).  You mentioned in your PM that you found something on this already, but it didn't work when you tried it.  Can you post a link to that thread, and let us know what happened when you tried it?  Screenshots of the events that handle the arrays would also help us figure out where the issue is.


What I'm thinking is one event that will run once only, to set up all the NPCs available, then a common event to select an available NPC from the list, remove it, and set a variable to that NPC's "id".  Then your statue event will have one page for each possible NPC, each one conditioned by the variable being equal to or greater than a certain value (the NPC's "id") as well as an initial page for the statue, which will call the common event.  All of the statues will be exactly the same, except the variable used to condition the pages - no two statues will share the same variable id.


If you need more help, I'll go into further detail after you provide that link I requested.





Shaz thank you for the reply. Link below.


When I try the script, nothing happens, it does not delete the variable in the array.


Anyway, your suggestion is what I was trying to do (one event at start to select the NPC's for all), but to no avail. If you can teach me how to set it up it will solve this problem of mine once and for all. Just a question, will this selection be random? Because if possible I want a new set of NPC's once the player starts a new game.


Thanks in advance!
Can you please post screenshots showing where you set the array up, and where you retrieve a value from it for the statue?  If the script isn't fully shown in the screenshot, please also edit the command so you can cut & copy here so I can see the full thing.


I notice that thread is quite an old one, and I gave a version of the script call that would have included a line break, for people who had an older version of the engine, and the line break has been removed with the change to the new forum software.  If that were an issue for you though, I'd expect it to throw an error.
Can you please post screenshots showing where you set the array up, and where you retrieve a value from it for the statue?  If the script isn't fully shown in the screenshot, please also edit the command so you can cut & copy here so I can see the full thing.


I notice that thread is quite an old one, and I gave a version of the script call that would have included a line break, for people who had an older version of the engine, and the line break has been removed with the change to the new forum software.  If that were an issue for you though, I'd expect it to throw an error.



Hi Shaz,


Unfortunately I deleted it already two days ago, so I cannot provide any screenshots. The truth is, I don't really know where to start.


So please let me show you my plan, according to your suggestion, which I think is the easiest way:


1) Set up an event at the start of the game that will select the NPC (list 1)


2) Set up a common event to select a random  NPC from list 1, this case "Princess"


3) Remove the selected NPC (Princess) from list 1


4) Set a variable for Princess


5) Statue turns to Princess according to the pages.


I can do 4 and 5, but for 1-3, I have no idea how to do it. 
Actually, that whole thing is all condensed onto a single line, so if you only recently found that thread and copied it exactly, then it wouldn't work (but I'd still expect it to give you an error).


This might be easier ... in your game starting event (create one if you don't already have one, and make it turn on a self switch to activate a new page; otherwise just add the following to your existing starting event).  Let's assume your statue variables are going to be numbered 11 to 20.  If you have different numbers, just change the 11..20 line below:

Code:
npc = [1,2,3,4,5,6,7,8,9,10]
for i in 11..20
  $game_variables[i] = npc.delete_at(rand(npc.size))
end



Then create one statue.  Page 1 will be the statue sprite and it will have a single command - to turn on self switch A.  Page 2 will have the sprite for NPC1 and will be conditioned by self switch A and variable 11 => 1.  Page 3 will have the sprite for NPC2 and will be conditioned by self switch A and variable 11 >= 2.  Page 4 will have the sprite for NPC3 and will be conditioned by self switch A and variable 11 >= 3.  And so on until page 11, which will have the sprite for NPC10 and will be conditioned by self switch A and variable 11 >= 10.  So however may elements in the array in that script, you will have that many pages plus 1.  Any other event commands you want on the NPC should be added to those pages as well (if they're all similar, I'd be creating just one, then copy/pasting it and changing what needs to be changed).


Once the first event is set up and tested and working correctly (as long as that start-of-game event has been run, it should change to SOME NPC when you interact with it), copy and paste it for as many NPCs as you need.  Then go into each one and change the variable on all pages to the next variable used above.  So statue 1 will use variable 11 on all its pages, statue 2 will use variable 12 on all its pages, etc.
Actually, that whole thing is all condensed onto a single line, so if you only recently found that thread and copied it exactly, then it wouldn't work (but I'd still expect it to give you an error).


This might be easier ... in your game starting event (create one if you don't already have one, and make it turn on a self switch to activate a new page; otherwise just add the following to your existing starting event).  Let's assume your statue variables are going to be numbered 11 to 20.  If you have different numbers, just change the 11..20 line below:

Code:
npc = [1,2,3,4,5,6,7,8,9,10]
for i in 11..20
  $game_variables[i] = npc.delete_at(rand(npc.size))
end



Then create one statue.  Page 1 will be the statue sprite and it will have a single command - to turn on self switch A.  Page 2 will have the sprite for NPC1 and will be conditioned by self switch A and variable 11 => 1.  Page 3 will have the sprite for NPC2 and will be conditioned by self switch A and variable 11 >= 2.  Page 4 will have the sprite for NPC3 and will be conditioned by self switch A and variable 11 >= 3.  And so on until page 11, which will have the sprite for NPC10 and will be conditioned by self switch A and variable 11 >= 10.  So however may elements in the array in that script, you will have that many pages plus 1.  Any other event commands you want on the NPC should be added to those pages as well (if they're all similar, I'd be creating just one, then copy/pasting it and changing what needs to be changed).


Once the first event is set up and tested and working correctly (as long as that start-of-game event has been run, it should change to SOME NPC when you interact with it), copy and paste it for as many NPCs as you need.  Then go into each one and change the variable on all pages to the next variable used above.  So statue 1 will use variable 11 on all its pages, statue 2 will use variable 12 on all its pages, etc.



Tried the script, but got these error message, I tried to do 4 NPC first to make it easier:

script_1.png

error.png
put the line break between ( and rand:

Code:
$game_variables[i] = npc.delete_at(
rand(npc.size))



You must have an older version of Ace.  If you redownload from wherever you purchased, you'll get a larger script box, and that won't be an issue as often.


and please don't quote me every single time - it makes it hard to read the thread.  If you want to notify someone that you've replied, just do @theirname
@Shaz


Now its working, but there is a problem, the event only selects the last NPC on the page.


So I created 3 variables, bear, chicken and cow. It is always the NPC "Cow" that is always showing (Page 4 of the statue). 

2ndpage.png

1stpage.png
The variable needs to be used in the Conditions, not in a Conditional Branch.
@Shaz


Thank you! I am just trying MV now and its really great, I am planning to "migrate" my game to it. Is the Script the same to generate and delete the random NPC?

Code:
$game_variables[i] = npc.delete_at(
rand(npc.size))
no.  In MV, the script would look something like this:

Code:
var npc = [1,2,3,4,5,6,7,8,9,10];
for (var i = 11; i < 21; i++) {
  var j = Math.randomInt(npc.length);
  $gameVariables.setValue(i, npc[j]);
  delete npc[j];
}
Thanks problem solved.

Original thread: https://forums.rpgmakerweb.com/threads/select-random-npc-but-do-not-repeat-when-selected.66020/