Dice rolling script in maya help - rookie programmer


#1

hi,

so i’m trying to get a little gui window that will allow me to select what kind of dice, how many of them and to roll those dice and report the results to a TEXT area in the gui.

currently, i have “999” showing up in the text area but i want the result of the roll to go in there. but don’t know how to do that. halp?

i also don’t understand how to query the state of the radio buttons so that i can use that in a function.

in terms of figuring out the functions for the number and kind of dice, pretty sure i can figure that out. but i just can’t figure out what i’m doing wrong with updating the text field and get the state of the radio buttons.

thanks!

====================

global int $textResults = 999;

global proc RollDice() {
$textResults = rand (1,7);
print $textResults;
}

global proc JIN_DiceRoller(){

$Results = 999;

if(`window -q -ex "window1_ui"`)deleteUI("window1_ui");
window -t "JIN_DiceRoller" -s false -wh 400 250 "window1_ui";
columnLayout -w 150 -adj true;
rowColumnLayout -p "window1_ui" -w 150 -nc 4;
radioCollection;
radioButton -l "D2";
radioButton -l "D6";
radioButton -l "D10";
radioButton -l "D20";
radioButton -l "D3";
radioButton -l "D8";
radioButton -l "D12";
radioButton -l "D100";
intSliderGrp -p "window1_ui" -w 50 -f true -l "# of dice to roll" -min 1 -s 1 -v 1;
columnLayout -p "window1_ui" -adj true;



button -bgc 1.0 0.616583287716 0.0 -ebg true -l "ROLL DICE" -c ("RollDice; print $Results; $myTextField = $Results;") -aop true;

$textResults = $Results;    

string $myTextField = `text -h 30 -l $textResults -fn "fixedWidthFont" -rs true`;
showWindow "window1_ui";

}

JIN_DiceRoller();


#2

Hey there,
Here’s how you can change text and query radioCollections with MEL.
I’ve commented on the side of any changes made to code, so think it could be easily followed:

global proc int RollDice() 
{
    int $textResults = rand (1,7);
    print ("Result of rolling dice is: " + $textResults + "\n");     //  prints result of dice rolling
    return $textResults;            // returns result of random call as int, so it could be used
}

global proc JIN_DiceRoller()
{
    $Results = 999;

    if(`window -q -ex "window1_ui"`)
        deleteUI("window1_ui");
    
    window -t "JIN_DiceRoller" -s false -wh 400 250 "window1_ui";
    
        columnLayout -w 150 -adj true;
            rowColumnLayout -p "window1_ui" -w 150 -nc 4;
                radioCollection dice_radioCollection;       // give internal name (any unique and valid name can be used)
                    radioButton -l "D2" D2_radioButton;     // give internal name so querying it will return meaningful name
                    radioButton -l "D6" D6_radioButton;     // give internal name so querying it will return meaningful name
                    radioButton -l "D10";
                    radioButton -l "D20";
                    radioButton -l "D3";
                    radioButton -l "D8";
                    radioButton -l "D12";
                    radioButton -l "D100";
                    
                intSliderGrp -p "window1_ui" -w 50 -f true -l "# of dice to roll" -min 1 -s 1 -v 1;
            columnLayout -p "window1_ui" -adj true;

        button -bgc 0.5 0.616583287716 0.0 -ebg true -l "QUERY RADIO BUTTON" -c "queryRadioCollection(\"dice_radioCollection\")" -aop true;     // added button that calls procedure which queries selected radioButton
        
        button -bgc 1.0 0.616583287716 0.0 -ebg true -l "ROLL DICE" -c "rollDice(\"dice_text\")" -aop true;     // button command will call rollDice procedure, and pass internal name of text as string argument

        text -h 30 -l $Results -fn "fixedWidthFont" -rs true dice_text;     // give text internal name, instead of storing it to a variable
        
    showWindow "window1_ui";
}

global proc rollDice(string $text)
{
    $textResults = RollDice();          // get random value by calling RollDice procedure (would advice sticking to camelCase, if coding with MEL)
    text -e -l $textResults $text;      // change text by entering edit mode (-e flag)    -   use internal name of text UI element (passed to procedure as $text string argument)
}

global proc string queryRadioCollection(string $radioCollection)
{
    
    string $selectedButton = `radioCollection -q -sl $radioCollection`;     // query selected radioButton by entering query mode for parent radioCollection (-q query)      -   use internal name for radioCollection to query (passed to procedure as $radioCollection string argument)
    
    // prints name of selected radioButton, or says if no button has been selected
    if ($selectedButton == "NONE")
        print "No radioButton currently selected.\n";
    else
        print ("Radio Button named: " + $selectedButton + " selected" + "\n");      // prints selected radioButton's internal name
        
    return $selectedButton;     // returns selected radioButton's internal name as string
}


JIN_DiceRoller()

One thing I would strongly suggest is to avoid using global variables, thus I’ve removed them from your code.

Regards,
Darko