How to return to Gamemode 0, after it goes through all the modes?
#1

Hello, I'm currently developing a Mini-game type server. There's currently 6 modes. When the server starts up, it goes through mode 0-5 and then it keeps repeating 5 over and over, after every cycle. After gamemode 5, I want it to restart and go back to mode 0.

I hope you understand what I'm trying to do, here's some of my code.
pawn Код:
forward ChangeMode();
public ChangeMode()
{
        if(CurrentGamemode  == 0)
    {
        ServerMinutes = 0;
        ServerSeconds = 10;
    }
        if(CurrentGamemode  == 1)
    {
        ServerMinutes = 0;
        ServerSeconds = 10;
    }
        if(CurrentGamemode  == 2)
    {
        ServerMinutes = 0;
        ServerSeconds = 10;
    }
        if(CurrentGamemode  == 3)
    {
        ServerMinutes = 0;
        ServerSeconds = 10;
    }
        if(CurrentGamemode  == 4)
    {
        ServerMinutes = 0;
        ServerSeconds = 10;
    }
        if(CurrentGamemode == 5)
    {
        ServerMinutes = 0;
        ServerSeconds = 10;
    }
    else
    {
        CurrentGamemode++;
    }
Thanks for your help.
Reply
#2

pawn Код:
if(CurrentGamemode == 5)
    {
        ServerMinutes = 0;
        ServerSeconds = 10;
        CurrentGamemode = 0;
    }
Reply
#3

One minor thing you could do to optimize your code is use a switch statement.

Example with your code.
pawn Код:
switch(CurrentGamemode)
{
    // If Current Gamemode is equal to the value after "case" then it runs that code
    // So on and so forth.
    case 0:
    {
        ServerMinutes = 0;
        ServerSeconds = 10;
        CurrentGamemode++; // adds one for the next time you call ChangeMode or w/e
    }
    case 1:
    {
        ServerMinutes = 0;
        ServerSeconds = 10;
        CurrentGamemode++;
    }
    case 2:
    {
        ServerMinutes = 0;
        ServerSeconds = 10;
        CurrentGamemode++;
    }
    case 3:
    {
        ServerMinutes = 0;
        ServerSeconds = 10;
        CurrentGamemode++;
    }
    case 4:
    {
        ServerMinutes = 0;
        ServerSeconds = 10;
        CurrentGamemode++;
    }
    case 5:
    {
        ServerMinutes = 0;
        ServerSeconds = 10;
        // if we've reached the final mode
        // we reset it to zero.
        CurrentGamemode = 0;
    }
}
Much more powerful than the if structure, and easier to read. I recommend you to look at that link. It tells you every thing about switch statements.
Reply
#4

Thanks a ton. I'll look into switches. Appreciate the help.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)