- Home /
The question is answered, right answer was accepted
Photon Pun 2, How to have all players using the same random generation than the host's?
Okay, so I am new in Photon, and in Unity. I have followed a few tutorials here and there to build what i have right now. I have managed to create a room, to make another player see the room. When the first player create a game, the program generates a random 2D map with planets of different sizes. The problem is, when the second player joins the room, he generates it's own random map and doesn't use the one of the first player. Here are the two files that I think have an error.
CreateRoom.cs :
using UnityEngine;
using Photon.Pun;
using Photon.Realtime;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class CreateRoom : MonoBehaviourPunCallbacks
{
[SerializeField]
private Text roomName;
[SerializeField]
private int maxPlayer = 5;
private RoomOptions roomOptions = new RoomOptions();
public void OnClick_CreatePhotonRoom()
{
if (!PhotonNetwork.IsConnected)
{
Debug.Log("You're not connected to the server! Connect to the server before trying to create a room");
return;
}
else
{
roomOptions.MaxPlayers = (byte) maxPlayer;
//roomOptions.EmptyRoomTtl = 15000;
if(roomName.text != null)
{
PhotonNetwork.CreateRoom(roomName.text, roomOptions, TypedLobby.Default);
}
else
{
Debug.LogError("You have to specify a room name before creating a room duh");
}
}
}
public override void OnCreatedRoom()
{
Debug.Log("Room " + roomName.text + " successfully created.");
PhotonNetwork.JoinRoom(roomName.text);
}
public override void OnCreateRoomFailed(short returnCode, string message)
{
Debug.Log("Failed to create the room" + roomName.text + ", cause : " + message.ToString());
}
public override void OnJoinedRoom()
{
Debug.Log("Room " + roomName.text + " successfully joined! :D");
if(PhotonNetwork.IsMasterClient)
{
PhotonNetwork.LoadLevel("InGame");
}
}
public override void OnJoinRoomFailed(short returnCode, string message)
{
Debug.Log("Failed to join the room" + roomName.text + ", cause : " + message.ToString());
}
}
The second one is a button on an image which when clicked, join the room of the first player :
using Photon.Pun;
using Photon.Realtime;
using UnityEngine;
using TMPro;
public class ListingPrefab : MonoBehaviourPunCallbacks
{
[SerializeField]
private TextMeshProUGUI text;
public RoomInfo roomInfo { get; private set; }
public void SetRoomInfo(RoomInfo roomInfo)
{
this.roomInfo = roomInfo;
text.text = roomInfo.Name + " Max players : " + roomInfo.MaxPlayers;
}
public void OnClick_JoinRoom()
{
Debug.Log(roomInfo.Name);
PhotonNetwork.JoinRoom(roomInfo.Name.ToString());
}
public override void OnJoinedRoom()
{
Debug.Log("We are connected to the room : " + roomInfo.Name);
PhotonNetwork.LoadLevel("InGame");
}
public override void OnJoinRoomFailed(short returnCode, string message)
{
Debug.Log("Failed entering the room : " + roomInfo.Name + " for cause : " + message);
}
}
Thanks for any help :)
Answer by gangafinti · Dec 01, 2019 at 06:02 PM
Use a random seed and send it to all the clients, they can then generate the exact same content.
https://docs.unity3d.com/530/Documentation/ScriptReference/Random-seed.html
I just made it through. I thought about a seed but i have get better with algorithms and stuff :)) The thing i have done is I activated the GenerationWorld() $$anonymous$$ethod for all the player who aren't masterClients, Changed all my Instantiate calls by InstantiateSceneObject and then Sync all my planets with everybody. Sorry for having asked something not that difficult :))
Follow this Question
Related Questions
Custom Properties, Unity, Object refence not set to an instance of an object 0 Answers
Callback for DNS entry error using Photon Networking 0 Answers
PUN (Photon Unity Networking) Players Move Other Players 1 Answer
PUN2, other player's prefab not populating in assigned field in unity editor 1 Answer
Photon disconnect after switching from mobile data to WIFI 1 Answer