Basically, I have a minecraft clone that I’m working on and I want to be able to share the masterclient’s seed with every player in the room and then set every players seed except the masterclient to the masterclient’s seed.
So, let’s say that I get a seed when I start my server and someone joins the server, I want to be able to modify their seed by replacing their original seed with mine (the masterclient’s seed) and I want to do this to every player that joins the server, but I don’t know how.
I’m using Photon for my networking and here are all my scripts related to networking in my game and the script that handles the seed itself:
NetworkManager (this manages spawning players to the server)
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;
public class NetworkManager : MonoBehaviour
{
public Transform spawn;
public GameObject serverSetup;
public GameObject noRoomsObject;
public GameObject serverN;
public GameObject playerN;
public void NewRoom()
{
string servername = serverN.GetComponent<InputField>().text.ToString();
string playername = playerN.GetComponent<InputField>().text.ToString();
serverSetup.SetActive(false);
PhotonNetwork.CreateRoom(servername);
Debug.Log ("Created a new room / " + servername.ToString());
PhotonNetwork.playerName = playername;
if(playername == null || playername.Length == 0)
{
string newName = "Player_" + (string)Random.Range(30, 50000).ToString();
PhotonNetwork.playerName = newName;
}
}
void Awake ()
{
Connect();
}
void Connect()
{
PhotonNetwork.ConnectUsingSettings("FairNSquare multiplayer v1");
Debug.Log("Connected to a lobby");
}
void OnJoinedLobby ()
{
if(PhotonNetwork.connected == true)
{
serverSetup.SetActive(true);
Debug.Log("Setting up a server manually...");
}
}
public void JoinRandomRoom()
{
RoomInfo[] rooms = PhotonNetwork.GetRoomList();
if(rooms.Length > 0)
{
PhotonNetwork.JoinRandomRoom();
serverSetup.SetActive(false);
}
else
{
Debug.LogError("No rooms available");
noRoomsObject.SetActive(true);
}
}
void OnJoinedRoom ()
{
Debug.Log("Joined a room / " + PhotonNetwork.room.name.ToString());
serverSetup.SetActive(false);
noRoomsObject.SetActive(false);
SpawnPlayer(PhotonNetwork.player.name.ToString());
}
void SpawnPlayer (string name)
{
GameObject myPlayer = (GameObject)PhotonNetwork.Instantiate("Player", spawn.transform.position, Quaternion.identity, 0);
Debug.Log("Spawned player: " + PhotonNetwork.playerName.ToString());
myPlayer.GetComponentInChildren<Camera>().enabled = true;
((MonoBehaviour)myPlayer.GetComponent("FirstPersonController")).enabled = true;
myPlayer.GetComponent<PlayerBehavior>().enabled = true;
Debug.Log("Spawned player " + name.ToString());
myPlayer.name = PhotonNetwork.playerName.ToString();
}
}
NetworkPlayer (this takes care of syncing every players position and rotation across the network and it also makes them move smoothly)
using UnityEngine;
using System.Collections;
public class NetworkPlayer : Photon.MonoBehaviour
{
Vector3 realPosition = Vector3.zero;
Quaternion realRotation = Quaternion.identity;
void Update ()
{
if(photonView.isMine)
{
//Do nothing
}
else
{
transform.position = Vector3.Lerp (transform.position, realPosition, 0.2f);
transform.rotation = Quaternion.Lerp (transform.rotation, realRotation, 0.2f);
}
}
public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if(stream.isWriting)
{
stream.SendNext(transform.position);
stream.SendNext(transform.rotation);
}
if(stream.isReading)
{
realPosition = (Vector3)stream.ReceiveNext();
realRotation = (Quaternion)stream.ReceiveNext();
}
}
}
World (this handles spawning new chunks and chooses random materials for them and it handles the seed as well)
using UnityEngine;
using System.Collections;
public class World : MonoBehaviour {
public static World currentWorld;
public Material[] materials;
public bool randomChunkMaterials;
public int chunkWidth = 20, chunkHeight = 20, seed = 0;
public float viewRange = 30;
public bool randomSeed;
//public Chunk chunkFab;
void Awake () {
currentWorld = this;
if(seed == 0 && randomSeed == true)
{
seed = Random.Range(0, int.MaxValue);
}
}
void Update () {
for(float x = transform.position.x - viewRange; x < transform.position.x + viewRange; x+= chunkWidth)
{
for(float z = transform.position.z - viewRange; z < transform.position.z + viewRange; z+= chunkWidth)
{
Vector3 pos = new Vector3 (x,0,z);
pos.x = Mathf.Floor(pos.x / (float)chunkWidth) * chunkWidth;
pos.z = Mathf.Floor(pos.z / (float)chunkWidth) * chunkWidth;
Chunk chunk = Chunk.FindChunk(pos);
if(chunk != null) continue;
if(PhotonNetwork.connected == true && PhotonNetwork.room != null)
{
GameObject newChunk;
newChunk = (GameObject)PhotonNetwork.Instantiate("Chunk", pos, Quaternion.identity, 0, null);
if(randomChunkMaterials == true)
{
newChunk.GetComponent<Renderer>().material = materials[Random.Range(0, materials.Length)];
}
}
//chunk = (Chunk)Instantiate(chunkFab, pos, Quaternion.identity);
//NewChunk(pos, Quaternion.identity);
}
}
}
}