1. Hey

    I'm trying to figure out how to check if a player is in range of their cupboard.

    I have a list called ProtectedCupboard which contains the player as type BasePlayer and the cupboard as type BaseEntity. Is there a way I can check if the player is within the range of that cupboard?

    Something like:
    Code:
    if (ProtectedCupboard[player].GetComponent<BuildingPriviledge>.PlayersInRange.Contains(player)
    Thanks!
     
  2. Wulf

    Wulf Community Admin

    You can check if they are in a building privilege zone, but not sure if that would help get who the main user for that cupboard is.
     
  3. I already have a system in place to track the Tool Cupboards and who owns them, I just need to find a way to check if a player is in Tool Cupboard range and get the Tool Cupboard that they're in range of as a BaseEntity.
     
  4. This snippets should show you the path...
    Code:
    FieldInfo _buildingPrivilege = typeof(BasePlayer).GetField("buildingPrivilege", (BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance | BindingFlags.NonPublic));List<BuildingPrivlidge> buildingPrivilege = (List<BuildingPrivlidge>)_buildingPrivilege.GetValue(entity as BasePlayer);
    foreach (var cup in buildingPrivilege)
        Debug.Log(cup as BaseEntity);
     
  5. Thankyou! I'm now using this code which (I hope) should work:

    Code:
    if (ProtectedCupboard.ContainsKey(HitPlayer))
                    {
                        FieldInfo _buildingPrivilege = typeof(BasePlayer).GetField("buildingPrivilege", (BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance | BindingFlags.NonPublic));                    List<BuildingPrivlidge> buildingPrivilege = (List<BuildingPrivlidge>)_buildingPrivilege.GetValue(entity as BasePlayer);
                        foreach (var cup in buildingPrivilege)
                        {
                            BaseEntity c = cup as BaseEntity;
                            if (c == ProtectedCupboard[HitPlayer])
                            {
                                //they are protected
                                info.damageTypes = new DamageTypeList();
                                info.HitMaterial = 0;
                                info.PointStart = Vector3.zero;
                                BasePlayer AttackingPlayer = info.Initiator as BasePlayer;
                                SendReply(AttackingPlayer, "This person is currently protected by starter protection for x hours.");
                            }
                        }
                    }
    [DOUBLEPOST=1472073616][/DOUBLEPOST]Is there a way I could check if a building/BuildingBlock is in the tool cupboard range? Would a better way be to create a sphere around the TC and then check if the position of the BuildingBlock is within the sphere?
     
  6. Physics.OverlapSphereNonAlloc would be a good way to achieve such lookups...

    Code:
    // UnityEngine.Physics
    /// <summary>
    ///   <para>Computes and stores colliders touching or inside the sphere into the provided buffer.</para>
    /// </summary>
    /// <param name="position">Center of the sphere.</param>
    /// <param name="radius">Radius of the sphere.</param>
    /// <param name="results">The buffer to store the results into.</param>
    /// <param name="layerMask">A that is used to selectively ignore colliders when casting a ray.</param>
    /// <param name="queryTriggerInteraction">Specifies whether this query should hit Triggers.</param>
    /// <returns>
    ///   <para>The amount of colliders stored into the results buffer.</para>
    /// </returns>
    public static int OverlapSphereNonAlloc(Vector3 position, float radius, Collider[] results, [DefaultValue("AllLayers")] int layerMask, [DefaultValue("QueryTriggerInteraction.UseGlobal")] QueryTriggerInteraction queryTriggerInteraction)
    {
        return Physics.INTERNAL_CALL_OverlapSphereNonAlloc(ref position, radius, results, layerMask, queryTriggerInteraction);
    }
    
     
  7. I got this error in console when adding that function to my plugin:
    Code:
    error CS0117: 'UnityEngine.Physics' does not contain a definition for 'INTERNAL_CALL_OverlapSphereNonAlloc'
    [DOUBLEPOST=1472075279][/DOUBLEPOST]Would using Physics.OverlapSphere work?
    I've written this code:
    Code:
    if (entity is BuildingBlock && info.Initiator is BasePlayer)
                {
                    BasePlayer AttackingPlayer = info.Initator as BasePlayer;
                    BuildingBlock AttackedBuildingBlock = entity as BuildingBlock;
                   
                    List<Collider> hitColliders = Physics.OverlapSphere(entity.CenterPoint(), 25f).ToList();                if (hitColliders.Contains(AttackedBuildingBlock.GetComponent<Collider>))
                    {
                        SendReply(AttackingPlayer, "This building is protected by starter protection for x hours.");
                    }
                }
    One thing I can't figure out is how to get the building block's (AttackedBuildingBlock) collider. Any ideas?
     
  8. Nevermind - I fixed it.
    I ended up just getting the position of the cabinet and the position of the building block, and then checking if they were within a radius of 25 (the TC radius) using Vector3.Distance.