Bits And Switches?

SephirothSephiroth Fayetteville, NC, USA
OK, I have a 'short int' that contains several bits of data about a player on my server, such as his login state, whether he is banned, or if the player is an immortal. Rather than using a load of 'if' checks, I want to try to use a switch statement, but don't know if it will work. Here's an example (not fromt he actual code, but same thing).
[code]
short int Flags;

Flags = FLAG_LOGIN;
Flags += FLAG_IMMORTAL;

switch(Flags)
{
case &FLAG_LOGIN:
//Does login bs
break;

case &FLAG_IMMORTAL:
SendToPlayer(socket, "You're a god!

");
break;
}
[/code]
Is it possible to do this, or should I use the 'if' checks?

-[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red][/b][/italic]

Comments

  • : OK, I have a 'short int' that contains several bits of data about a player on my server, such as his login state, whether he is banned, or if the player is an immortal. Rather than using a load of 'if' checks, I want to try to use a switch statement, but don't know if it will work. Here's an example (not fromt he actual code, but same thing).
    : [code]
    : short int Flags;
    :
    : Flags = FLAG_LOGIN;
    : Flags += FLAG_IMMORTAL;
    :
    : switch(Flags)
    : {
    : case &FLAG_LOGIN:
    : //Does login bs
    : break;
    :
    : case &FLAG_IMMORTAL:
    : SendToPlayer(socket, "You're a god!

    ");
    : break;
    : }
    : [/code]
    : Is it possible to do this, or should I use the 'if' checks?
    :
    : -[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red][/b][/italic]
    :
    [blue]That will not work. Use IFs:[/blue][code]
    #define BEGIN_MAP while (1) {
    #define END_MAP break; }
    #define ON_BIT(val,bit,func) if (((val) & (bit))==(bit)) { func (); break; }

    void OnStateDead ()
    {
    }

    void OnStateImmortal ()
    {
    }

    void OnStateActivated ()
    {
    }

    BEGIN_MAP
    ON_BIT (player, STATE_DEAD, OnStateDead)
    ON_BIT (player, STATE_IMMORTAL, OnStateImmortal)
    ON_BIT (player, STATE_ACTIVATED, OnStateActivated)
    END_MAP
    [/code]
  • SephirothSephiroth Fayetteville, NC, USA
    Changed it. The login flags became so complex for new character creation I made some seperate flag variables. One is used for logging in and character generation and the others deal with playing states and affects. Thanks anyways. Once I get the basic functions up I'll post the client and hold an open test.

    -[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red][/b][/italic]

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories