Search Unity

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

Discussion in 'Visual Scripting' started by Carcophan, Oct 5, 2021.

  1. Carcophan

    Carcophan

    Joined:
    May 10, 2021
    Posts:
    79
    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?

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


    Code (CSharp):
    1.  public void UpdatePanelSlotsForUsedItems()
    2.     {
    3.         var index = 0;
    4.         foreach (Item item in list)
    5.         {
    6.             // do not take more items with you when there is no more space
    7.             if (index >= maxActivatedPowerUps)
    8.             {
    9.                 return;
    10.             }
    11.             if (!item.IsActivated())
    12.             {
    13.                 continue;
    14.             }
    15.  
    16.             var obj = inventoryPanel.transform.GetChild(index);
    17.             obj.gameObject.SetActive(true);
    18.             var slot= obj.GetComponent<InventorySlotController>();
    19.             slot.item = item;
    20.             slot.UpdateSelectedInfo();
    21.             index++;
    22.         }
    23.         // update the rest of the free spots
    24.         for(var i = index; i < maxActivatedPowerUps; i++)
    25.         {
    26.             var obj = inventoryPanel.transform.GetChild(i);
    27.             obj.gameObject.SetActive(true);
    28.             var slot= obj.GetComponent<InventorySlotController>();
    29.             slot.item = null;
    30.             slot.UpdateSelectedInfo();
    31.         }
    32.     }
     
  2. mikeNspired

    mikeNspired

    Joined:
    Jan 13, 2016
    Posts:
    82
    Since your UpdatePanelSlotsForUsedItems is public. Go to Project settings and click this.
    upload_2021-10-5_14-30-11.png

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

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

    Carcophan

    Joined:
    May 10, 2021
    Posts:
    79
    This is amazing advice - thank you. I look forward to trying it soon.

    Edit:
    Success!! Thank you again :D
    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).

    upload_2021-10-7_7-57-53.png
     
    Last edited: Oct 7, 2021