Script question about random number+%

Johan

Villager
Member
Joined
Feb 23, 2017
Messages
14
Reaction score
2
First Language
Swedish
Primarily Uses
RMMV
Hi all! :)

I have some question about JavaScript code;

1. How to get a random number instead to use in-game variables?
Is there any script code to get a random number instead of using one of the in-game variables?

2. How to add % chance for both var line?
Like each number decrease chance to get, so item ID 410 would be 1% chance to get, meanwhile 400 would be 100%.

3. * Sorry... I got this questions when I did start posting here, and now I did forget my third questions :(


My code that I use for my test project;
Code:
var item = $gameVariables.value(1);

var nr = $gameVariables.value(2);

$gameParty.gainItem($dataItems[item], nr)
(Value 1 = random 400-410 ( = item ID beteween 400-410).
(Value 2 = random number 1-5 ( = how many item you can get ).

''''

My point for this script;

I trying to make a common event that will be available 1 times for every in-game month and will be there until 2-3 days, after that you can't access that until next month.
Inside that Common Event would be that script code.
You can then roll for a random item/weapon/armor from the database from x to y % + chance for extra loot!.
x = higher chance to get that loot
y = lower chance to get that loot
__________________________________________________________________________________________________

How to best setup that Common Events are my next problem :p
Right now I want to focus on how to get best possible Script code.
(If you have good idea/tips about the common event, please :) , not necessary because this topic focuses on my script setup.)
 

GuanyuMaker

Veteran
Veteran
Joined
Mar 9, 2015
Messages
63
Reaction score
74
First Language
Portuguese
Primarily Uses
RMMV
For a % chance, you can use random var 1-100. For a 5% chance for example the conditional would be something like if RandomVar<=5. Script wise I can only think about using Math.floor and Math.random. Not sure if math works on script calls made on editor . Anyway you can easly do it by the editor.

More details on math.random usage here:
https://www.w3schools.com/jsref/jsref_random.asp
 
Last edited:

Johan

Villager
Member
Joined
Feb 23, 2017
Messages
14
Reaction score
2
First Language
Swedish
Primarily Uses
RMMV
Thanks for replay =)

Did read more at that site and played around with in-game console, after some try I got this results;

Code:
var item = Math.floor(Math.random() * 10) + 40;  // Random number between 40-50
var nr = Math.floor(Math.random() * 5) + 1;      // Random number between 1-5
$gameParty.gainItem($dataItems[item], nr)
Now Nr1 question is done :)
Did just replaced variables to Math.floor script,
however! that script is just the same except Variables got replaced with the global variable.

But is that not possible to make % chance for a higher number?
Or the only way to go is to create conditions for each?
In that case, I can sort out maybe 4 different group and use if/else branches, but I prefer the other one :).

And now I did remember my third question;
How do I print out what item/gear/armor user got? via text.
 

GuanyuMaker

Veteran
Veteran
Joined
Mar 9, 2015
Messages
63
Reaction score
74
First Language
Portuguese
Primarily Uses
RMMV
Dont worry those vars are not global at all. They will be clean after the script call ends. I can think of a simpler way to work with percentage as its not a type, so I think you may need to is somethink like that:
var sucess = 5; // 5 is the sucess rate in percentage .Replace it with the % of sucess you may want to use.
var chanceroll = Math.floor(Math.random() * 100) + 1; // var that represents the chance you rolled.

if(chanceroll <= sucess){
// code to give the item you want.
}
For showing a message you may want to use:
$GameMessage.add("");
However i dont know how to print those vars, you may want to try and test somethings.
 
Last edited:

Johan

Villager
Member
Joined
Feb 23, 2017
Messages
14
Reaction score
2
First Language
Swedish
Primarily Uses
RMMV
Sorry for late post.

Can you explain more about that code you linked?
Trying to learn javascript, but I'm still a noob at it, but I feel I'm getting closer to what I want on my game =)

"// code to give the item you want." , do I have to replace that with my code that I was linked?

Found this script; (Will try to use script line 4 and 5 into my script)
Code:
var items = [1, 2, 3, 0];
var index = Math.randomInt(items.length);
if (items[index] > 0) {
  var reward = $dataItems[items[index]];
  $gameMessage.add("Obtained " + reward.name + "!");
  $gameParty.gainItem(reward, 1);
} else {
  $gameMessage.add("You get nothing, you lose, GOOD DAY SIR!");
}
Edit: After 20 different script route I did finaly got it!;
Code:
var items = [Math.floor(Math.random() * 10) + 40];
var nr = Math.floor(Math.random() * 5) + 1;
var index = Math.randomInt(items.length);
var reward = $dataItems[items[index]];
$gameMessage.add("Obtained " + reward.name + "!");
$gameParty.gainItem(reward, nr);
Now, I just need to add message about how many items you got :p
This script will only write items name, but nothing about how many you got.
Prob you need to add new "reward" and use that new code to "nr",
will see if I can figure it out. Meanwhile, if you know correct code for that, place post it here thanks :)
 
Last edited:

GuanyuMaker

Veteran
Veteran
Joined
Mar 9, 2015
Messages
63
Reaction score
74
First Language
Portuguese
Primarily Uses
RMMV
First I need to say I'm not the most recommended to help you there, I dont really know JS or RMMV libs. I do use the litte I know about C, C# and adapt it to JS while im rpg making. Said that it may exist better solutions than mine for what you want to do.

What I gave you is a sucess chance checker. It will randomize a number beteween 1 to 100 if that number is smaller than the success rate you setted in that case is 5% it will return true and trigger the code inside of it. Basically thats what happends with percentage chances. If your give item code is right it will work if you replace it. However it wont be exactly 5% as your give item code still have failure chance that is when the index rolls 0, so it will be a bit lower than 5% in that case.

For the game message i cant really help you. While I know it is the code to show message i never messed with it.

Said all that as far as what you showed to the moment I can see that it can be done at the editor with not much more effort but easier if you're unsure about using JS codes. Isnt that a option?
 
Last edited:

Johan

Villager
Member
Joined
Feb 23, 2017
Messages
14
Reaction score
2
First Language
Swedish
Primarily Uses
RMMV
My goal on my rpgmv game project is to learn more about js and take advance from other and learn,
at the same time build up a nice game. I know that there is a lot you can't do in RPG maker mv software without script code.
And also you can make more simple way with the script than multiple events.
Also, I do use the editor, sure you can use those global variables end to replace it with the in-game variable. But that is what I want to skip to save up more variables slot for others. I'm not sure how to add a game message in the editor to show what item you got and how many, looks cleaner with the script and easy to change later on. Maybe I will use if condition instead of script like what you did say from your first post here, then I don't have to mess with the script code :)
About your code, did play around with it to support more than 2, however, the script code has only a few rows so it looks a little messy :p;

var sucess = 1; var chanceroll = Math.floor(Math.random() * 100) + 1;
if(chanceroll <= sucess){
var items = [Math.floor(Math.random() * 7) + 92]; var index = Math.randomInt(items.length); var reward = $dataItems[items[index]];
$gameMessage.add("Obtained " + reward.name + "!"); $gameParty.gainItem(reward, 1);
} else if(chanceroll <= 40){
var sucess = 40; var chanceroll = Math.floor(Math.random() * 100) + 1; if(chanceroll <= sucess){
var items = [Math.floor(Math.random() * 7) + 65]; var index = Math.randomInt(items.length); var reward = $dataItems[items[index]];
$gameMessage.add("Obtained " + reward.name + "!"); $gameParty.gainItem(reward, 1); }
} else if(chanceroll <= 90){
var sucess = 90; var chanceroll = Math.floor(Math.random() * 100) + 1; if(chanceroll <= sucess){
var items = [Math.floor(Math.random() * 7) + 47]; var index = Math.randomInt(items.length); var reward = $dataItems[items[index]];
$gameMessage.add("Obtained " + reward.name + "!"); $gameParty.gainItem(reward, 1); }}
It have 3 different % chance. And it works fine =)
After 20 clicks total I got 8 no loot (maybe that is around 90%). If I change from 90 to 100 I got allways loot from that loot table if I don't got any loot from the other loot table. So yeah its works perfect :)

But thanks for tip =)
Got what I wanted (random loot based on %).
And yes, I don't plan to use everything on the script. Just this one and maybe other if that is not possible with the normal editor.
 

GuanyuMaker

Veteran
Veteran
Joined
Mar 9, 2015
Messages
63
Reaction score
74
First Language
Portuguese
Primarily Uses
RMMV
Just tried to help =). Personally, I find easier to use editor for messing with messages. There's tags that can be used for most things if you're using Yanfly's message core.

Yeah, scripts calls only have few lines =/ . For bigger codes you may need to write your own plugin or break it in multiple calls using the help of editor and tricks to pass over vars(kind of a mess). I usually read other people plugins or the base code that you can find in your game folder to learn things that i need. There's some cool tutorials on Youtube too.

Anyway, I hope I've helped. Good luck !
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Profile Posts

Day 9 of giveaways! 8 prizes today :D
He mad, but he cute :kaopride:

Our latest feature is an interview with... me?!

People4_2 (Capelet off and on) added!

Just beat the last of us 2 last night and starting jedi: fallen order right now, both use unreal engine & when I say i knew 80% of jedi's buttons right away because they were the same buttons as TLOU2 its ridiculous, even the same narrow hallway crawl and barely-made-it jump they do. Unreal Engine is just big budget RPG Maker the way they make games nearly identical at its core lol.

Forum statistics

Threads
106,040
Messages
1,018,476
Members
137,824
Latest member
dobratemporal
Top