Search Unity

Custom Class Troubles

Discussion in 'Visual Scripting' started by Kobayen, Sep 4, 2021.

  1. Kobayen

    Kobayen

    Joined:
    Apr 17, 2014
    Posts:
    6
    Using VS under 2021.1.19f1
    Following the instructions on another thread, I made my own custom class and Unity regenerated a few units to handle them but I noticed it didnt generate a Set New Chatline unit like it has for Vector2, here included to illustrate my point.

    upload_2021-9-4_9-15-24.png

    This is my code. If I missed something, I'm listening. If I need to make a function to have the Set unit, how do I do that?

    Code (CSharp):
    1. using Unity.VisualScripting;
    2. using UnityEngine;
    3. [Inspectable]
    4. public class ChatLine
    5. {
    6.     [Inspectable]
    7.     public Sprite Pict;
    8.     [Inspectable]
    9.     public string Name;
    10.     [Inspectable]
    11.     public string Body;
    12. }
     
  2. PanthenEye

    PanthenEye

    Joined:
    Oct 14, 2013
    Posts:
    2,068
    You can't really "Set" a class but you should be able to create a new instance of it via the "new" keyword for programmer naming and "Create" for human naming. UnityVS might default to human naming, I don't recall.
     
  3. Kobayen

    Kobayen

    Joined:
    Apr 17, 2014
    Posts:
    6
    UnityVS uses Create. But the Create Chat Line lacks inputs.
    upload_2021-9-5_15-57-51.png
     
  4. Hikiko66

    Hikiko66

    Joined:
    May 5, 2013
    Posts:
    1,304
    Your script needs a constructor, with inputs
     
  5. Kobayen

    Kobayen

    Joined:
    Apr 17, 2014
    Posts:
    6
    I trust it will surprise nobody that I had to look up whats a constructor.
    For whoever newbie stumbles on this someday, this is what a functioning custom class with a constructor looks like.

    Code (CSharp):
    1. using Unity.VisualScripting;
    2. using UnityEngine;
    3. [Inspectable]
    4. public class ChatLine
    5. {
    6.     [Inspectable]
    7.     public Sprite Pict;
    8.     [Inspectable]
    9.     public string Name;
    10.     [Inspectable]
    11.     public string Body;
    12.  
    13.     public ChatLine()
    14.     {    }
    15.  
    16.     public ChatLine(Sprite consPict, string consName, string consBody)
    17.     {
    18.         this.Pict = consPict;
    19.         this.Name = consName;
    20.         this.Body = consBody;
    21.     }
    22. }
    The empty
    public CLASS()
    constructor is important. UnityVS will throw errors without it. Remember to regenerate your units. Someday Ill figure how to add default values to new classes, but not today.
     
  6. PanthenEye

    PanthenEye

    Joined:
    Oct 14, 2013
    Posts:
    2,068
    You could solve that with a SuperUnit (also called SubGraph in newer UnityVS versions).

    1. Create a new Super Unit graph asset. You can search for "Super Unit" (or was it "Superunit"?) in the fuzzy finder. Created this way, it's embedded in the graph and non-reusable. But you can convert it to a graph asset from the Graph Inspector sidebar with the superunit selected. Or create a new graph asset the regular way and add Input and Output nodes manually.
    2. In the superunit create a new instance of the class and set it to a graph variable.
    3. Add all the individual Set nodes for the default values and wire them into the Super Unit's Input node. Use the newly created class instance as the target for Set nodes.
    4. For superunit input value ports, there's a bool. hasDefaultValue or something along those lines. You can access it in the Graph Inspector's sidebar with Input node selected, one of the last options for a value port. This will let you enter different input values per Super Unit's instance right on the Superunit node.
    5. Output the graph variable containing ChatLine instance to Output node's value port

    This gives you a reusable ChatLine instance creator node that can also set different default values per instance.
     
    Last edited: Sep 6, 2021