Reward script

Ask and answer questions about making games and related topics. Unrelated topics go in that other forum.

Moderators: marionline, SDHawk

Post Reply
User avatar
yoelleoy
Red Slime
Posts: 28
Joined: Sun Jan 25, 2015 2:30 pm

Reward script

Post by yoelleoy »

Hi, I am trying to make a script where it will give the player a random reward but I don't know what to use or how to make it random or a %chance of getting a certain reward.
Sorry if this seems like a dumb question but I am still trying to learn the whole coding language.
Last edited by yoelleoy on Sun May 03, 2015 8:26 pm, edited 1 time in total.
TMC
Metal King Slime
Posts: 4308
Joined: Sun Apr 10, 2011 9:19 am

Post by TMC »

Use the 'random' command. For example, for a 7% chance of something happening, write:

Code: Select all

if &#40;random&#40;1,100&#41; <= 7&#41; then &#40;...&#41;
If you want to choose exactly one of a series of things each with a certain chance, use a chain of ifs:

Code: Select all

variable &#40;rand&#41;
rand &#58;= random&#40;1,100&#41;
if &#40;rand <= 5&#41; then &#40; # 5% chance
  ...
&#41; else if &#40;rand > 5 && rand <= 15&#41; then &#40; # 10% chacne
 ...
&#41; else if &#40;rand > 15 && rand <= 35&#41; then &#40;  # 20% chance
  ...
&#41; else &#40;  # 65%
  ...
&#41;
If you just want to select between a set of things, each with equal chance, then clearly things can be a bit simpler.

You can also use 'switch':

Code: Select all

variable &#40;rand&#41;
rand &#58;= random&#40;1,4&#41;
switch &#40;rand&#41; do &#40;
  case&#40;1&#41;
    ...
  case&#40;2&#41;
    ...
  case&#40;3&#41;
    ...
  case&#40;4&#41;
    ...
&#41;
Last edited by TMC on Mon May 04, 2015 5:50 am, edited 1 time in total.
User avatar
yoelleoy
Red Slime
Posts: 28
Joined: Sun Jan 25, 2015 2:30 pm

Post by yoelleoy »

TMC wrote:Use the 'random' command. For example, for a 7% chance of something happening, write:

Code: Select all

if &#40;random&#40;1,100&#41; <= 7&#41; then &#40;...&#41;
If you want to choose exactly one of a series of things each with a certain chance, use a chain of ifs:

Code: Select all

variable &#40;rand&#41;
rand &#58;= random&#40;1,100&#41;
if &#40;rand <5> 5 && rand <15> 15 && rand <= 35&#41; then &#40;  # 20% chance
  ...
&#41; else &#40;  # 65%
  ...
&#41;
If you just want to select between a set of things, each with equal chance, then clearly things can be a bit simpler.

You can also use 'switch':

Code: Select all

variable &#40;rand&#41;
rand &#58;= random&#40;1,4&#41;
switch &#40;rand&#41; do &#40;
  case&#40;1&#41;
    ...
  case&#40;2&#41;
    ...
  case&#40;3&#41;
    ...
  case&#40;4&#41;
    ...
&#41;
Thanks a lot for the help :)
Post Reply