How to create an event that randomly chooses from 100 vars with no repeat?

You're checking to see if the jokes are all done before splicing one out, the numbers you're stating all seem to match up, and the script calls look OK. I'm not sure why it would skip one of them. Is it always the same one that gets skipped?

Like Turan says, the new Array(140) command should be removed. It does generate the wrong range of values (1~140 instead of 1801~1904), but that's irrelevant because you exit the event right afterwards. It's just unnecessary.


Oh. That's what I get for trying to keep track of too many threads at once! 😴

I did a quick search and didn't find an MV version of triacontane's plugin. It should be pretty simple, though.
Spoiler: plugin and walkthrough
Try saving this as a .js file, then import as a plugin:
JavaScript:
/*:
 * @target MV MZ
 * @plugindesc Load custom JSONs into global vars, for access via script call.
 * @author Caethyril
 * @url https://forums.rpgmakerweb.com/threads/163256/
 * @help Edit the plugin code to customise variable & file name pairs.
 *
 * Free to use and/or modify for any project, no credit required.
 */
DataManager._databaseFiles.push(
    // Load Stuff.json into $dataStuff on game boot
    { name: "$dataStuff", src: "Stuff.json" }
);
As noted in the plugin help, you can rename it to $dataJokes and Jokes.json or whatever you like by editing the code.

Then if the JSON file looks like this (save it as a .json file in your project's data folder):
JSON:
[
  "Index 0",
  "Index 1",
  "Index 2"
]
You should be able to access it in-game like this:
JavaScript:
$dataStuff[0]  // "Index 0"
You would still need an array to splice out of, though, because $dataStuff should remain constant during play. So, something like this:
  1. Control Variables 1498 -> Script (based on length of $dataStuff):
    JavaScript:
    Array.from($dataStuff, function(_, n) { return n; })
    This gets a list of array indices for each entry of $dataStuff. Checking for "all jokes done" will work the same as before.

  2. Splice out of variable 1498 just like before, into variable 1499.

  3. Get joke text for selected index:
    JavaScript:
    $dataStuff[$gameVariables.value(1499)]
    If you don't care about keeping hold of the joke index at this point then you could just store this text in variable 1499 (i.e. replace the joke index number with the respective text). Otherwise you'd want to store it in a third variable (1500?) so you can display it via the \v text code.


It's a test message: it shows the selected joke ID (value of var 1499), followed by the respective joke's text (value of var with ID equal to value of var 1499).

So I re-ran the full test on all the jokes, and confirmed that all of the jokes do get represented with the event as presently written.

Here's a souvenir image of the two full test results because they took so long they deserve to be memorialized SOMEWHERE :LZSblush:

1702550152637.png


I completely misunderstood what new Array(140) did, I understand better now. This is my first time working with arrays, it will be a continuing education I'm sure.

@caethyril your plugin works perfectly, I just have two quick questions, since I'll be porting over the (actual thousands of) static strings from vars to this database.

First, I'm a little unclear where I would plug in the number of entries here:

Control Variables 1498 -> Script (based on length of $dataStuff): Array.from($dataStuff, function(_, n) { return n; })

Let's say I have 134 entries here, which I do in the current case -- what would that script look like in that case?

My other question has to do with how I would manage one of these little DB's in a case where the entries get cycled through in order, which is another use case relevant to my project. In that case, I have hundreds of text strings that would get called in order, rather than the trickier random-and-remove-from-array situation.

Thanks again for bearing with me, this is so enormously useful I am honestly kind of speechless. Thank you so much caeth for this little plugin too, I can't believe you can get so much functionality from such a tiny little piece of javascript. Kind of incredible imo.

-c
First, I'm a little unclear where I would plug in the number of entries here:

Control Variables 1498 -> Script (based on length of $dataStuff): Array.from($dataStuff, function(_, n) { return n; })
You don't need to change anything: it's "based on length of $dataStuff". That script call starts with $dataStuff, so it will produce a new array of the same length. The difference is that each entry will be filled with the index value (0, 1, 2, etc) instead of the original $dataStuff values.

My other question has to do with how I would manage one of these little DB's in a case where the entries get cycled through in order
You would determine the index differently. E.g. instead of storing an array of all indices that haven't been selected yet, simply store the current index and increase it by 1 each time. Then use that index (stored in, say, variable ID 123) to fetch the value as usual, e.g.
JavaScript:
$dataStuff[$gameVariables.value(123)]
Also, in case it wasn't clear: you can add more custom JSON files by editing the plugin code, e.g.
JavaScript:
DataManager._databaseFiles.push(
    // Load Stuff.json into $dataStuff on game boot
    { name: "$dataStuff", src: "Stuff.json" },  // <- comma!
    // Also load MoreStuff.json into $dataExtras on game boot
    { name: "$dataExtras", src: "MoreStuff.json" }
);

I can't believe you can get so much functionality from such a tiny little piece of javascript
Technically you can't. It can only be that short in this situation because the existing core scripts do all the heavy lifting: loading from disk into RAM on game boot and waiting for all files to finish loading before continuing.

Thanks again for bearing with me
No problem! It's also handy in case anyone else has a similar question. 👋
This thread is closed due to being solved. If you need it reopened, report it to let us know why. Thank you!
This thread is reopened at the user’s request.
So I have a follow-up question related specifically to @caethyril 's little database plugin, thanks to mods for reopening -- since this thread is linked in the plugin itself, it seemed most appropriate to keep it here.

As I mentioned previously, I'd love to use this plugin to return straight up incremental indexes. So, rather than randomness and splicing from an array, it just needs to return index 0, then ++ over and over.

Then, if there are no more indexes, I can do a little show text of "there's no more".

I've been trying to build this event for a long, long time now and I'm not getting anywhere, and I just know I'm overthinking this and it ought to be pretty simple. So I was wondering more specifically how I would set a variable to the contents at index 0, then increment the index by 1, and then set the same variable to the new contents. Sorry, I know I don't have the language right.

Many thanks for your help!

EDIT: Never mind :LZSangel: I was just overthinking it MASSIVELY.

2023-12-21 20_24_57-Window.png


This is all it took. Ultra simple. Reporting as re-solved, pardon the trouble!

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/how-to-create-an-event-that-randomly-chooses-from-100-vars-with-no-repeat.163256/