Game Development Community

dev|Pro Game Development Curriculum

Dynamic GUI

by Erik Madison · 08/13/2008 (6:16 am) · 3 comments

A project I'm working on re-uses a basic gui in a number of places, with just a few things being changed here and there, on demand. This script simplifies the job for me, perhaps you may find some use for it as well.
// Set a game gui button (or other named control) to enable/visible and vv
function clientCmdSetGameGUIButton(%control, %envis, %val)
{   
   if (!isObject(%control))
      return;
   //echo("Command to set" SPC %control SPC %envis SPC %val);
   if (%envis $= "setActive") 
      %command = %control @ ".setActive(" @ %val @ ");";
   else if (%envis $= "setVisible")
      %command = %control @ ".setVisible(" @ %val @ ");";
   else if (%envis $= "setFrame")  
      %command = %control @ ".setFrame(" @ %val @ ");";
   else if (%envis $= "text" || %envis $= "setText")              
      %command = %control @ ".setText(" @ %val @ ");";
   else
      error("Unknown state requested of clientCmdSetGameGUIButton()");
   eval(%command);
}
Call it like this from the server....
commandToClient(%client, 'SetGameGUIButton', "deal2Button", "setVisible", true);
   commandToClient(%client, 'SetGameGUIButton', "Bet1Button", "setActive", false);
Or slow it down a bit with a schedule...
schedule(4050, 0, commandToClient, %client, 'SetGameGUIButton', "GenericTextA", "setText", "Customized Gui");

#1
08/13/2008 (6:47 am)
May be useful... thanks
#2
08/13/2008 (7:11 am)
Nice. I can see this being useful.
Cheers.
#3
08/29/2008 (5:48 pm)
Instead of using a clunky if/else if/else construct, a simple switch$() will make things much more legible.

Note that it's "switch$" not just "switch". "switch$" is for string comparisons and "switch" is for numeric.

function clientCmdSetGameGUIButton(%control, %envis, %val)
{   
    if (!isObject(%control))
        return;
    //echo("Command to set" SPC %control SPC %envis SPC %val);
    switch$ (%envis)
    {
        case "setActive":  %control.setActive( %val );
        case "setVisible": %control.setVisible( %val );
        case "setFrame": %control.setFrame( %val ); return;
        case "setText": %control.setText( %val ); return;
        case "text": %control.setText( %val ); return;
        default: error("Unknown state requested of clientCmdSetGameGUIButton()");
    }
}

Just thought I'd add a $0.02 tip.