RMMV insert random item from array into text box

placebo3ffect

Warper
Member
Joined
Mar 6, 2021
Messages
4
Reaction score
1
First Language
English
Primarily Uses
RMMV
hi, i want to try to set up a system where i can insert a series of phrases into an array, assign a label to the lists depending on the event i want it associated with, then call that label as a command to insert a random phrase during specific battles between turns, sorta in an undertale like style.

i asked a friend of mine who knows java about how this would be set up, and he gave me this:

let numbers=[0,1,2,3,4,5,6,7,8,9];
print( numbers[int(random(0, numbers.length))];

I want to achieve something like that for mv, but have multiple arrays with different names (like battleslime, battlebat etc) to call on different phrases, preferably by adding <battleslime> as a function in the regular text editor like how text plugins work. somebody else told me to have variables have the phrases and assign them based on that, but I want to be able to quickly open up a text editor or the array and edit the phrases without having to go through a whole variable.

would this be possible to transfer into mv code? i don't know much about java, nor how the specific code is for mv, so any help is appreciated
 

Eliaquim

Hakuen Studio
Veteran
Joined
May 22, 2018
Messages
1,882
Reaction score
1,360
First Language
Portuguese - Br
Primarily Uses
RMMZ
Hi!

Do you mean something like every enemy in battle will have a random phrase?

Like, if you have a Slime and a Bat on the battle, then you will have:

JavaScript:
let slimePhrases = ["Kill him", "Oh no", "Who is the leader"]
let batPhrases = ["quiiick", "I'm flying", "I will bite you"]

let allPhrases = [SlimePhrases, BatPhrases]

Then take a random label (SlimePhrases or BatPhrases) and, from that label, take a random phrase?
 

Wavelength

MSD Strong
Global Mod
Joined
Jul 22, 2014
Messages
5,957
Reaction score
5,581
First Language
English
Primarily Uses
RMVXA
Moved to Learning JavaScript - that's probably the best place to ask questions like this.

Other members around me may have a much better way to do this, but the way I would go about it is:
  1. Create a hash (or a "mega-array") to hold an array of all of the possible phrases for each enemy (or each whatever you want to make phrases for), by placing them in a "plugin" javascript file (example of hash below)
  2. Before showing a text box, pick an appropriate phrase from the hash at random, and populate it into an Actor's name, by using a script line (inside a "Script" event command) like $gameActors.actor(10)._name = my_hash[3][Math.floor(Math.random() * my_hash[3].length)]; Note that the monstrously long line Math.floor(Math.random() * my_hash[3].length) is just picking a random index number from 0 to the length of my_hash[3]
  3. Use the Message Code \N[#] in the message box to show the Actor's name, which has been set to this phrase.
Example of Hash:
my_hash = {};
my_hash[3] = ["Hi, I'm a Slime", "I'm green with envy!", "Touch me and I squish sploosh"];
my_hash[8] = ["Hi, I'm a Bat", "I nibble on fruits and actors"];

There, we defined a hash and set its properties "3" and "8" to arrays. You could use any numbers or even "words" instead (my_hash["lollipop"] = ... ) Maybe 3 and 8 are the Enemy ID's of Slime and Bat, respectively. Then, when you set the Name in step 2, you could use something like $gameTroop.enemies()[0]._enemyId instead of 3 to look in whatever array is appropriate to the Troop Leader's Enemy ID in the database.
 

placebo3ffect

Warper
Member
Joined
Mar 6, 2021
Messages
4
Reaction score
1
First Language
English
Primarily Uses
RMMV
yeah, sorta. I actually figured out a makeshift way of doing what I was trying to do, but it's kinda inconvenient since i had to put the code in a common event, then call that common event during the battle.

(the common event for the enemy's phrases)
2021-03-05 20_39_23-Database.png

(the page in battle that calls the phrases)
1615006123692.png

while that works and all, the common event is only for one specific enemy instead of just having one huge script that assigns a label / command to the enemy's specific lines, so having to make a common event for each enemy is going to be pretty time consuming and will clog up the other event pages.

the selecting a random phrase in multiple enemies like you mentioned is also good, however i'm still trying to figure out how to do the single enemy first lol

(sorta something like this using your script)

let slimePhrases = ["Kill him", "Oh no", "Who is the leader"]
let batPhrases = ["ah", "wowie", "look its a bat"]
let bearPhrases = ["woah bear," "help"]

basically a bunch of arrays but in a single common event to be called from various other battles

then in the textbox, be able to call a random from whatever the list was for the command used, ex the word <slimePhrases> in a specific troop's event page like above

1615006209231.png

sorta like how text plugins require a command like that to add specific effects
 

placebo3ffect

Warper
Member
Joined
Mar 6, 2021
Messages
4
Reaction score
1
First Language
English
Primarily Uses
RMMV
Create a hash (or a "mega-array") to hold an array of all of the possible phrases for each enemy (or each whatever you want to make phrases for), by placing them in a "plugin" javascript file (example of hash below)
ok, i made a .js file with the arrays and the phrases.
1615006982102.png
and this was put into the event page of the battle

1615006958118.png
(i changed the # into 10 for the actor's name to be changed after screenshotting this)

however it's giving me an error about the script not being defined.

(it's cut odd for privacy reasons in the user name)
2021-03-05 21_06_09-Developer Tools - file____C__Users_Noir_Documents_Games_owo_index.html_test.png

1615007200750.png
how do you define the hash?
 

placebo3ffect

Warper
Member
Joined
Mar 6, 2021
Messages
4
Reaction score
1
First Language
English
Primarily Uses
RMMV
my_hash = {};

Based on your screenshot, it looks like you forgot that line before defining my_hash[3] and my_hash[8]. But it's possible you did, as I see there is at least something above those in the screenshot.
It works now!

1615011690360.png
it seems to be green because of an auto color plugin i have enabled, but i can easily turn off the auto coloring for actor names considering they never get used other than this.

thank you so much!!
 

Latest Threads

Latest Posts

Latest Profile Posts

Made the "My Beating Mystery" board today:
3ENFLSC.png

in Grim Trigger episodes 3 & 4, the main characters play a Clue rip-off board game called "My Beating Mystery" (haha), so of course I had to draw the board for it. Can't decide if I wanna add blood or not
Jeez...
I finally decide, after several ppl asking me to, to create a gamethread about project M... And my internet craps out on me halfway through the process :(
making my first game is like trying to stuff all my experience with old games into one
so its slow and painful when i dont get it right... i bet the next game is gonna be finished earlier!
When you don't find any time to work... :kaocry:
First Ten Minutes of my Caretaker inspired game

Forum statistics

Threads
110,401
Messages
1,053,010
Members
143,460
Latest member
Totaldramawwe
Top