Skip to content

Dessert RTS Photon Networking Helper for PUN2 #173

Description

@badrian14

Hello there, first of all thanks for the asset and for the effort you put into it, I have been using pun2 for a while with other projects, but I just saw that LockstepFramework uses Pun1, I edited the code to make it work but I still cannot match 2 players, only if I use host button with 1 player in room after the second one conected, it works. Here is the edited code:

`using UnityEngine;
using System.Collections;
using Photon;
using System.Linq;

using Photon.Pun;
using Photon.Realtime;
using Photon;
using ExitGames.Client.Photon;

namespace Lockstep.NetworkHelpers
{
//TODO: Finish PhotonNetworkHelper
public class PhotonNetworkHelper : NetworkHelper, IOnEventCallback
{
[SerializeField]
private bool _directMessages = true;
public override bool IsConnected
{
get
{
return PhotonNetwork.IsConnected && PhotonNetwork.IsMasterClient;
}
}

    public override int ID
    {
        get
        {
            return PhotonNetwork.LocalPlayer.ActorNumber;
        }
    }

    private bool _isServer;

    public override bool IsServer
    {
        get
        {
            return _isServer;
        }
    }

    public override int PlayerCount
    {
        get
        {
            return PhotonNetwork.PlayerList.Length;
        }
    }

    public void OnEvent(EventData photonEvent)
    {
        HandleData(photonEvent.Code, photonEvent.CustomData, photonEvent.SenderKey);
    }

    void HandleData(byte eventCode, object content, int id)
    {
        base.Receive((MessageType)eventCode, (byte[])content);
    }

    const string Version = "1.1";

    public override void Host(int roomSize)
    {
        PhotonNetwork.GameVersion = Version;
        PhotonNetwork.ConnectUsingSettings();
        _isServer = true;
    }

    public override void Connect(string ip)
    {
        PhotonNetwork.GameVersion = Version;
        PhotonNetwork.ConnectUsingSettings();
        _isServer = false;
    }

	#region Hook up to photon events
	public void OnJoinedLobby()
    {

        RoomOptions roomOptions = new RoomOptions();
        TypedLobby typedLobby = new TypedLobby();
        PhotonNetwork.JoinOrCreateRoom("Test", roomOptions, typedLobby);
    }

	public void OnJoinedRoom()
    {
        if (this._isServer)
        {
            
            PhotonNetwork.SetMasterClient(PhotonNetwork.LocalPlayer);
        }
    }

	public void OnPhotonJoinedRoomFailed()
    {
        Debug.Log("Failed Joined Room");
    }
	#endregion
    public override void Disconnect()
    {
        PhotonNetwork.Disconnect();
    }

    bool reliable = false;
    protected override void OnSendMessageToAll(MessageType messageType, byte[] data)
    {
        
        RaiseEventOptions options = RaiseEventOptions.Default;
        if (_directMessages)
        {
			options.Receivers = ReceiverGroup.Others;
            this.Receive(messageType, data);
        }
        else {
			options.Receivers = ReceiverGroup.All;
        }
        SendOptions sendOptions = new SendOptions { Reliability = reliable };
        PhotonNetwork.RaiseEvent((byte)messageType, data, options, sendOptions);

    }

    protected override void OnSendMessageToServer(MessageType messageType, byte[] data)
    {
        if (this.IsServer && this._directMessages)
        {
            this.Receive(messageType,data);
        } else
        {
            Debug.Log(PhotonNetwork.InRoom);

            RaiseEventOptions options = RaiseEventOptions.Default;
            options.Receivers = ReceiverGroup.MasterClient;
            SendOptions sendOptions = new SendOptions { Reliability = reliable };
            Debug.Log((byte)messageType);
            PhotonNetwork.RaiseEvent((byte)messageType, data, options, sendOptions);
        }
    }

    void OnGUI()
    {
        GUILayout.Space(200f);
        GUILayout.Label(PhotonNetwork.NetworkClientState.ToString());
        GUILayout.Label(PhotonNetwork.GetPing().ToString());
    }
}

}
`

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions