So, in order for me to transmit the position of a object to a from a server, I need to get its transform. The easiest way I found to do this is with Vector3.this [int index] and creating a vector3. Now I just need to know how to link a gameobject to the Vector3 I assigned in the code:
public Vector3 p;
public GameObject tester;
void Start () {
deserializer = new Deserializer();
ClientConfig clientConfig = new ClientConfig();
clientConfig.appVersion = "0.2";
client = new Client(clientConfig);
client.Connected += OnConnected;
client.LoginSuccessful += OnLogged;
client.MessageReceived += OnMessageReceived;
inputText.text = "";
outputText.text = "";
WriteOutputText("Connecting...");
client.Connect("127.0.0.1", 10001);
p[0] = someint;
//This is where I need to link the gameobject.
}
Above is where I need to assign the gameobject to the vector3, and this is where I send it:
void SendXTransform(float xTransform)
{
Serializer serializer = new Serializer(new MemoryStream());
serializer.WriteFloat(xTransform);
client.SendMessage(TRASFORM_UPDATEX, serializer.currentPosition, serializer.GetBytes());
}
void Update () {
SendXTransform(p[0]);
}
Any help would be appreciated. Thanks!
Best way I can think of is to have some kind of master interpreter class that accepts server commands and interprets them a la
Syncher
static HashTable synchableObjects;
static void IncomingMessage( int objectIdentifier, int command, Vector3 value ) {
synchableObjects[ objectIdentifier ].ApplyCommand( command, value );
}
static int AddSynchable( Synchable object ) {
synchableObjects.Add( nextID, object );
nextID ++;
}
Then synchables would communicate thusishly:
Synchable.js
private int myID;
enum Command {
SetTransform = 1, Delete = 2, etc
}
void ApplyCommand( int command, Vector3 value ) {
// do whatever
}
void Start() {
myID = Syncher.AddSynchable( this );
}
My C# is pretty rusty but you should be able to make value a “any type” variable (or overload ApplyCommand to death, or make smart choices in what all information can be sent). Then any object that needs to synch with the server will be synched (or rather, told how to synch) by the Syncher class.
So, basically, my favorite method is to create a new class that automagically takes care of its own synchage. Anything that the server can request a synch on gets a Synchable component added to it, and Syncher listens for server instructions to synch a particular object by its unique identifier.
Might have to have the server assign identifiers at startup, as the order of Start() functions running is not guaranteed.
Ok, thanks! Just one more thing: The server program I’m using (Beamserver 2) Has a built in system where you can assign a tag to commands that are sent out, and then you can set their payload as a string, a float, a object, ect. So do you think it would be more efficient to have a tag for each command such as TRANSFOM_UPDATE, ANIMATION_UPDATE, DELETE_OBJECT, and have a different server side command for each action, or just have one tag such as OBJECT_SYNC and encode everything in that one message? Thanks!
You want to send as little information as possible to keep speed up, so I’d say separate it out into the several commands. Otherwise Object_Sync would have to send transform information, animation information, and whether or not to delete - instead of just one of those - each time it sends.