Help with Network Culling

I'm in the process of writing some network culling logic. I have read this: https://doc.photonengine.com/en-us/pun/current/gameplay/interestgroups

and I have also read the culling demo which lead me to the Culling Handler and Culling area components. Taking what I have learned there I was trying to cull based on player distance dynamically. So here is the code that I have, I want to make sure that I am writing/understanding this correctly.

So here is the idea of this following code. I have spheres at different distances and when the owner photonview player is within a certain circle's distance they will sub to a group, enable sending to that group, and set the photon view of the other player to that group. Is my thinking here correct?
[SerializeField] protected List<CullDistance> cullDistances = new List<CullDistance>();
[SerializeField] protected byte last_group = 255;

protected List<byte> _subToGroups = new List<byte>();
protected int _cullIndex = 0;
protected int _finalIndex = 0;
protected List<SyncPlayer> _allPlayers = new List<SyncPlayer>();
protected List<SyncPlayer> _tmpList = new List<SyncPlayer>();
protected int _loops = 0;

public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
        {
            if (photonView.IsMine)
            {
                SetInterestGroups();

                _cullIndex++;
                if (_cullIndex > _finalIndex)
                {
                    _cullIndex = -1;
                    _finalIndex--;
                    if (_finalIndex < 0)
                    {
                        _finalIndex = cullDistances.Count;
                    }
                }
                if (_cullIndex == -1 && _loops == 0)
                {
                    PhotonNetwork.SetSendingEnabled(new byte[0], null);
                    photonView.Group = 0;
                }
                else if (_cullIndex < cullDistances.Count && _subToGroups.Count > 0 && _subToGroups.Contains(cullDistances[_cullIndex].interest_group))
                {
                    PhotonNetwork.SetSendingEnabled(new byte[0], new byte[1] { cullDistances[_cullIndex].interest_group });
                    photonView.Group = cullDistances[_cullIndex].interest_group;
                }
                else if (_cullIndex == cullDistances.Count && _finalIndex == cullDistances.Count)
                {
                    _loops = (_loops+1 > 3) ? 0 : _loops + 1;
                    PhotonNetwork.SetSendingEnabled(new byte[0], new byte[1] { last_group });
                    photonView.Group = last_group;
                }
            }
        }

protected void SetInterestGroups()
        {
            _subToGroups.Clear();
            _allPlayers.Clear();
            _tmpList.Clear();
            _allPlayers.AddRange(FindObjectsOfType<SyncPlayer>());
            foreach (CullDistance item in cullDistances)
            {
                foreach (SyncPlayer player in _allPlayers)
                {
                    if (Vector3.Distance(transform.position, player.transform.position) <= item.distance)
                    {
                        _tmpList.Add(player);
                    }
                }
                if (_tmpList.Count > 0)
                {
                    foreach (SyncPlayer player in _tmpList)
                    {
                        _allPlayers.Remove(player);
                        player.photonView.Group = item.interest_group;
                    }
                    _tmpList.Clear();
                    _subToGroups.Add(item.interest_group);
                }
            }
            if (_allPlayers.Count > 0)
            {
                foreach (SyncPlayer player in _allPlayers)
                {
                    player.photonView.Group = last_group;
                }
                _subToGroups.Add(last_group);
            }
            PhotonNetwork.SetInterestGroups(new byte[0], _subToGroups.ToArray());
        }

Comments

  • Let me clarify a little bit here. I see jittery movement when the other players get further away (which is what I expect) but they smooth out again when they get to the "last_group" which is not what I would expect to happen. Is my thinking off a little bit here. I would normally expect the "last_group" to be the move jittery.
  • Could you make sense of it?
  • The code always subscribes to the last_group, right? So no matter where anyone is, the group is subscribed. If a user actually is in said group, it makes sense all updates go into that?
Sign In or Register to comment.