Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade implementation to support Netcode For Game Objects v1.0-pre (Formerly MLAPI) #3

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

JayPeet
Copy link

@JayPeet JayPeet commented Aug 11, 2021

Unsure if you are taking PRs, but I was planning on using this for my game which I am currently migrating to MLAPI and needed to upgrade this implementation to support the latest public release.

@Budtom
Copy link
Owner

Budtom commented Aug 18, 2021

Thanks for the PR, this is great 😄 I'll give it a test when I get chance and merge if possible 👍

@JayPeet
Copy link
Author

JayPeet commented Aug 18, 2021

No problem!

I'm mid porting my old game from UNet across to MLAPI, so I have not even managed to test it myself yet... (1800 errors, down to 4). I should be able to test it my side properly soon

I also have an implementation of IDissonancePlayer for MLAPI which I will either add to this PR, or do another PR for later.

@Budtom
Copy link
Owner

Budtom commented Aug 19, 2021

Amazing 👏 Feel free to comment here if you have any issue 👍

@Widdershin
Copy link

Widdershin commented Sep 10, 2021

Here's a working implementation of IDissonancePlayer:

using Dissonance;
using MLAPI;
using MLAPI.NetworkVariable;
using UnityEngine;

/// <summary>
/// When added to the player prefab, allows Dissonance to automatically track
/// the location of remote players for positional audio for games using the
/// Unity MLAPI networking library.
/// </summary>
[RequireComponent(typeof(NetworkObject))]
public class MlapiPlayer : NetworkBehaviour, IDissonancePlayer
{
    public Transform voiceOrigin;
    private DissonanceComms _comms;

    public NetworkVariableString playerName = new NetworkVariableString(new NetworkVariableSettings { WritePermission = NetworkVariablePermission.OwnerOnly }, "");

    public string PlayerId
    {
        get { return playerName.Value; }
    }

    public Vector3 Position
    {
        get { return voiceOrigin.position; }
    }

    public Quaternion Rotation
    {
        get { return voiceOrigin.rotation; }
    }

    public NetworkPlayerType Type
    {
        get
        {
            if (_comms == null || PlayerId  == null)
                return NetworkPlayerType.Unknown;
          
            return IsLocalPlayer ? NetworkPlayerType.Local : NetworkPlayerType.Remote;
        }
    }

    public bool IsTracking { get; private set; }

    public void OnEnable()
    {
        _comms = FindObjectOfType<DissonanceComms>();
    }

    public void OnDisable()
    {
        if (IsTracking) { 
            _comms.StopTracking(this);
            IsTracking = false;
        }
    }

    public override void NetworkStart()
    {
        base.NetworkStart();
        if (!_comms)
        {
            return;
        }

        if (IsLocalPlayer)
        {

            if (_comms.LocalPlayerName != null)
            {
                _comms_LocalPlayerNameChanged(_comms.LocalPlayerName);
            }
            else
            {
                _comms.LocalPlayerNameChanged += _comms_LocalPlayerNameChanged;
            }
        }
        else
        {
            if (playerName.Value != "")
            {
                OnPlayerNameChanged("", playerName.Value);
            }
            playerName.OnValueChanged += OnPlayerNameChanged;
        }
    }

    private void _comms_LocalPlayerNameChanged(string newPlayerName)
    {
        playerName.Value = _comms.LocalPlayerName;
        _comms.TrackPlayerPosition(this);
        IsTracking = true;
    }

    private void OnPlayerNameChanged(string oldName, string newName)
    {
        _comms.TrackPlayerPosition(this);
        IsTracking = true;
    }
}

I've built a game with this that's up and running, and I can confirm it works. The only other change I've had to make to this project is switching usage of IsHost to IsServer, since IsHost returns false if you're not also a client.

@JayPeet
Copy link
Author

JayPeet commented Sep 10, 2021

Awesome!

I have an implementation my side which I planned on merging into this PR, but feel free to open a seperate PR for your implementation. (I have been a bit busy porting my game to newer tech)

@JayPeet
Copy link
Author

JayPeet commented Sep 10, 2021

Good call on the IsHost -> IsServer changes. Ill have to review that before completing this PR

@Budtom
Copy link
Owner

Budtom commented Sep 10, 2021

Thanks both, @Widdershin feel free to PR this :) I will get around to testing this one and merging.

@Widdershin
Copy link

Sweet I'll make some PRs soon.

One more thing (will make a separate PR for this), but wanted to mention it now in case anyone needs it.

All uses of NetworkBufferPool.GetBuffer() need to be replaced with PooledNetworkBuffer.Get(). Otherwise we can get our hands on a disposed buffer, which will be leaked when Dispose is called, since disposed buffers aren't returned to the pool.

If is anyone is hitting the 1024 buffers have been created. Did you forget to dispose? message with this project, that's why.

@JayPeet
Copy link
Author

JayPeet commented Sep 28, 2021

Whoops, was not aware that was how it worked. Ill update it now

Not even been able to test as I got really busy with work / the project

@JayPeet
Copy link
Author

JayPeet commented Oct 30, 2021

Back on this today, got a release tomorrow which has required me to make the switch from UNET to MLAPI.

MLAPI is all different since I did this, but I just ported the games codebase over to MLAPI, now known as Netcode for Game Objects (NFGO? Lame acronym)

Going to rework this PR to handle NFGO 1.0s prerelease as I require it for my game. Should have it reworked today

@JayPeet
Copy link
Author

JayPeet commented Oct 30, 2021

Ported anyway. Still not tested though as my games just in an unusable state while I port other stuff

Will be attempting to implement IDissonancePlayer for a MlapiPlayer class as my game uses the HlapiPlayer class

…hich is not supported with Mlapis NetworkVariable. I cant actually see why it was a sync var, since it also sends it across an RPC
@JayPeet
Copy link
Author

JayPeet commented Oct 30, 2021

MlapiClient: Caught fatal error: Attempted to write without first calling TryBeginWrite()

Looks like I am supposed to call that beforehand. I dont recall seeing that in the docs so....

@JayPeet
Copy link
Author

JayPeet commented Oct 31, 2021

Fixed. Tested and working!

@JayPeet JayPeet changed the title Upgrade MLAPI implementation to support MLAPI 0.1.0 Upgrade implementation to support Netcode For Game Objects v1.0-pre (Formerly MLAPI) Oct 31, 2021
@Widdershin
Copy link

Nice! Just as a heads up as well, there's an official implementation published on the Unity Store now :)

https://assetstore.unity.com/packages/tools/integration/dissonance-for-netcode-for-gameobjects-206514

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants