ConnectAndJoinRandom.cs help?

Options
zigglr
zigglr
I use random matchmaking, using the ConnectAndJoinRandom.cs script. When three players are in the room, the game will start and each player is assigned a spawn point. This works fine if all players quickly join the room. However someone creates the room, and then the other players don't join for another few minutes, the first player doesn't get spawned. Any ideas why? Thanks a lot for the help.



using UnityEngine;
using System.Collections;
using System;
using UnityEngine.UI;
using System.IO;
using System.Threading;
using System.Collections.Generic;
using System.Linq;


public class ConnectAndJoinRandom : Photon.MonoBehaviour
{

public GameObject myPlayer;
public GameObject secondPlayer;

public int number;

public int count;


private GameObject spawnpoint;



public string playerprefabname = "player";

Vector3 spawner = new Vector3(9.9f, -3.8f, -0.1f);

Text loadtext;



/// Connect automatically? If false you can set this to true later on or call ConnectUsingSettings in your own scripts. public bool AutoConnect = true;

public byte Version = 1;

/// if we don't want to connect in Start(), we have to "remember" if we called ConnectUsingSettings() private bool ConnectInUpdate = true;

public virtual void Start()
{

count = 0;

PhotonNetwork.autoJoinLobby = false; // we join randomly. always. no need to join a lobby to get the list of rooms.


}

public virtual void Update()
{


if (ConnectInUpdate && AutoConnect && !PhotonNetwork.connected)
{
Debug.Log("Update() was called by Unity. Scene is loaded. Let's connect to the Photon Master Server. Calling: PhotonNetwork.ConnectUsingSettings();");

ConnectInUpdate = false;
PhotonNetwork.ConnectUsingSettings(Version + "." + Application.loadedLevel);
}
}

// to react to events "connected" and (expected) error "failed to join random room", we implement some methods. PhotonNetworkingMessage lists all available methods!

public virtual void OnConnectedToMaster()
{
if (PhotonNetwork.networkingPeer.AvailableRegions != null) Debug.LogWarning("List of available regions counts " + PhotonNetwork.networkingPeer.AvailableRegions.Count + ". First: " + PhotonNetwork.networkingPeer.AvailableRegions[0] + " \t Current Region: " + PhotonNetwork.networkingPeer.CloudRegion);
Debug.Log("OnConnectedToMaster() was called by PUN. Now this client is connected and could join a room. Calling: PhotonNetwork.JoinRandomRoom();");
PhotonNetwork.JoinRandomRoom();
}

public virtual void OnPhotonRandomJoinFailed()
{
Debug.Log("OnPhotonRandomJoinFailed() was called by PUN. No random room available, so we create one. Calling: PhotonNetwork.CreateRoom(null, new RoomOptions() {maxPlayers = 4}, null);");
PhotonNetwork.CreateRoom(null, new RoomOptions() { maxPlayers = 3 }, null);
}

// the following methods are implemented to give you some context. re-implement them as needed.

public virtual void OnFailedToConnectToPhoton(DisconnectCause cause)
{
Debug.LogError("Cause: " + cause);
}








public void OnJoinedRoom()
{

++count;
if (PhotonNetwork.playerList.Length == 3)
{

Destroy(loadtext);
Debug.Log("2 Players In Room Starting Level");
GameObject.Find ("findinggame").transform.localScale = new Vector3(0, 0, 0);

if (count == 1)
{
spawnpoint = GameObject.Find("spawnpoint");
}
}

spawnpoint.transform.localEulerAngles = new Vector3(348f, 357f, 12f);

GameObject myPlayer = PhotonNetwork.Instantiate(playerprefabname, spawnpoint.transform.position, spawnpoint.transform.rotation, 0);
myPlayer.name = "player11";



//GameObject MyCam = PhotonNetwork.Instantiate ("Camera", CamPos, Quaternion.identity, 0);
GameObject camera = GameObject.FindWithTag("MainCamera");

if (camera != null)
{
CameraController followScript = camera.GetComponent("CameraController") as CameraController;
if (followScript != null)
{
followScript.target = myPlayer;
}
}



}


public void OnPhotonPlayerConnected(PhotonPlayer newPlayer)
{

++count;

if (PhotonNetwork.playerList.Length == 3)
{

Destroy(loadtext);
GameObject.Find ("findinggame").transform.localScale = new Vector3(0, 0, 0);


Debug.Log("2 Players In Room Starting Level");


if (count == 2)
{

spawnpoint = GameObject.Find("spawnpoint2");


}

if (count == 3)
{

spawnpoint = GameObject.Find("spawnpoint3");


}



}

spawnpoint.transform.localEulerAngles = new Vector3(348f, 357f, 12f);

GameObject secondPlayer = PhotonNetwork.Instantiate(playerprefabname, spawnpoint.transform.position, spawnpoint.transform.rotation, 0);



//GameObject MyCam = PhotonNetwork.Instantiate ("Camera", CamPos, Quaternion.identity, 0);
GameObject camera = GameObject.FindWithTag("MainCamera");

if (camera != null)
{
CameraController followScript = camera.GetComponent("CameraController") as CameraController;
if (followScript != null)
{
followScript.target = secondPlayer;
}
}
}
}

Comments

  • Tobias
    Options
    I don't see anything that's timing relevant. You should debug with logs, if the players join and execute the scripts you expect.

    You code will break when someone leaves before the game starts. The spawnpoints won't get assigned correctly.
  • zigglr
    Options
    Tobias said:

    I don't see anything that's timing relevant. You should debug with logs, if the players join and execute the scripts you expect.

    You code will break when someone leaves before the game starts. The spawnpoints won't get assigned correctly.

    Oh ok, so is there a way to detect when a player leaves? Then I can do Count--

    Thanks
  • I can't necessarily see anything directly related to some sort of count down as @Tobias said. However, just from person experiences is it not easier to have your spawn points as empty game objects on the world, create a list of these spawn points and randomly spawn at one of them? This means the script would work on every scene so long as there is at least one spawn point.

    Also, shouldn't autoJoinLobby be true? This means the player would automatically be pushed into a lobby and then into a room.

    OnJoinedLobby() { OnJoinedRandomRoom() }
    OnJoinedRandomRoom() { SpawnPlayer() }

    Just my personal opinion. don't quite me on any of it all as I'm still trying to learn PUN myself.
  • Tobias
    Options
    You don't have to join a lobby to find rooms. They actually just give you the rooms list, which is not necessary in many cases (and actually bad, if you get it without needing it).

    You don't have to count the players manually. There are callbacks for joining and leaving players (check the class PunBehaviour) and there is a players list, which is kept for you. It's count, is the number of players...

    Per PhotonPlayer, you can get the next active - no matter which ID that client has in a room.
    Example: 1 and 2 joined but but 2 left before the 3rd player joined. The list would be: 1, 3. For player 1, the next player would be 3, not 2...
  • Tobias said:

    You don't have to join a lobby to find rooms. They actually just give you the rooms list, which is not necessary in many cases (and actually bad, if you get it without needing it).

    You don't have to count the players manually. There are callbacks for joining and leaving players (check the class PunBehaviour) and there is a players list, which is kept for you. It's count, is the number of players...

    Per PhotonPlayer, you can get the next active - no matter which ID that client has in a room.
    Example: 1 and 2 joined but but 2 left before the 3rd player joined. The list would be: 1, 3. For player 1, the next player would be 3, not 2...

    I didn't know this myself. Thanks for helping me begin coding my Scoreboard! :blush: