mattbrok92

Warper
Member
Joined
Oct 26, 2015
Messages
4
Reaction score
2
First Language
English
Hello,

I am currently working on a skill that will roll a random number between 1-6 and apply a state based on the number rolled. Below is what I'm using and it seems to work most of the time, but occasionally there is a number rolled that won't apply a state. I'm not entirely sure how to check the random number output or what I need to adjust here. Any help would be appreciated!


<After Eval>

var roll = Math.floor(Math.random()*(6)+1)

if(roll==1){

target.addState(353);

}

if(roll==2){

target.addState(354);

}

if(roll==3){

target.addState(355);

}

if(roll==4){

target.addState(356);

}

if(roll==5){

target.addState(357);

}

if(roll==6){

target.addState(358);

}


</After Eval>

Thanks!
 

Shaz

Global Moderators
Global Mod
Joined
Mar 2, 2012
Messages
44,645
Reaction score
15,728
First Language
English
Primarily Uses
RMMV

I've moved this thread to Plugin Support. Thank you.



var roll = Math.floor(Math.random()*(6)+1)

It shouldn't make a difference, but try Math.floor(Math.random() * 6) + 1

If you want to see the output you might be able to add a console.log(roll) after that line, then press F8 while playing and go to the Console tab to see what the resulting number is.
 

Arthran

Veteran
Veteran
Joined
Jun 25, 2021
Messages
97
Reaction score
71
First Language
English
Primarily Uses
RMMZ
I'm not entirely sure how to check the random number output
console.log is generally a good way to go for this type of thing.

JavaScript:
<After Eval>

var roll = Math.floor(Math.random()*(6)+1);

console.log("The value of roll is: " + roll);

// The rest of your stuff

</After Eval>

While playtesting the game, press F8 to open the console, and then your log messages will show up in there as they are executed.
 

ATT_Turan

Forewarner of the Black Wind
Veteran
Joined
Jul 2, 2014
Messages
3,154
Reaction score
1,865
First Language
English
Primarily Uses
RMMV
A few pieces of advice for you:
- Any time you're posting any kind of code stuff, do it inside CODE tags (the </> icon above the text box). That will preserve your indenting and anything inside brackets.

- There's no purpose to the parentheses around the number 6 after your Math.random. It won't make anything malfunction, but it's also not doing anything.

- if/else conditions, loops and similar things automatically apply to the next single code statement. You only need to create a code block with braces if there's more than one statement within. It isn't making anything break, but you could be simplifying it to:
Code:
if (roll==1)
    target.addState(353);

- You should be using if/else if statements. By listing everything with if, the code for each statement is getting checked even if one of them already passed and applied a state. That won't make anything malfunction in this specific case, but it's unnecessary and generally a bad habit. You could also use a switch statement, which would allow you to get rid of the extra variable.

- For that matter, with the way your state IDs are numbered, there's no reason for you to have any if or else or switch statements. They're all sequential, so your entire notetag could be simplified to:
Code:
<After Eval>
target.addState(353+Math.floor(Math.random()*6));
</After Eval>

If you're still getting situations where states aren't getting added there, then you know there's something wrong where the target is resistant to the state or the state isn't filled out properly or something.
 

mattbrok92

Warper
Member
Joined
Oct 26, 2015
Messages
4
Reaction score
2
First Language
English
Thank you all for the assistance! With the console log, I was able to figure out there was an issue when the same number rolled twice in a row.
 

ATT_Turan

Forewarner of the Black Wind
Veteran
Joined
Jul 2, 2014
Messages
3,154
Reaction score
1,865
First Language
English
Primarily Uses
RMMV
Yes, by default the engine will do nothing with a state if the target already has it. You could use a plugin to change that (add the duration, increase the effect, something) or make your notetag code more complex to always ensure it results in a state that the target doesn't have.
 

Latest Threads

Latest Posts

Latest Profile Posts

After the initial success with one event, I set up four different events on the same map to detect who is in which slot in my current party, and fortunately it managed to work as intended, with the events changing image to match the party members even as I swapped their positions around in formation. This will be a great tool for immersive scene design.
Finally finished the first hand-drawn map! It took me a week, but I'm so proud!
Actual line of code I just had to write for a plugin:

return Object.keys(this._shopStock).map(key => key.split(",")).map(element => element.map(value => Number(value))).filter(key => key[0] === mapId && key[1] === eventId);

Forum statistics

Threads
118,447
Messages
1,116,046
Members
155,414
Latest member
AlvaroSimarro
Top