Help with deleting an item from an array.

I need to pull a string from an array, then delete the string from the array after it's pulled so that it can't get pulled again next time.

As an example, if the array contains

0- "Hi"
1- "Hey"
2- "What's up"

Then a random one of those will be picked when you talk to an NPC. after you talk to them, I want whichever one was chosen to then be deleted from the array permanently.

I can't figure out how to do it because my javascript knowledge is still basic.
It depends on what you want the end result to be. It can either leave an undefined element, return a new array, etc.

You can read on different methods that can be used in the Array object here: https://www.w3schools.com/js/js_array_methods.asp

Check out array.splice() , it's probably what you are looking for. You can also try the delete method, but people usually recommend not using it since it leaves a hole in the array.
Thanks, this is almost what I need! In my game though, when you speak to a certain NPC then what they say is randomly picked from a small array.

Now my issue is that I don't know how to tell it to splice whatever was just selected as the random string from the array.


var pool = ["A","B","C"];

$gameVariables.setValue(20, pool[Math.floor(Math.random() * pool.length)]);


If I were to use
pool.splice(0, 1);

It would just take out the first thing, and if i just tell it to take out a random thing, it probably won't pick the same one that was just picked before.
Thanks, this is almost what I need! In my game though, when you speak to a certain NPC then what they say is randomly picked from a small array.

Now my issue is that I don't know how to tell it to splice whatever was just selected as the random string from the array.


var pool = ["A","B","C"];

$gameVariables.setValue(20, pool[Math.floor(Math.random() * pool.length)]);


If I were to use
pool.splice(0, 1);

It would just take out the first thing, and if i just tell it to take out a random thing, it probably won't pick the same one that was just picked before.

I don't think your variable is set right, it would only ever return 0 or the array length, use
Code:
$gameVariables.setValue(20, pool[Math.randomInt(pool.length)]);
instead. Then use
Code:
pool.splice($gameVariables.value(20), 1);
As the variable is already set to the index.

EDIT: Or maybe the Math.random() does work too.
edit out maybe I'm wrong
This lets you use just the array of each NPC instead of having to fill up your global variables:
JavaScript:
function pickNSplice(array) {
    i = Math.floor(Math.random() * array.length);
    line = array[i];
    array.splice(i, 1);
    return line;
}

So you call the function with whatever array you want to pluck a random entry from. Put that just like it is inside window.onload in main.js or make it its own .js and load as a plugin. Or you can call it in a script call when the game begins, you'll figure it out!

For example the NPC's script calls could then look like this. Call this only once as a sort of a setup for the lines they can say, and then turn it off with a self switch:
JavaScript:
$gameMap.event(this.eventId()).pool = ["Hey!", "You there!", "You need to polish your armor!"];
If it's not disabled by a self switch it will just reset the array the next time you talk with the NPC and that would defeat the whole purpose.

Then
JavaScript:
pickNSplice($gameMap.event(this.eventId()).pool);
will give you the random entry. So maybe you set a variable to that function so you can use it in a text message or however you wanna use it 😴
Hi!
Well, I think it's best for you if you work with two arrays:
1 - An array that contains all possible options.
2 - An array that contains all options chosen.

With this, I used the array.prototype.some method to check:
JavaScript:
let allOptions = ["Hi", "Hey", "What's up"];
let chosenOption = [];

// Let's build a method that checks if the player already chose that option first:
function hasChosen(option){
  return chosenOption.some(item => item == allOptions[option])// If the player has already have that choice, then do nothing.
}

// Now let's make another method. If the player has does not have chosen the option, this will add it to the chosenOptions
function selectOption(option){
  if(hasChosen(option)) return;
  chosenOption.push(allOptions[option]);
}

// Let's execute it!
selectOption(1);

// Let's execute again and see if it will add the same option!
selectOption(1);

//Let's check the options that the player already chose
console.log(chosenOption)

If you follow this logic you will make it 🙂
But I do prefer to make this using objects instead of an array, like this:
let allOptions = {
Option1: "Hi",
Option2: "Hey",
Option3: "What's up"
}
Man, everything i want to do for my game is way beyond my abilities, I need to level up my javascript somehow. At least I can understand this stuff. Thank you all for your advice, I'm a writer so the dialogue in my game is the main focus for me, I want to do interesting things with it.
You can use the arrayObject.filter() method to remove elements from an array at specific index in JavaScript.

Code:
var rValue = 'three'
var arrayItems = ['one', 'two', 'three', 'four', 'five', 'six']
arrayItems = arrayItems.filter(item => item !== rValue)
console.log(arrayItems)

Original thread: https://forums.rpgmakerweb.com/threads/help-with-deleting-an-item-from-an-array.119360/