• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GML Dice Roll Emulation Program

P

popstar426

Guest
Hi,
I am trying to make a program where you input a set of die (say 2d6) and have it output every possible combination of those dice (1-1,1-2,1-3,1-4,1-5,1-6,2-1 and so on). I would also like it to do things such as totaling the sum of both the dice and give it the ability to tally certain values (such as numbers divisible by 2). It doesn't matter how I input the dice or how the results are outputted. How would I go about achieving this.
 

Simon Gust

Member
If you throw 2x d6, you can make a irandom_range(1,6) for each d6.
I think making a script function is good for that

Code:
/// scr_dice_roll( dieCount, dieType )
var die_count = argument0;
var die_type = argument1;
var die_info; // array to be output

die_info[die_count, 4] = 0; // predefine array for speed bonus
die_info[0, 1] = 1; // start the product tab with 1 so it doesn’t give out 0 always.

switch (die_type)
{
case d6:
{
for (var i = 1; i <= die_count; i++)
{
die_info[i, 0] = irandom_range(6);
die_info[i, 1] = die_info[i, 0] & 1;

die_info[0, 0] += die_info[i, 0];
die_info[0, 1] *= die_info[i, 0];
}
}
break;
case d20:
{
// do some d20 action here
}
break;
}

return (dice_info);

/*
dice info table
dice[0,  0] = sum of dice
dice[0,  1] = product of dice
dice[1+, 0] = the result of the current die roll
dice[1+, 1] = odd number = true, even number = false;
*/
The script returns the array with the info in it
Code:
var die_info = scr_dice_roll( 2, d6 );
var die_sum = die_info[0, 0];
var die_pro = die_info[0, 1];

var die1_roll = die_info[1, 0];
var die1_odd =  die_info[1, 1];
...
 

NicoFIDI

Member
Code:
/// rollDice(rollCode)
// rollCode format: [ammount]d[dice faces]+[added]
var rollCode = argument[0];

var values;
values[0] = 0; // ammount
values[1] = 0; // dice faces
values[2] = 0; // added

var actualValue = 0;

for (var i = 1; i < (string_length(rollCode)+1); i++) {
    var currStr = string_copy(rollCode, i, 1);
 
    switch(currStr) {
        case "d": actualValue = 1; break;
        case "+": actualValue = 2; break;
    
        default:
            values[actualValue] *= 10;
            values[actualValue] += real(currStr);
            break;
    }
}

var result = values[2];
for (var i = 0; i < values[0]; i++) {
    result += irandom(values[1]-1)+1;
}

return result;
DISCLAMERS:
-it doesnt check that it have the real format but you can validate inside the default
-you can operate only with integers
 
Top