Use a section of C# code within a larger Visual Scripting / Script Machine

Hello everyone.

I’ve been building out a bunch of Visual Scripting elements, but found a cool Inventory module written in C#. I’d like to build the entire inventory system in VS, but to save time, used the Asset Store sample in its place.

It works as-is, with the mouse interacting with everything. I am looking to continue to use my existing Visual Scripting as well as this C# section.

The problem I am having is trying to use a keyboard interaction to active/de-active the objects. The C# script, below, runs an update when the player closes the inventory window out with the ‘x’ button in the corner - which would not occur if I just active/de-active the objects through VS.

I’d like to know how to run a section of C# script (public void UpdatePanelSlotsForUsedItems), shown below, but similar to how you would run a Custom Event Trigger (when I click a keyboard button).

It is easy enough to get the objects to be visible / not visible on screen, but triggering this specific C# component, which is a critical element in updating the inventory, escapes me right now.

How can I run just ‘parts of code’ from VS, as if they were Custom Events?

7549348--933103--upload_2021-10-5_15-51-59.png

 public void UpdatePanelSlotsForUsedItems()
    {
        var index = 0;
        foreach (Item item in list)
        {
            // do not take more items with you when there is no more space
            if (index >= maxActivatedPowerUps)
            {
                return;
            }
            if (!item.IsActivated())
            {
                continue;
            }

            var obj = inventoryPanel.transform.GetChild(index);
            obj.gameObject.SetActive(true);
            var slot= obj.GetComponent<InventorySlotController>();
            slot.item = item;
            slot.UpdateSelectedInfo();
            index++;
        }
        // update the rest of the free spots
        for(var i = index; i < maxActivatedPowerUps; i++)
        {
            var obj = inventoryPanel.transform.GetChild(i);
            obj.gameObject.SetActive(true);
            var slot= obj.GetComponent<InventorySlotController>();
            slot.item = null;
            slot.UpdateSelectedInfo();
        }
    }

Since your UpdatePanelSlotsForUsedItems is public. Go to Project settings and click this.

Now you should be able to access it in visual scripting like this.
7549507--933142--upload_2021-10-5_14-29-22.png

Now when you click space bar, it will call whatever method you choose

This is amazing advice - thank you. I look forward to trying it soon.

Edit:
Success!! Thank you again :smile:
7553875--933955--upload_2021-10-7_7-57-10.png

As a note to others - I was unable to get it to work by just clicking ‘Generate’, but after using both ‘Regenerate Units’ and ‘Fix Missing Scripts’, it became available. (I am guessing it was the latter, but I didn’t notice it until after I used the Units button).