0
\$\begingroup\$

I'm working on a multiplayer game in which the server is a custom C# server. What I want to do is to store the position of each player in my database.

In my case I'm sending Vector3 as an object through NetworkStream, the problem is how to access to that object in the server side. As you know there is no Vector3 class out there.

I don't want to get the object sent by the client as a string, manipulate it to get the coordinate. I want directly to access the coordinate of the Vector3 in the server side.

\$\endgroup\$
  • \$\begingroup\$ Do you want to update an SQL database in real-time whenever a player moves? Are you sure this is a good idea? Usually it is sufficient to save player positions on logout and every few minutes. \$\endgroup\$ – Philipp Oct 6 '16 at 11:01
1
\$\begingroup\$

You cannot just send a Vector3 over the network, the data has to be serialized first. Either you are going to serialize it manually, or you use some sort of helper libraries that obfuscate all the serialization process from you.

Personally I prefer to construct my own serializer/deserializer when dealing with network packets.

A vector3 can be easily serialized to a short string, and then deserialized into a container object server side in order to consume the data.

string data = v3.x + "," + v3.y + "," + v3.z;

On server:

//custom vector3
struct Vector3 {
    float X;
    float Y;
    float Z;
    public Vector3(float x, float y, float z) {
        X = x;
        Y = y;
        Z = z;
    }
}    

//Data comes in
string[] data = networkData.Split(',');
Vector3 v3 = new Vector3(Single.Parse(data[0]), Single.Parse(data[1]), Single.Parse(data[2]));

//Use data
print(v3.x);
\$\endgroup\$
  • 1
    \$\begingroup\$ Instead of rolling your own, I would recommend class JsonUtility to serialize objects to JSON and then any other JSON library on the server-side to parse it and deserialize it to an object. \$\endgroup\$ – Philipp Oct 6 '16 at 11:05
  • \$\begingroup\$ It may also be wise to do binary serialization, which should result in less data to send via the network. This does not only go easy on the user's ressources, but even more on your servers, which may need to handle hundreds of players simultaneously. I'm sure C# provides utilities to do binary serialization. \$\endgroup\$ – LukeG Feb 22 '17 at 0:47
0
\$\begingroup\$

If you insist on sending the Vector3 object, you will need to create a Vector3 class on your "custom C# server" to parse the object. If you don't want to do that, you should be sending data out of the Vector3 to your server. You can't just take an object and parse it's contents without your server knowing anything about the object.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.