- Home /
What is the easiest way to set up a LAN-game?
Hey guys,
I have been trying to figure this out, since I am trying to create a very simple LAN-game, for testing purposes and to eventually create an AI from playing the game myself.
Here's my problem: What I am building is an RTS game, which means that synchronization is critical.
In my code-example (see under) I wrote some code which handles perfect synching, the example is just a cube changing color, but ofc this could be anything.
//This function is called from a button-press.
public void OnCubeColorChange(string color) {
networkView.RPC("ServerSetCubeColor", RPCMode.All, color);
}
//We use this function, since we can't send a Color-variable in an RPC
Color HexToColor(string hex)
{
byte r = byte.Parse(hex.Substring(0,2), System.Globalization.NumberStyles.HexNumber);
byte g = byte.Parse(hex.Substring(2,2), System.Globalization.NumberStyles.HexNumber);
byte b = byte.Parse(hex.Substring(4,2), System.Globalization.NumberStyles.HexNumber);
return new Color32(r,g,b, 255);
}
//All users receive this message, but only the host will execute any code
[RPC]
void ServerSetCubeColor(string cubeColor) {
if(Network.isServer) {
networkView.RPC("SetCubeColor", RPCMode.All, cubeColor);
}
}
//All users receive this message and it is certain that everyones cube is the same color.
[RPC]
void SetCubeColor(string cubeColor) {
GetComponent<MeshRenderer>().material.color = HexToColor(cubeColor);
}
So when someone presses the button, it will send out a message to all users (RPCMode.Server did not call anyone, so I couldn't use that) and only the host (checked by Network.isServer) will send out a message to everyone again, which actually changes the color of the cube.
Perfectly synced, but the host will always have that 0.05 second advantage over the other users, making this a little bit unfair. A second thing is that sending a message out to everyone when actually only the host should get it, seems a little inefficient.
So my final question is: Does this code make sense to people? I feel like it's not the most logical way, but I would prefer not to work with a Master server, since it seems like quite a bit of hassle which might not be necessary.
Thanks for any help!
Cheers,
Ivo
P.S. I am aware of the fact that it's not neat coding to put the server-function in the Cube-object itself.
Your answer
Follow this Question
Related Questions
absolute beginner Multiplayer 0 Answers
How fast do Syncvars synchronize? 1 Answer
Network Client Connecting with different Maps 3 Answers
Trying to Connect a Remote Computer from a Different ISP 0 Answers