Forum rules - please read before posting.

Using Opsive Controller in adding weapons

Hi, I was wondering if anyone knows how i would do a action to add opsive controller weapons and ammo via a hotspot. I did do an empty game object and added a pickup script:

using UnityEngine;
using Opsive.UltimateCharacterController.Inventory;

///


/// Picks up the specified ItemType within the Start method.
///

public class Pickup : MonoBehaviour
{
[Tooltip("The character that should pickup the item.")]
[SerializeField] protected GameObject m_Character;
[Tooltip("The ItemType that the character should pickup.")]
[SerializeField] protected ItemType m_ItemType;
[Tooltip("The number of ItemType that the character should pickup.")]
[SerializeField] protected int m_Count = 1;

/// <summary>
/// Picks up the ItemType.
/// </summary>
public void Start()
{

}

public void pickupEvent()
{
    var inventory = m_Character.GetComponent<InventoryBase>();
    if (inventory == null)
    {
        return;
    }

    // Picks up the ItemType:
    // ItemType: The ItemType to pick up.
    // Count: The amount of ItemType to pick up.
    // -1: The slot ID that picked up the item. A -1 value will indicate no specified slot.
    // true: Should the item be picked up immediately? If false the EquipUnequip ability may not switch to the newly picked up item.
    // false: Should the item be force equipped?
    inventory.PickupItemType(m_ItemType, m_Count, -1, true, false);
}

}

and yes it adds the weapon but it doesn't add any ammo to it. I have a separate weapon with no bullets and the player has to pick up ammo as well but weapon gets added but no ammo at all. I don't really script so I got the script from opsive documentation. And also I do know how to do actions with copying the action template but how would I do an action out of that script. Any help would be much appreciated.

Comments

  • For help about the ammo, you'll need to consult the Opsive forums/developers.

    For converting this to a custom Action, you'd move the code in the pickupEvent function to the Action's Run function, copy over the variables, and expose Editor fields to set their values in the ShowGUI function.

    Something like this:

    using UnityEngine;
    #if UNITY_EDITOR
    using UnityEditor;
    #endif
    using Opsive.UltimateCharacterController.Inventory;
    
    namespace AC
    {
    
        [System.Serializable]
        public class ActionPickUp : Action
        {
    
            [SerializeField] protected bool isPlayer;
            [SerializeField] protected GameObject m_Character;
            [SerializeField] protected ItemType m_ItemType;
            [SerializeField] protected int m_Count = 1;
    
    
            public ActionPickUp ()
            {
                this.isDisplayed = true;
                category = ActionCategory.Custom;
                title = "Pick up Opsive";
            }
    
    
            override public float Run ()
            {
                InventoryBase inventory = (isPlayer) ? KickStarter.player.GetComponent <InventoryBase>() : m_Character.GetComponent<InventoryBase>();
                if (inventory == null)
                {
                    return;
                }
    
                inventory.PickupItemType (m_ItemType, m_Count, -1, true, false);
                return 0f;
            }
    
    
            #if UNITY_EDITOR
    
            override public void ShowGUI ()
            {
                isPlayer = EditorGUILayout.Toggle ("Is player?", isPlayer);
                if (!isPlayer)
                {
                    m_Character = (GameObject) EditorGUILayout.ObjectField ("Character:", m_Character, typeof (GameObject), true);
                }
                m_ItemType = (ItemType) EditorGUILayout.EnumPopup ("Item type:", m_ItemType);
                m_Count = EditorGUILayout.IntField ("Count:", m_Count);
    
                AfterRunningOption ();
            }
    
            #endif
    
        }
    
    }
    

    Know, however, that you don't necessarily need a custom Action for this, as you already have a script that performs what you need. You can use the Object: Call event or Object: Send message Actions to run the "pickupEvent" function on the original component so long as its placed on a GameObject in your scene.

  • Yeah, I have consulted the documentation which doesn't help when you don't code and all I get from the developer is I didn't do the integration ask the developer of adventure creator. He did say i have to call pickupItemType.

  • Sorry that action doesn't work because of too many errors.

  • Hi got my script to work with ammo and all. I can't do it in a action because of this:
    m_ItemType = (ItemType) EditorGUILayout.EnumPopup ("Item type:", m_ItemType);
    Its saying can not convert string to enum.

  • Try just:

    m_ItemType = (ItemType) EditorGUILayout.EnumPopup (m_ItemType);
    

    Failing that, post the full script itself, along with the exact error message (in full) you're getting.

  • It still doesn't work.
    https://imgur.com/jz9igPX
    I took a screenshot of it with the popup error ok.

  • m_ItemType = (ItemType) EditorGUILayout.ObjectField ("Item type:", m_ItemType, typeof (ItemType), true);
    
  • Thank you I have it all working now.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.