Unity2D: Creating a Cross-Platform Multi-Player Game in Unity Error!
Hi, i'm following this tutorial on raywenderlich to do with multiplayer, since Unity updated, they have added a new method for the participant lefting the event. So I am getting this error on Unity:
Assets/Script/Multiplayer/GPG Multiplayer/MultiplayerController.cs(8,14): error CS0738:
MultiplayerController' does not implement interface member >
GooglePlayGames.BasicApi.Multiplayer.RealTimeMultiplayerListener.OnParticipantLeft(GooglePlayGames.BasicApi.Multiplayer.Participant)' and the best implementing candidateMultiplayerController.OnRoomSetupProgress(float)' return type
void' does not match interface member return type `void'
Whilst following the tutorial, I saw that someone in the comment section had the same error as me and someone replied back, however I wasn't sure how I should implement it as he didn't really explain it in great and clear detail!I also do not know how to create my own OnParticipantLeft(Participant leavingPlayer) method, which is why I was following the tutorial!
Hmm... looks like since this tutorial was written, they've added a new method or two to that interface. Here's the new method they're expecting to see
/// /// Raises the participant left event. /// This is called during room setup if a player declines an invitation /// or leaves. The status of the participant can be inspected to determine /// the reason. If all players have left, the room is closed automatically. /// /// name="participant">Participant that left void > OnParticipantLeft(Participant participant); If you just want to get > your project going again, you can add your own > OnParticipantLeft(Participant leavingPlayer) method to your > MultiplayerController object and have it print out a debug message or > two. > > In an actual game, you could probably use this to make sure, say, your > lobby display doesn't get messed up if a player joins, then leaves > while you're waiting for a third or fourth player to join.
So anyway does anyone know how to fix this error? Below is my code:
using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections;
using GooglePlayGames;
using GooglePlayGames.BasicApi.Multiplayer;
using System.Collections.Generic;
public class MultiplayerController : RealTimeMultiplayerListener
{
public MPLobbyListener lobbyListener;
private static MultiplayerController _instance = null;
private uint minimumOpponents = 1;
private uint maximumOpponents = 1;
private uint gameVariation = 0;
private byte _protocolVersion = 1;
// Byte + Byte + 2 floats for position + 2 floats for velcocity + 1 float for rotZ
private int _updateMessageLength = 22;
private List<byte> _updateMessage;
public MPUpdateListener updateListener;
public string GetMyParticipantId() {
return PlayGamesPlatform.Instance.RealTime.GetSelf().ParticipantId;
}
public List<Participant> GetAllPlayers() {
return PlayGamesPlatform.Instance.RealTime.GetConnectedParticipants ();
}
private void StartMatchMaking() {
PlayGamesPlatform.Instance.RealTime.CreateQuickGame (minimumOpponents, maximumOpponents, gameVariation, this);
}
private MultiplayerController() {
_updateMessage = new List<byte>(_updateMessageLength);
PlayGamesPlatform.DebugLogEnabled = true;
PlayGamesPlatform.Activate ();
}
private void ShowMPStatus(string message) {
Debug.Log(message);
if (lobbyListener != null) {
lobbyListener.SetLobbyStatusMessage(message);
}
}
public void SendMyUpdate(float posX, float posY, Vector2 velocity, float rotZ) {
_updateMessage.Clear ();
_updateMessage.Add (_protocolVersion);
_updateMessage.Add ((byte)'U');
_updateMessage.AddRange (System.BitConverter.GetBytes (posX));
_updateMessage.AddRange (System.BitConverter.GetBytes (posY));
_updateMessage.AddRange (System.BitConverter.GetBytes (velocity.x));
_updateMessage.AddRange (System.BitConverter.GetBytes (velocity.y));
_updateMessage.AddRange (System.BitConverter.GetBytes (rotZ));
byte[] messageToSend = _updateMessage.ToArray();
Debug.Log ("Sending my update message " + messageToSend + " to all players in the room");
PlayGamesPlatform.Instance.RealTime.SendMessageToAll (false, messageToSend);
}
public void SignInAndStartMPGame() {
if (! PlayGamesPlatform.Instance.localUser.authenticated) {
PlayGamesPlatform.Instance.localUser.Authenticate((bool success) => {
if (success) {
Debug.Log ("We're signed in! Welcome " + PlayGamesPlatform.Instance.localUser.userName);
StartMatchMaking();
} else {
Debug.Log ("Oh... we're not signed in.");
}
});
} else {
Debug.Log ("You're already signed in.");
StartMatchMaking();
}
}
public void SignOut() {
PlayGamesPlatform.Instance.SignOut ();
}
public bool IsAuthenticated() {
return PlayGamesPlatform.Instance.localUser.authenticated;
}
public void TrySilentSignIn() {
if (! PlayGamesPlatform.Instance.localUser.authenticated) {
PlayGamesPlatform.Instance.Authenticate ((bool success) => {
if (success) {
Debug.Log ("Silently signed in! Welcome " + PlayGamesPlatform.Instance.localUser.userName);
} else {
Debug.Log ("Oh... we're not signed in.");
}
}, true);
} else {
Debug.Log("We're already signed in");
}
}
public static MultiplayerController Instance {
get {
if (_instance == null) {
_instance = new MultiplayerController();
}
return _instance;
}
}
public void OnRoomSetupProgress (float percent)
{
ShowMPStatus ("We are " + percent + "% done with setup");
}
public void OnRoomConnected (bool success)
{
if (success) {
ShowMPStatus ("We are connected to the room! I would probably start our game now.");
lobbyListener.HideLobby();
lobbyListener = null;
SceneManager.LoadScene("MainGame");
} else {
ShowMPStatus ("Uh-oh. Encountered some error connecting to the room.");
}
}
public void OnLeftRoom ()
{
ShowMPStatus ("We have left the room. We should probably perform some clean-up tasks.");
}
public void OnPeersConnected (string[] participantIds)
{
foreach (string participantID in participantIds) {
ShowMPStatus ("Player " + participantID + " has joined.");
}
}
public void OnPeersDisconnected (string[] participantIds)
{
foreach (string participantID in participantIds) {
ShowMPStatus ("Player " + participantID + " has left.");
}
}
public void OnRealTimeMessageReceived (bool isReliable, string senderId, byte[] data)
{
// We'll be doing more with this later...
byte messageVersion = (byte)data[0];
// Let's figure out what type of message this is.
char messageType = (char)data[1];
if (messageType == 'U' && data.Length == _updateMessageLength) {
float posX = System.BitConverter.ToSingle(data, 2);
float posY = System.BitConverter.ToSingle(data, 6);
float velX = System.BitConverter.ToSingle(data, 10);
float velY = System.BitConverter.ToSingle(data, 14);
float rotZ = System.BitConverter.ToSingle(data, 18);
Debug.Log ("Player " + senderId + " is at (" + posX + ", " + posY + ") traveling (" + velX + ", " + velY + ") rotation " + rotZ);
// We'd better tell our GameController about this.
if (updateListener != null) {
updateListener.UpdateReceived(senderId, posX, posY, velX, velY, rotZ);
}
}
}
}
Thank you. :)
Answer by LuigiDUDE · Aug 03, 2017 at 12:49 PM
A little late, but if you are still stuck:
You are just missing the OnParticipantLeft function. Add this since you're using the RealTimeMultiplayerListener interface. Copy-paste this next function between OnLeftRoom() and OnPeersConnected(string[] participantsIds)
public void OnParticipantLeft(Participant participant)
{
_instance.OnParticipantLeft(participant);
}