Forum rules - please read before posting.

Inventory Combine Events not working in runtime

edited March 8 in Technical Q&A

I working on solution where you an drag items from the inventory and drag dem around in the scene and use them on other items.
I have been trying to use AC's Combine System, so everything can be handled though ActionLists.

The challenge: Combine Events do not work in runtime when items are being spawned from the inventory and into the scene.

  • I have set up hotspots on items that are being spawned. I have testet and get debug logs when I click the spawned item in the scene that shows they are active
  • I have tried to force a combination through script (with the help of ChatGPT), but can't get it to activate Actionlists.
    (When I drag one item from the inventory into the scene and drag another and use one of them on the other nothing happens)

  • I have tried to debug with AC's EventMananger but don't get a Combine Event logged.

What I'm trying to achieve:

  • The player drags an item from the inventory and drag it into the scene (Ie a chair)
  • The player drags an item from the inventory and drag it into the scene - either dropping it or directly on the chair (ie a teddybear)
  • ActionList will take over what happens after (An animation with the teddyboard sitting on the chair, deleting items etc)

My questions is:
How do I get AC to register Combine Events in runtime so I also can run ActionLists from a runtime-spawned item?
Is there an official AC way to to handle combine between runtime-spawned items?

I have attrached the scripts I have been using (based on some from the arranging puzzle, help from Chris and some chatGPT)

I have also included screenshots of my Inventory Setup and Item prefab:
https://imgur.com/a/VufFPRn

Any direction would be much appreciated
Thanks,
Dan

Scripts:

Inventory Prefab Spawner:

using UnityEngine;
using AC;

public class InventoryPrefabSpawner : MonoBehaviour
{
    public string menuName = "Inventory";
    public string elementName = "Items";
    public bool removeSpawnedItems;
    public float inventoryHeightThreshold = 0.125f; // 12.5% af skærmhøjden
    public int sortingLayerOnDrag = 500; // Høj sorting order, så item er øverst

    private InvInstance clickedInstance;
    private bool isDragging;
    private bool itemSpawned;
    private GameObject spawnedItem;
    private SpriteRenderer spawnedSpriteRenderer;

    void OnEnable() { EventManager.OnMenuElementClick += OnMenuElementClick; }
    void OnDisable() { EventManager.OnMenuElementClick -= OnMenuElementClick; }

    void Update()
    {
        if (isDragging && !itemSpawned)
        {
            Vector3 currentMousePos = KickStarter.playerInput.GetMousePosition();
            float mouseYNormalized = currentMousePos.y / Screen.height;

            if (mouseYNormalized > inventoryHeightThreshold)
            {
                SpawnClickedItem();
                itemSpawned = true;
            }
        }

        if (isDragging && itemSpawned && spawnedItem != null)
        {
            Vector3 newPosition = GetTouchWorldPosition();
            spawnedItem.transform.position = new Vector3(newPosition.x, newPosition.y, 0f);
        }

        if (isDragging && Input.GetMouseButtonUp(0))
        {
            isDragging = false;

            if (spawnedSpriteRenderer != null)
            {
                spawnedSpriteRenderer.sortingOrder = 0;
            }

            spawnedItem = null;
        }
    }

    void OnMenuElementClick(Menu menu, MenuElement element, int slot, int buttonPressed)
    {
        if (KickStarter.runtimeInventory.SelectedItem != null) return;

        if (menu.title == menuName && element.title == elementName)
        {
            MenuInventoryBox inventoryBox = element as MenuInventoryBox;
            clickedInstance = inventoryBox.GetInstance(slot);

            if (clickedInstance != null)
            {
                isDragging = true;
                itemSpawned = false;
            }
        }
    }

    private void SpawnClickedItem()
    {
        if (!InvInstance.IsValid(clickedInstance)) return;
        GameObject prefab = clickedInstance.InvItem.linkedPrefab;
        if (prefab == null) return;

        if (removeSpawnedItems)
        {
            KickStarter.runtimeInventory.PlayerInvCollection.Delete(clickedInstance, 1);
        }

        Vector3 spawnPosition = GetTouchWorldPosition();
        spawnedItem = Instantiate(prefab, spawnPosition, Quaternion.identity);

        if (!spawnedItem.GetComponent<DraggableSceneItem>())
        {
            spawnedItem.AddComponent<DraggableSceneItem>();
        }

        Hotspot hotspot = spawnedItem.GetComponent<Hotspot>();
        if (hotspot == null)
        {
            hotspot = spawnedItem.AddComponent<Hotspot>();
        }
        hotspot.provideUseInteraction = true;

        Debug.Log("✅ Hotspot tilføjet til " + spawnedItem.name);

        spawnedSpriteRenderer = spawnedItem.GetComponent<SpriteRenderer>();
        if (spawnedSpriteRenderer != null)
        {
            spawnedSpriteRenderer.sortingOrder = sortingLayerOnDrag;
        }

        spawnedItem.transform.position = new Vector3(spawnPosition.x, spawnPosition.y, 0f);
        KickStarter.runtimeInventory.SetNull();
    }

    private Vector3 GetTouchWorldPosition()
    {
        Vector3 touchPosition = KickStarter.playerInput.GetMousePosition();
        touchPosition.z = 0f;
        return Camera.main.ScreenToWorldPoint(touchPosition);
    }
}

Script for forcing Combine Event in runtime:
(That causes errors)

using UnityEngine;
using AC;

    public class ForceCombineEvent : MonoBehaviour
    {
        public static void TryCombine(InvInstance item1, InvInstance item2)
        {
            if (item1 == null || item2 == null) return;

            Debug.Log($"🚀 Tvinger combine: {item1.InvItem.label} + {item2.InvItem.label}");

            InvCombineInteraction interaction = KickStarter.inventoryManager.GetCombineInteraction(item1.InvItem, item2.InvItem);
            if (interaction != null && interaction.actionList != null)
            {
                Debug.Log($"✅ ActionList fundet: {interaction.actionList.name}");
                KickStarter.actionListManager.Interact(interaction.actionList);
            }
            else
            {
                Debug.LogWarning("⚠️ Ingen ActionList fundet for denne kombination!");
            }
        }
    }

Comments

  • Don't bother with the ChatGPT script - it's inventing functions that don't exist.

    You're using the Arranging Puzzle template, right? Open up ArrangingPuzzlePiece and add the [SerializeField] attribute in front of the associatedItemID variable near the top. That'll then expose it in the Inspector - set this value to the ID number of the Inventory item it should represent. You should then just be able to create Inventory interactions on the Hotspots - nothing to do with Inventory combine interactions in the Inventory Manager.

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.