简体   繁体   English

C#Lidgren库-发送玩家数据

[英]C# Lidgren Library - Send playerdata

I am currently writing an object oriented game with a multiplayer. 我目前正在与多人游戏一起编写面向对象的游戏。 Player inherits from entity and when I send my current players properties via 播放器继承自实体,当我通过发送当前播放器属性时

NetOutgoingMessage outmsg = Server.CreateMessage(); [...] outmsg.WriteAllProperties(p);

where p is my player object, my clients only receive properties which are declared in my players class and not from the entity class. 其中p是我的播放器对象,我的客户仅收到在我的播放器类中声明的属性,而不是从实体类声明的属性。 Therefore my clients can't update the player positions etc. 因此,我的客户无法更新玩家位置等。

Any advice? 有什么建议吗?

edit: 编辑:

It recognizes the inherited objects, but it doesn't send them properly. 它可以识别继承的对象,但不能正确发送它们。 I think I might need to use some Bindingflags as there is an option to it and it should be possible to send objects which are allocated to a player too. 我想我可能需要使用一些Bindingflags,因为它有一个选项,应该也可以发送分配给玩家的对象。 I am using a Vector2 class to specify the position and velocity of my player, but my client either can't receive a Vector2 class or the server can't send it. 我正在使用Vector2类来指定播放器的位置和速度,但是我的客户端无法接收到Vector2类,或者服务器无法发送它。 If I use integers, strings, booleans or anything it works, but not with custom classes. 如果我使用整数,字符串,布尔值或任何可行的方法,但不使用自定义类。

This should (have not tried this myself) be possible since you can serialize anything into bytes but not straight out the box. 这应该(自己没有尝试过)是可能的,因为您可以将任何内容序列化为字节,但不能直接使用。 There are 3 primary conditions: 有3个主要条件:

  • objects that need to be sent should be exactly the same on client and server. 客户端和服务器上需要发送的对象应该完全相同。
  • You need to send the type of data along with the data at a expected position. 您需要将数据类型与数据一起发送到预期位置。
  • And most probably need to sent the amount of bytes send since NetIncomingMessage does not have read till end. 由于NetIncomingMessage直到读完才读,因此最有可能需要发送发送的字节数。

One way to send data type info is to send a enum as the first byte 发送数据类型信息的一种方法是将枚举作为第一个字节发送

enum PacketTypes
{
    LOGIN,
    MOVEMENT,
    SHOOTING,
}

out.Write((byte)PacketTypes.Login);

NetOutgoingMessage.Write can take a bytes or byte array. NetOutgoingMessage.Write可以采用字节或字节数组。 So you can use any method to serialize your object to bytes. 因此,您可以使用任何方法将对象序列化为字节。 Before you write it as a out message first determine the amount of bytes and write that after the packet info. 在将其写为out消息之前,首先确定字节数,然后在数据包信息之后写出。 Then write the data. 然后写入数据。

On the receiving end you need to determine what kind of data you received and handle that properly. 在接收端,您需要确定接收到的数据类型并正确处理。 if (inc.ReadByte() == (byte)PacketTypes.Login) , now we know how to handle this custom package. if (inc.ReadByte() == (byte)PacketTypes.Login) ,现在我们知道如何处理此自定义包了。 Now read out the integer that specifies how much bytes the data is, read out that amount and deserialize it. 现在读出指定数据多少字节的整数,读出该数量并反序列化。

There are plenty of sources that explain serializing and deserialzing data so I won't go into depth here. 有很多资料可以解释数据的序列化和反序列化,因此在这里我不再赘述。 And since you can send and receive bytes with Lidgren this really should just work. 而且由于您可以使用Lidgren发送和接收字节, Lidgren这确实应该可以工作。 However, you should always send as little data as possible and in practice a couple of primitives are often enough. 但是,您应该始终发送尽可能少的数据,实际上,几个原语通常就足够了。 For example, the name of the player does not need to be send each time you want a position. 例如,您不需要每次想要位置时都发送球员的姓名。 The abilities of that player are probably even less interesting. 该球员的能力可能甚至没有那么有趣。 You need to break it down in pieces, if a player moves send 2 floats. 如果玩家移动,则需要将其分解成2个浮点数。 If a player gets hit send a int. 如果玩家被击中,则发送一个整数。 For a ability being used you often only need an integer as ID. 对于使用的能力,您通常只需要一个整数作为ID。

I just (finally) read, that Lidgren is not able to send or read custom objects. 我只是(最后)读到,Lidgren无法发送或读取自定义对象。

https://groups.google.com/forum/#!topic/lidgren-network-gen3/sdj6LFMdKAY https://groups.google.com/forum/#!topic/lidgren-network-gen3/sdj6LFMdKAY

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM