jlusco

Veteran
Veteran
Joined
May 29, 2016
Messages
34
Reaction score
63
First Language
Eng
Primarily Uses
So I have set an array
var items =[1,2,3,4,5];
How do I then set a variable to be a random value from the array?
Sorry I suck as javascript
 

KenSoulslayer

Graphic Designer/JS Dev
Member
Joined
Nov 29, 2020
Messages
26
Reaction score
10
First Language
Hindi
Primarily Uses
RMMZ
Code:
const randomEl = items[Math.floor(Math.random() * items.length)];

If you need me to explain this snippet, ask and I will let you know.

Also using let is better in JS if you are not going to use this variable in another code snipper as var has global scope and mess up other variables, if you are not sure which one to use and you dont have a lot of variables var is fine.
If the value wont change during runtime just use const
 

jlusco

Veteran
Veteran
Joined
May 29, 2016
Messages
34
Reaction score
63
First Language
Eng
Primarily Uses
Code:
const randomEl = items[Math.floor(Math.random() * items.length)];

If you need me to explain this snippet, ask and I will let you know.

Also using let is better in JS if you are not going to use this variable in another code snipper as var has global scope and mess up other variables, if you are not sure which one to use and you dont have a lot of variables var is fine.
If the value wont change during runtime just use const
So if I understand you I would do
let items =[1,2,3,4,5];
const randomEl = items[Math.floor(Math.random() * items.length)];
$gameParty.gainItem($dataItems[randomEl],1);
to reward the random item from the list. Am I understanding this correctly?
 

jlusco

Veteran
Veteran
Joined
May 29, 2016
Messages
34
Reaction score
63
First Language
Eng
Primarily Uses
Code:
const randomEl = items[Math.floor(Math.random() * items.length)];

If you need me to explain this snippet, ask and I will let you know.

Also using let is better in JS if you are not going to use this variable in another code snipper as var has global scope and mess up other variables, if you are not sure which one to use and you dont have a lot of variables var is fine.
If the value wont change during runtime just use const
You are a life saver this worked perfectly for the random reward I was working on. Thank you.
 

KenSoulslayer

Graphic Designer/JS Dev
Member
Joined
Nov 29, 2020
Messages
26
Reaction score
10
First Language
Hindi
Primarily Uses
RMMZ
So if I understand you I would do
let items =[1,2,3,4,5];
const randomEl = items[Math.floor(Math.random() * items.length)];
$gameParty.gainItem($dataItems[randomEl],1);
to reward the random item from the list. Am I understanding this correctly?

Yes that's right, for brevity's sake you can also use it in a single line and avoid declaring the random variable as below!

Code:
let items =[1,2,3,4,5];
$gameParty.gainItem($dataItems[items[Math.floor(Math.random() * items.length)]],1);

and such won't really affect anything though

You are a life saver this worked perfectly for the random reward I was working on. Thank you.

You're welcome, glad I could help! :kaopride:
 

Latest Threads

Latest Posts

Latest Profile Posts

Interesting week. Lots of stress and anxiety especially on Tuesday but still got thru it. How is everyone doing?
I was pleased with how the little ... whatever those square bits like upside-down turrets on the skirt are? I like how they turned out. Here's a little ranger/bandit/archery enthusiast to model it:

TestChara-08.gif


I'm calling her Robin because obviously.
Just downloaded the demo and boy howdy are my arms tired (???) :kaohi:
I just watched a trailer for the upcoming 2021 JRPG Fantasian. They demonstrated an interesting mechanic where random encounters can be sent to a stockpile instead of fighting them right away, which can be emptied in a single battle string at the player's choice of timing.
"The pleasure from the senses seems like nectar at first, but it is sour as the toxin in the end." -Kirshna

Forum statistics

Threads
109,157
Messages
1,042,603
Members
141,656
Latest member
drackdragon113
Top