Forum rules - please read before posting.

Placing object when enough amount of inventory items

edited October 2020 in Technical Q&A

Hey everyone!

I have tried to work this out myself, but after a week of moving around in the AC editor I just didn't manage to make this work. We want to create a game where someone can get wood/stone etc. and collect these. This works perfectly. However we also want to use these inventory items (which have multiple of each instance) to be used as resources. I tried using the crafting menu, but it doesn't do what we need it to do.

What do we need?
We need a simple menu with certain objects, and when you click on an object and drag it to a location, it will be placed on that location if you have enough resources and the x amount of resources will be taken from the inventory.

I have now used a container which I call, in which the object is shown. I plan on dragging (or clicking) this object and placing this in the scene. Now I still have to make sure it is actually placed when clicked in the scene & that it only is placed when x amount of resources are available.

Anyone have an idea how to make this work?
_
It is made with sprites and 2D assets!_

Kind regards,
Ivar

Comments

  • edited October 2020

    Welcome to the comunity, @ivar_dailymedia.

    Sounds similar to this thread. Essentially, you can hook into the OnInventoryDeslect event to place a sprite in the scene at the cursor position.

    Are you looking to have the item be placed arbitrarily (i.e. anywhere the user clicks), or only in certain areas?

    You might need some further tweaking, but give this a try first:

    using UnityEngine;
    
    namespace AC
    {
    
        public class PlaceItem : MonoBehaviour
        {
    
            void OnEnable () { EventManager.OnInventoryDeselect_Alt += OnInventoryDeselect; }
            void OnDisable () { EventManager.OnInventoryDeselect_Alt -= OnInventoryDeselect; }
    
            void OnInventoryDeselect (InvInstance invInstance, InvCollection invCollection)
            {
                if (invInstance.InvItem.linkedPrefab)
                {
                    float cameraDepth = transform.position.z - Camera.main.transform.position.z;
                    Vector3 screenPosition = new Vector3 (KickStarter.playerInput.GetMousePosition ().x, KickStarter.playerInput.GetMousePosition ().y, cameraDepth);
                    Vector3 spawnPosition = Camera.main.ScreenToWorldPoint (screenPosition);
    
                    GameObject spawnedObject = Instantiate (invInstance.InvItem.linkedPrefab);
                    spawnedObject.transform.position = spawnPosition;
    
                    invCollection.Delete (invInstance, 1); // Delete 1 count of the item instance
                }
            }
    
        }
    
    }
    

    For any item you want to be able to place in the scene, create an equivalent Sprite prefab for it and set it as the item's Linked prefab in the Inventory Manager.

    Then, place the script above on a GameObject in the scene. When you de-select an item, it'll then spawn that prefab in the scene and reduce the item's count by 1.

    This does make use of the updated inventory system in v1.72, however, so you'll need to update if you haven't already.

  • Dear Chris,

    Thanks for replying! I will try to make it work as soon as I get the chance!

    I will get back to you if I can't get it to work, but for now thank you very much!

    Kind regards,
    Ivar

  • edited October 2020

    Dear @ChrisIceBox ;

    So I have been trying to get this to work since last Thursday. And I simply don't know what I am doing wrong.

    what have I done:

    • I have created a prefab from the sprite in the inventory and linked this in the inventory manager.
    • tried to tweak the code to match the name of the class
    • tried to get rid of the errors.

    When I try to add the script to the game object I get this error:
    Can't add script component 'PlaceBuildings' because the script class cannot be found. Make sure that there are no compile errors and that the file name and class name match.

    (the script is called PlaceBuildings.cs, so the file name and class name should match)

    I managed to get quite far by getting rid of the errors; but these are still present and I don't know what I am doing wrong. Probably the lack of knowledge about C# is the issue here. Could you take a short look into it and tell me where I go wrong?

        using UnityEngine;
    
        namespace AC
        {
    
            public class PlaceBuildings : MonoBehaviour
            {
                void OnEnable()
                {
                    EventManager.OnInventorySelect += OnInventorySelect;
                    EventManager.OnInventoryDeselect += OnInventoryDeselect;
                }
                void OnDisable()
                {
                    EventManager.OnInventorySelect -= OnInventorySelect;
                    EventManager.OnInventoryDeselect -= OnInventoryDeselect;
                }
    
                void OnInventoryDeselect(InvInstance invInstance, InvCollection invCollection)
                {
                    if (invInstance.InvItem.linkedPrefab)
                    {
                        float cameraDepth = transform.position.z - Camera.main.transform.position.z;
                        Vector3 screenPosition = new Vector3(KickStarter.playerInput.GetMousePosition().x, KickStarter.playerInput.GetMousePosition().y, cameraDepth);
                        Vector3 spawnPosition = Camera.main.ScreenToWorldPoint(screenPosition);
    
                        GameObject spawnedObject = Instantiate(invInstance.InvItem.linkedPrefab);
                        spawnedObject.transform.position = spawnPosition;
    
                        invCollection.Delete(invInstance, 1); // Delete 1 count of the item instance
                    }
                }
    
            }
    
        }
    

    These are the errors I get:

    Error   CS0103  The name 'OnInventorySelect' does not exist in the current context  Assembly-CSharp     LIne 16
    
    Error   CS0123  No overload for 'OnInventoryDeselect' matches delegate 'EventManager.Delegate_Inventory'    Assembly-CSharp     Line 12     
    
    Error   CS0123  No overload for 'OnInventoryDeselect' matches delegate 'EventManager.Delegate_Inventory'    Assembly-CSharp     Line 17 
    
    Error   CS0103  The name 'OnInventorySelect' does not exist in the current context  Assembly-CSharp     Line 11     
    

    The bugger here is that I can't test the game as long as I don't fix these errors; but I don't know specifically what I am doing wrong if I can't fix it.

    I hope you could take the time to tell me what I am doing wrong.

    Kind regards,
    Ivar Blaauw

  • CS0103 The name 'OnInventorySelect' does not exist in the current context

    You're hooking into the OnInventorySelect event in your OnEnable and OnDisable functions, but don't provide the mentioned function to hook into.

    You either need to remove mention of "OnInventorySelect" in your function, or add a suitable function to the script, i.e.

    void OnInventorySelect (InvItem invItem)
    {}
    
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.