Random location for NPC event

Hello again everyone, and happy new year.
I'm trying to let the game generate a random number (0-3), based on the outcome, the NPC (Called Leo) will move to one on four locations (3,10; 5,10; 7,10; and 9,10).
After that, the outcome will be removed from the array, and loop again till no elements are in the array.
This is what I have now with a script:

let myArray = [0,1,2,3]
let x = Math.floor((Math.random() * 4) + 1);
myArray.splice(x);

The problem is that at this point, I don't know if to copy x to a Game Variable 0042 (and I don't know how to do that) and work with inbuilt functions, or to continue with the script (and I don't know how to do that).

Do you have any suggestion?
Thank you in advance 🙂
let x doesn't work as it target nothing!

as you need to target the array like

let x = Math.floor(Math.random() * myArray.length);

so your array is targetted, but if you have 4, I would do it differently
based of x and y, so you need more for [[x,y][x,y],x,y]]
than random index for array[0][1] from this to have the x,y assigned.
We want the game to remember which locations are still valid, so we might initialize the array and assign that into a variable.
The script call would probably look like this:
Code:
//Initialize the list of locations
$gameVariables.setValue(varId, [0, 1, 2, 3]);
Code:
//Get a random valid location
let array = $gameVariables.value(varId);
let location = array.splice(Math.floor(Math.random() * array.length), 1)[0];

//Set that location on to a new variable location
$gameVariables.setValue(varId2, location);
Thank you both 🙂
We want the game to remember which locations are still valid, so we might initialize the array and assign that into a variable.
The script call would probably look like this:
Code:
//Initialize the list of locations
$gameVariables.setValue(varId, [0, 1, 2, 3]);
Code:
//Get a random valid location
let array = $gameVariables.value(varId);
let location = array.splice(Math.floor(Math.random() * array.length), 1)[0];

//Set that location on to a new variable location
$gameVariables.setValue(varId2, location);


With "varId" you mean to use one in the engine? Because it gives me "ReferenceError. varId is not defined"
Thank you both 🙂



With "varId" you mean to use one in the engine? Because it gives me "ReferenceError. varId is not defined"
Yes, replace that with whatever index you assign. Same with varId2
if you use it as scriptcall, you can use it if you do

let varId = 3;
let varId2 = 4;

and it works as well, but that's up to you,
so you can basicly reuse it constantly if you change the numbers this way.
mmmm... you are saying:
- let 42 = 3;
create a variable 42 (0042 on RPG Maker) and set the value 3
- let 43= 4;
create a variable named 43 (0043 on RPG Maker) and set the value 4
- $gameVariables.setValue(42, [0, 1, 2, 3]);
set the variable 42 with an array [0, 1, 2, 3]
- let array = $gameVariables.value(42);
create a variable called array with the contents of variable 42
- let location = array.splice(Math.floor(Math.random() * array.length), 1)[0];
create a variable called location that is a random element of the array and then erase that element

?
Hi again, I went a bit on:
// Declare randomSlots as a global variable
let randomSlots = [0, 1, 2, 3];

function pickRandomAndSetVariable() {
if (randomSlots.length > 0) {
// Pick a random element from the array
let randomIndex = Math.floor(Math.random() * randomSlots.length);
let randomValue = randomSlots[randomIndex];

// Remove the picked element from the array
randomSlots.splice(randomIndex, 1);

// Set the value of game variable 0042
$gameVariables.setValue(42, randomValue);

// Print the array and the variable 0042 values
console.log("RandomSlots array:", randomSlots);
console.log("Variable 0042 value:", $gameVariables.value(42));
}
}

// Example: Triggering the function
pickRandomAndSetVariable();

This on is pretty ok with what I want. I'll set a function based on the value of 0042 to move the NPC to coordinates (or use coordinates straight away). The problem is that I want the NPC to move to all the destinations, so to make it recursive till the array is empty.
Should I use the loop function in the events?
The problem is that I want the NPC to move to all the destinations, so to make it recursive till the array is empty.
Should I use the loop function in the events?
What are your plans for movement? Immediately (like teleportation) to each destination or move to each destination tile by tile? And, will it be while something else is happening or will the player have to wait like something similar to a cutscene?
What are your plans for movement? Immediately (like teleportation) to each destination or move to each destination tile by tile? And, will it be while something else is happening or will the player have to wait like something similar to a cutscene?
Hi! I'll be moving to destination while the player moves the main character (pretty much the goal is to find some object. First win, more or less).
To make it simple.
Let's say that both MC and NPC need to reach 4 tables and find one object (the first touching the right table). I want to set the NPC to randomly choose the order of the tables.
I can't fully help at the moment, but it looks like a parallel event with a move route (with a wait for completion for that route) may work.

Something like this in the move route (i'm typing it out but make sure you use the script call button in the list. Put what you need in x (the variable array) and since we know what y is, just leave it at 10):
Code:
◇ Script : this.moveStraight( this.findDirectionTo(x, 10) )
◇ Script : // However you are removing the value from the game variable array

Are you putting your function in as a plugin or using a script call for all of it? I would avoid defining functions with the eval script calls and just go ahead and make Variable 42 an array that you can push and pull from. Once it's an array, you can work with it like you do any array (splice, indexOf, etc.)

I can run some tests when I get home if I get the chance.
I can't fully help at the moment, but it looks like a parallel event with a move route (with a wait for completion for that route) may work.

Something like this in the move route (i'm typing it out but make sure you use the script call button in the list. Put what you need in x (the variable array) and since we know what y is, just leave it at 10):
Code:
◇ Script : this.moveStraight( this.findDirectionTo(x, 10) )
◇ Script : // However you are removing the value from the game variable array

Are you putting your function in as a plugin or using a script call for all of it? I would avoid defining functions with the eval script calls and just go ahead and make Variable 42 an array that you can push and pull from. Once it's an array, you can work with it like you do any array (splice, indexOf, etc.)

I can run some tests when I get home if I get the chance.
Thank you, for the moving function I was thinking about the YanFly plugin.
Is the recursive random element-erase element form the array that cause me problem 🙁
Thanks for your patience! This is what I have. Please change whatever works for you - I believe this or something like this is what you were trying to do.

Whether or not it works, feel free to pull/optimize/change what you need. I wasn't moving the player around since I was busy getting the console to provide data, but the player is able to move during this process.

Currently, there are no noticeable errors, though make sure to test and adjust accordingly.

The console.log should show you the X coordinates that it has "randomized" - it's not the best way to do it but it's something usable and you don't really have a big pool of data to randomize.



s1.png

s2.png

s3.png

Edit: changed the last photo with correct wait time. Also, in the script "this.moveStraight..." on page 2, it needs to be $gameVariables.value(42)[0] and not v[42][0]. Though after more testing, it looks like I can use short hand without defining v in a move route - interesting.

Here is a reference for the first wait frame command on page 2:
Speed 1: 120 wait frames
Speed 2: 60 wait frames
Speed 3: 30 wait frames
Speed 4: 15 wait frames
Speed 5: 7.5 wait frames
Speed 6: 3.75 wait frames
Thanks for your patience! This is what I have. Please change whatever works for you - I believe this or something like this is what you were trying to do.

Whether or not it works, feel free to pull/optimize/change what you need. I wasn't moving the player around since I was busy getting the console to provide data, but the player is able to move during this process.

Currently, there are no noticeable errors, though make sure to test and adjust accordingly.

The console.log should show you the X coordinates that it has "randomized" - it's not the best way to do it but it's something usable and you don't really have a big pool of data to randomize.



View attachment 291025
View attachment 291026
View attachment 291027
Edit: changed the last photo with correct wait time.

Here is a reference:
Speed 1: 120 wait frames
Speed 2: 60 wait frames
Speed 3: 30 wait frames
Speed 4: 15 wait frames
Speed 5: 7.5 wait frames
Speed 6: 3.75 wait frames

Oh, wow, thanks.
I'll have a look at it tomorrow with a fresh mind and eyes 😀
Thank you very much again for the help everyone. This community is always awesome 🙂

Original thread: https://forums.rpgmakerweb.com/threads/random-location-for-npc-event.164526/