Some good example for using enums

后端 未结 10 1528
感情败类
感情败类 2021-01-05 06:28

I learned enums when I learned C and from time to time I keep myself reminding about it and most of the time by re-reading from some source,it occurred to me that this is d

10条回答
  •  臣服心动
    2021-01-05 07:19

    I started a personal project in, and I wanted to identify my packet ID, it looks like this:

    enum{
    //Client to server
        //Connection
        ID_KEEP_ALIVE       = 0x00,
        ID_LOGIN_REQUEST    = 0x01,
        ID_CONNECTING       = 0x02,
        ID_DISCONNECT       = 0x03,
    
        //Player actions
        ID_PLAYER_INFO      = 0x04,
        ID_PLAYER_MOVE      = 0x05,
        ID_PLAYER_ATTACK    = 0x06,
    
        //Inventory
        ID_LOOT_ITEM        = 0x10,
        ID_DESTROY_ITEM     = 0x12,
        ID_USE_ITEM         = 0x13,
        ID_EQUIP_ITEM       = 0x15,
        ID_UNEQUIP_ITEM     = 0x16,
        ID_DROP_ITEM        = 0x17,
    };
    

    and then, when I receive a packet, I've got a huge switch that looks like this to process the packets and send them:

    switch(packet.packetID){
        case ID_KEEP_ALIVE:
            //...
            break;
        case ID_LOGIN_REQUEST:
            //...
            break;
        case ID_CONNECTING:
            //...
            break;
        case ID_DISCONNECT:
            //...
            break;
        //..
    }
    

    it's my best example, enjoy :)

提交回复
热议问题