Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice

Question Can I get the "Index Of" node to find an object inside of a Generic List?

Discussion in 'Visual Scripting' started by literacy, Sep 28, 2023.

  1. literacy

    literacy

    Joined:
    Nov 14, 2021
    Posts:
    56
    I defined a Class called Game State and put another class in it called Activity State.

    I have two graph variables:

    * One is currentActivity, of type Activity State.
    * Another is listOfActivities, which is a List of Activity States.

    I want to look inside of the listOfActivities and get the index number of the Activity State that matches currentActivity. (I know for sure that the item is in the list - just want the index number of it.)

    I am getting an error:

    upload_2023-9-28_17-25-7.png

    InvalidConversionException: Cannot convert from 'System.Collections.Generic.List`1[ActivityState]' to 'Unity.VisualScripting.AotList'.

    From what I understand, the Index Of node can not work on generic lists like Activity State. It can only work on AOT lists.

    I have tried two different paths:

    1. Write a custom node that can get an Index Of for a generic list, so I can plug in my List of Activity States directly into that.
    2. Write a custom node that can convert my generic List of Activity States into an AOT list.

    I have failed at both of these attempts. There is a third hacky option:

    3. I could hack a subgraph which just iterates through every item on the list and looks for a match.

    I guess I could do #3, but I feel like there must be a simpler way to access the Index Of an item that I know to be on a generic list.

    Does anyone have advice on how to proceed?
     
  2. Trindenberg

    Trindenberg

    Joined:
    Dec 3, 2017
    Posts:
    396
    Use IList -> IndexOf

    upload_2023-9-28_19-28-39.png
     
    literacy likes this.
  3. literacy

    literacy

    Joined:
    Nov 14, 2021
    Posts:
    56
    Ah wonderful! I found it in my Unity under the name List Interface:

    upload_2023-9-29_10-9-25.png

    Seems to be working well, thanks so much!!
     
    Trindenberg likes this.