GSC Printing/Toggle Utilities

BullyWiiPlaza

Modder
Messages
214
Reaction score
174
Points
818
I created the following functions to make toggles and printing easier. I distinguish between global prints which should be announced to everyone in the center of the scren using your name so that they know who did what and local prints which are only announced to you in the killfeed. According to which kind of mod you have, you may set the
Code:
printGlobally
variable to true or leave it undefined.
Code:
printToggleState(modName, toggleVariable, printGlobally)
{
    if(!isDefined(printGlobally))
    {
        printGlobally = false;
    }

    message = modName + " ";

    if(toggleVariable)
    {
        message += booleanToString(true);

        if(printGlobally)
        {
            message = modName + " " + booleanToString(true);
            printGlobalPlayerAction(message);
            return;
        }

        self iPrintln(message);
        return;
    }

    message += booleanToString(false);

    if(printGlobally)
    {
        message = modName + " " + booleanToString(false);
        printGlobalPlayerAction(message);
        return;
    }

    self iPrintln(message);
}

printGlobalPlayerAction(action)
{
    iPrintlnBold(self.name + ": " + action);
}

booleanToString(booleanVariable)
{
    return booleanVariable ? "^2ON^7" : "^1OFF^7";
}
If I wanted to toggle god mode I would for example write the following:
Code:
toggleGodMode()
{
    self.godMode = invertAndPrint("God Mode", self.godMode);

    if(self.godMode)
    {
        self enableInvulnerability();
    }
    else
    {
        self disableInvulnerability();
    }
}
What happens here is when you run the function it will say
Code:
God Mode ^2ON
in the killfeed to you only and turn god mode on. When you run the function again, it will print
Code:
God Mode ^1OFF
and it will turn it off. As you can see, clean code. Note: Always using brackets after an if or else is good coding style but if you really wanted, you could omit them. I don't.

Our next example is a so-called global mod being toggled. For this I'll use the dvar for player movement speed:
Code:
toggleSuperSpeed()
{
    level.superSpeed = invertAndPrint("God Mode", level.superSpeed, true);
    setDvar("g_speed", self.godMode ? 400 : 90);
}
Notice how I'm using the level entity to attach the variable since it's a level variable and not a self variable. Also I'm using a third argument for
Code:
invertAndPrint()
because I want it to be printed globally to the center of the screen and not just in the killfeed of the invoker.

When you run the function the first time it will say your name followed by a colon and
Code:
Super Speed ^2ON
. It also turns the mod on. Then if you run the function again, you can imagine what happens. I'm using the
You do not have permission to view link Log in or register now.
to shorten the code even further.

Now compare, what do you prefer writing? The basic way like as follows
Code:
toggleGodMode()
{
    if(!isDefined(self.godMode))
    {
        self.godMode = true;
        self iPrintln("God Mode ^2ON");
        self enableInvulnerability();
    }
    else
    {
        self.godMode = undefined;
        self iPrintln("God Mode ^1OFF");
        self disableInvulnerability();
    }
}
or "my" way? I guess the answer is obvious.
 
Last edited:

The Dark Side

Former Staff Member
Messages
1,007
Reaction score
784
Points
993
Where is invert and print being called from? o_O Would you just change that to iPrintln? Probably a dumb question..
 
Top