Add deactivation parameter in ObjectPickedUp

Cheo

Active member
Hello, I've been doing some work for @discofever on a custom Pickup ability, and had to make the ObjectPickedUp void in Object Pickup virtual and override it because it deactivates or destroys the pickup object. Not all games make their pickups instantly vanish, we may want it to disappear when an event was triggered. @Sangemdoko please consider adding a bool to make this deactivation optional and perhaps making this void virtual. Thanks.
 
In the latest version of the character controller this function should already be virtual.

But I'll add a new destroyDelay which could help for those use cases:


C#:
[Tooltip("Delay in seconds. If set to 0 there will be no delay, if -1 the item will not be destroyed.")]
[SerializeField] protected float m_DestroyDelay;


C#:
        /// <summary>
        /// The object has been picked up.
        /// </summary>
        /// <param name="pickedUpBy">A reference to the object that picked up the object.</param>
        protected virtual void ObjectPickedUp(GameObject pickedUpBy)
        {
            // The object may not have been instantiated within the scene.
            if (m_GameObject == null) {
                return;
            }

            m_IsDepleted = true;

            // Send an event notifying of the pickup.
            EventHandler.ExecuteEvent(pickedUpBy, "OnObjectPickedUp", this);

            // Optionally play a pickup sound if the object picking up the item is attached to a camera.
            // A null GameObject indicates that the clip will play from the AudioManager.
            var foundCamera = Shared.Camera.CameraUtility.FindCamera(pickedUpBy);
            if (foundCamera != null) {
                m_PickupAudioClipSet.PlayAtPosition(m_Transform.position);
            }

            if (m_DestroyDelay == 0) {
                DestroyPickup();
            } else if (m_DestroyDelay > 0) {
                Scheduler.Schedule(m_DestroyDelay, DestroyPickup);
            }
        }

        public virtual void DestroyPickup()
        {
            if (ObjectPoolBase.InstantiatedWithPool(m_GameObject)) {
#if ULTIMATE_CHARACTER_CONTROLLER_MULTIPLAYER
                if (NetworkObjectPool.IsNetworkActive()) {
                    NetworkObjectPool.Destroy(m_GameObject);
                    return;
                }
#endif
                ObjectPoolBase.Destroy(m_GameObject);
            } else {
                // Deactivate the pickup for now. It can appear again if a Respawner component is attached to the GameObject.
                m_GameObject.SetActive(false);
            }
        }

I hope that helps
 
Top