- Home /
UNET Server side seeding?
So what I want to do isn't very complex and I hope it's just some syntax or a small bit of understanding that I'm missing.
But basically I want to have my server generate a seed which all my clients are able to use to generate identical randomly generated levels (the server generates the seed randomly but each clients has the same seed essentially).
The way I implemented this was to have a gameObject (this object just exists locally in scene) with Network Identity which used a NetworkBehaviour script to check it it was the server and if it was it would generate a seed in the form of a string which was also a [SyncVar]. It would then use a cmd to sync the seed.
The code looks something like this:
[SyncVar]
public string seed = "";
public int roomSize = 12;
void Start () {
if(isServer){
GenSeedString();
CmdSyncSeed(seed);
}
}
public void GenSeedString(){
string temp = "";
for(int i=0; i < 800; i++){
int n = Random.Range(0,roomSize);
temp = temp +n+"%";
}
seed = temp;
}
[Command]
void CmdSyncSeed(string s){
seed = s;
}
When I load it up the seeds just aren't the same between client and server?
Answer by tkcast · Jan 26, 2016 at 08:06 PM
Commands are sent from player objects on the client to player objects on the server. Since you're calling CmdSyncSeed only from the server (because of the 'isServer' check), it shouldn't be a [Command]. CmdSyncSeed should be an ordinary function, without the [Command] parameter. ref: http://docs.unity3d.com/Manual/UNetActions.html
Check the server and client seeds a while after generating it this way. Please give it a try, I hope this solves your problem!