Jump to content

BPackets


Bimbol
 Share

Recommended Posts

  Introduction

BPackets - packet serialization module for Gothic 2 Online.

Project links:
- Repository

Usage example

// SHARED-SIDE
enum BonusType {
    DAMAGE,
    HEALTH
}

class BonusItemMessage extends BPacketMessage {
    </ type = BPacketInt32 />
    type = -1

    </ type = BPacketInt32 />
    value = -1
}

class TradeItemMessage extends BPacketMessage {
    </ type = BPacketInt32 />
    id = -1

    </ type = BPacketInt32, optional = true />
    amount = null

    </ type = BPacketArray(BonusItemMessage), optional = true />
    bonuses = null
}

print(TradeItemMessage.format())
// < Packet: 1 >
// + id: int32 = -1
// + amount: int32 = null
// + bonuses: array = null

// SERVER-SIDE
if (SERVER_SIDE) {
    addEventHandler("onPlayerJoin", function (pid) {
        local items = [
            TradeItemMessage(5, 32),
            TradeItemMessage(23, null, [
                BonusItemMessage(BonusType.DAMAGE, 10),
                BonusItemMessage(BonusType.HEALTH, 50)
            ]),
        ]

        foreach (message in items) {
            message.serialize().send(pid, RELIABLE)
        }
    })
}

// CLIENT-SIDE
if (CLIENT_SIDE) {
    TradeItemMessage.bind(function (message) {
        print("RECEIVED ITEM:")
        print("id: " + message.id)
        if (message.amount != null) {
            print("amount: " + message.amount)
        }

        if (message.bonuses != null) {
            print("Bonuses:")
            foreach (bonus in message.bonuses) {
                print("+ " + bonus.type + " = " + bonus.value)
            }
        }
    })

    // RECEIVED ITEM:
    // id: 5
    // amount: 32
    // RECEIVED ITEM:
    // id: 23
    // Bonuses:
    // + 0 = 10
    // + 1 = 50
}

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...