Complicated Common Event Help

Good evening,

I've been working on a game where, at midnight each night, a player-selected character is sacrificed to a monster and a random townsperson is murdered by a killer.
I'm having a lot of trouble figuring out how to map out the common event for selecting the killer's victim.

The first few nights it works, but it always ends up choosing a variable of someone who is already dead or the killer in the town him or herself.

I need the event to know to reselect the variable until it's chosen someone eligible to be the killer's victim. I've tried it with a loop (which I'd admittedly never practiced with before), and I tried it with calling the same common event again when it chooses an ineligible townsperson (which resulted in a common event exceeds 100 error).

Any advice or suggestions would be so appreciated!

I'll post a screenshot of part of the common event below.
you'll need to use an array to store the available targets and remove any already selected elements from the array. And that needs script commands.

please search the forum - someone else asked about the same problem (just with drawing cards) a short while ago and got the solution, I just don't have the link.
Spoiler: Code
Code:
var arr = [];
if($gameVariables.value(19) == 0)
  $gameVariables.setValue(19,arr);
var rand = -1;
while(rand == -1 || $gameVariables.value(19).contains(rand)){
 rand = Math.randomInt(27) + 1;
}
$gameVariables.setValue(18,rand);
arr = $gameVariables.value(19);
arr.push(rand);
$gameVariables.setValue(19,arr);

If you don't wanna search for the other solution, I'm pretty sure this'll work for what you need too. It sets variable 19 as your array to keep track of who's died, and makes variable 18 the person who dies this time. Just be sure not to run it when everyone's been chosen or it'll loop.
Thanks for the advice so far! I've never attempted scripting myself before, but I think if I search for some tutorials I may be able to figure it out.
the link @Andar mean is this i think:

i think no need to loop the randomize at all...
just make an array then shuffle it... then the first sacrifice is the first array index(0)... second sacrifice is second array index(1)... it mean you only need to randomize once... no need to loop...

for example let's say the number of villager is 33 person.
use event command script call then put this code:
Code:
this._vilarr = [];
for (i = 1; i<=33; i++){
  this._vilarr.push(i);
};
this._vilarr.sort((a,b)=> 0.5 - Math.random());
you will got shuffled array villager 0->33
change the 33 in above code to number of villager you want...

then directly use event command change variable to store those to game variables
which you can use later on eventing...
let's say villager sacrifice order will stored at the variable 1-33.

change variable 1 -> set -> script:
Code:
this._vilarr[0];
to store the first day sacrifice...

then continue to next sacrifice change variable 2 -> set -> script;
Code:
this._vilarr[1];
to store the second day sacrifice

until the number of sacrifice order finished.

basically you can access the sacrifice with:
Code:
this._vilarr[day s/he die - 1];

hope this help...
just make an array then shuffle it... then the first sacrifice is the first array index(0)... second sacrifice is second array index(1)... it mean you only need to randomize once... no need to loop...

I think this is the best solution, but it won't really work because of the characters the player sacrifices, which (I'm guessing) should also be ignored by the killer.

You can skip the character when it's already dead or something, but at this point i think probably just randomly picking an element in the array and removing it would be better:

First initialize the array (only once, at the beginning of the killings):
JavaScript:
this._killable = Array.from({ length: 33 }, (_, i) => i + 1);

Then randomly pick an element from the set and remove it everytime you need the killer to take someone out:
JavaScript:
const index = Math.randomInt(this._killable.length); // pick a random index on the set
$gameVariables.setValue(18, this._killable[index]); // set variable to id
this._killable.splice(index, 1); // remove at the index you picked

Aditionally, remove the IDs of the characters that are sacrificed by the player (replace <ID> by the actual ID):
JavaScript:
this._killable.remove(<ID>)

---

Extra: If you're worried about being very efficient (doesn't seem to be necessary in this case, but who knows when you might need something like this to run with thousands of elements on a toaster xd), you could even improve this to do removals faster:

JavaScript:
const index = Math.randomInt(this._killable.length); // pick a random index on the set
$gameVariables.setValue(18, this._killable[index]); // set variable to id

// Replace the removed element with the last element on the list and pop it, since elements
// are picked at random anyway this makes no difference.
// Pop takes constant time vs linear for splice, this is much better if your array has a lot of elements.
this._killable[index] = this._killable[this._killable.length - 1];
this._killable.pop();

Its more lines and might be harder to follow the logic though, so if you just need it to work i'd suggest going with the former approach.

Removals for sacrifices can also be improved from linear (remove uses indexOf and splice, which are both linear) to constant with a hash map from IDs to indices on the array, and then applying the same logic to remove the chosen element (while being careful to maintain the hash map when doing swaps).
I'm going to give a bit more information.

During the intro sequence of my game, the game selects three variables among the townspeople. It sets variable Sacrifice to a random number between 1-27 (27 townspeople), the variable Pardoned to 1-27 (selecting again if it chooses the same number as the Sacrifice) and then finally a Serial Killer between 1-27 (selecting again if it chooses the same number of the Sacrifice or the Pardoned).

Those three roles already have variables.

Then I have a time system going. Your character chooses someone to have sacrificed. If they choose the correct townsperson, you win! (If you choose the Pardoned townsperson, you lose!) If they don't win or lose, the Serial Killer activates and also kills someone.

Then here is where I need the array I guess. Because I need the townsperson chosen to ignore the Sacrifice, the Pardoned, the Serial Killer and anyone who has already been sacrificed or killed by the serial killer on a previous day. (I think that means I need 27 variables for sacrifices and 27 variables for being killed)

So it looks like I have to make it so that the array has the variable removed for the character who is the sacrifice, the pardoned and the serial killer after those three variables are chosen during the intro. Then, I suppose I need to tell the array to remove the variables of those already killed by either the sacrifice or the serial killer each time that occurs.
I appreciate everyone being so helpful and kind! The Javascript is a lot over my head, though, so I'm wondering if it may be worth asking for help in the project recruitment section. Thank you so much again!
Thread closed

Original thread: https://forums.rpgmakerweb.com/threads/complicated-common-event-help.131794/