2D online game development (network) (8) 1

xiaoxiao2021-04-05  254

2D

Online game development (network) (8)

Author:

akinggw

Foreword

Have already written

Raknet

The eighth article programmed, in the previous content, we explain

Raknet

How to transmit ordinary information, light is not enough. Because a game is not possible to transfer only this information, they may transmit the data structure.

And our content today is related to the data structure.

Create a packet

What kind of packet is established, or what kind of information you want to include in the packet is completely determined by what you want to send. And what kind of information should we send in the game? For example, we have to send a game player

Mine

In the game world coordinates, then we need the following data:

l

Player

Mine

Coordinates in the game world,

3

Floating point value:

X, Y, Z

. You can use a vector value.

l

Use some way to determine all players know players

Mine

The presence. About this problem, you can use it directly

Network id generator

Class is completed. We assume a class

Mine

From

NetWorkObject

Inherited, then all other players have to get and store

Mine

of

ObjectID.

l

Who is player?

Mine,

For this question, please refer to

Players, Playerid

. If you are playing games on the server, we can take you

Playerid

The value is set to some value, such as

255

. And if you can use it on the client

GetPlayerid

get it.

l

When a player

Mine

In a place

,

But maybe

10

After seconds, it reaches another place, so the most important thing is that we get the same time, the advantage is to avoid players.

Mine

Have a different time on different computers. Fortunately,

Raknet

can use

Timestamping

To handle this problem.

Use structure or bitstream

Finally, you send data to send the stream of the user's role. There are two ways to encode the data you want to send. One way is to build a structure and another is to build a bitstream class.

The advantage of establishing a structure is to change the structure very easily and see what data you actually sent. Thus, the sender and the recipient can share the same source files in the structure, so that unnecessary errors are avoided. The disadvantage of establishing a structure is that you will have to change and recompile many files frequently. An opportunity to compress when transmitting during bitstream.

The advantage of using the bitstream is that you don't have to change any other external files. Simply create a bitstream, write the data you want to send to the bitstream, and then send it. You can also read and write data by compression, then pass

Bool

The value is to determine if the data is compressed. The shortcomings of the bitstream are that you have now made a mistake, it is not easy to change.

Create a package with the structure

turn on

NetWorkstructure.h

There will be a big part in the middle of the file, like this:

// -----------------------------------------

//

Your structure is below

// ------------------------------------------

// ------------------------------------

//

Your structure is here

// ---------------------------------

// -----------------------------------------------

//

Your structure is above

// --------------------------------------

There are two universal forms for the structure, one is with

Timestamping

One is not. Without

Timestamping

form

#pragma pack (1)

Struct structname

{

Unsigned char typeid; //

Place your structure type here

//

Your data is placed here

}

;

band

Timestamping

form

#pragma pack (1)

Struct structname

{

Unsigned char useterstamp; //

Assign it

ID_TimeStamp

Unsigned long timestamp; //

Pass through

TimegetTime ()

Returned system time is here

Unsigned char typeid; //

Structure type is here

//

Your data is placed here

}

;

For the front of our role, we want to use here

Timestamping

Therefore, the results of structural filling are as follows:

#

Pragma Pack (1)

Struct structname

{

Unsigned char useterstamp; //

Assign this to

ID_TimeStamp

Unsigned long timestamp; //

Put by

GetTime ()

Get the system time here

Unsigned char typeid; //

This will allocate a type

id

Until

Packetenumerates.h

In this, here, we can say that

ID_SET_TIMED_MINE

Float X, Y, Z; //

Character

Mine

coordinate of

Objectid Objected; //

Character

Mine

The goal

Id

For reference on different computers

Mine

General method

Playerid PlayerId; //

Character

Mine

Players

Playerid

}

As we write above, we add

TypeID

The purpose is when we get a data stream to arrive, we will know what we have seen. So the last step is

Packetenumerates.h

Add

ID_SET_TIMED_MINE.

Note that you cannot contain a pointer directly or indirectly in the structure.

You will notice that I am called in the structure.

Objectid Objected

with

Playerid Playerid

. Why not use some more descriptive names, such as

MineID

with

MineOwerid?

I told you in my actual experience in practical use of the descriptive name is unmokent. Because although you know what parameters in these packets mean, they don't know. The advantage of using a general name is that you can handle your packets by paste, copying, without re-filling data. When you have a lot of packets, you will be in a big game, this method has saved a lot of differences.

Nest structure

There is no problem with the nest structure, it is worth noting that the first byte always determines the type of packet.

#pragma pack (1)

Struct a

{

Unsigned char typeid; // id_a

}

#pragma pack (1)

Struct B

{

Unsigned char typeid; // id_a

}

转载请注明原文地址:https://www.9cbs.com/read-132149.html

New Post(0)