Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Elegant way to Switch on Type in Visual Scripting, without using ToString?

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

  1. literacy

    literacy

    Joined:
    Nov 14, 2021
    Posts:
    50
    I have a class called Lesson; Lessons can be either a Puzzle or a Story.

    When I load each Lesson, I'd like to check first if it's a Puzzle or Story.

    I'm able to do this in Visual Scripting with the following code, by converting the Type to a String and then doing a Switch on String.

    upload_2023-9-7_12-19-38.png

    Is there a more elegant way to do this in Visual Scripting without converting to strings? Thank you!
     
  2. PanthenEye

    PanthenEye

    Joined:
    Oct 14, 2013
    Posts:
    1,719
    You could add a new enum property to Lesson class called LessonType, then switch on enum - LessonType.
     
  3. literacy

    literacy

    Joined:
    Nov 14, 2021
    Posts:
    50
    @PanthenEye Thanks! Yah I could, but would that require me to manually set a pulldown for each Lesson?

    Here's how I'm using it: I've defined a Chapter as a List of Lessons... and then I want to go through the Lessons and automatically render each as either a Puzzle or a Story.

    Is there a way to do that automatically with the eNum approach you mentioned? Thanks!
     
  4. S2NX7

    S2NX7

    Joined:
    Aug 17, 2021
    Posts:
    26
    Hey i made custom node that allows you to switch on type here is the node Explanation.png
     

    Attached Files:

  5. S2NX7

    S2NX7

    Joined:
    Aug 17, 2021
    Posts:
    26
    just put the script in your project and regenerate the nodes
     
  6. PanthenEye

    PanthenEye

    Joined:
    Oct 14, 2013
    Posts:
    1,719
    How are you adding Lessons to the Chapter List? By hand or via a script?
     
  7. literacy

    literacy

    Joined:
    Nov 14, 2021
    Posts:
    50
    I'm adding them by hand via a Scriptable Object.

    Code (CSharp):
    1. using UnityEngine;
    2. using Unity.VisualScripting;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5.  
    6. [CreateAssetMenu(fileName = "New Chapter", menuName = "Chapter", order = 11)]
    7.  
    8. public class Chapter : ScriptableObject
    9. {
    10.     [SerializeField]
    11.     public string chapterName;
    12.  
    13.     [SerializeField]
    14.     public List<Lesson> thisChapter;
    15.  
    16. }
     
  8. literacy

    literacy

    Joined:
    Nov 14, 2021
    Posts:
    50
    Wow, thank you very much!
     
    S2NX7 likes this.
  9. PanthenEye

    PanthenEye

    Joined:
    Oct 14, 2013
    Posts:
    1,719
    You could have an OnValidate method in Chapter SO that loops over thisChapter items and sets the LessonType enum property based on Lesson type automatically whenever you update the list by hand.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections.Generic;
    3.  
    4. [CreateAssetMenu(fileName = "New Chapter", menuName = "Chapter", order = 11)]
    5.  
    6. public class Chapter : ScriptableObject
    7. {
    8.     [SerializeField] public string chapterName;
    9.  
    10.     [SerializeField] public List<Lesson> thisChapter = new();
    11.  
    12. #if UNITY_EDITOR
    13.     private void OnValidate()
    14.     {
    15.         foreach (Lesson lesson in thisChapter)
    16.         {
    17.             if (lesson is Story)
    18.             {
    19.                 lesson.LessonType = LessonTypes.Story;
    20.             }
    21.             if (lesson is Puzzle)
    22.             {
    23.                 lesson.LessonType = LessonTypes.Puzzle;
    24.             }
    25.         }
    26.     }
    27. #endif
    28. }
    29.  
    30. public enum LessonTypes
    31. {
    32.     Story,
    33.     Puzzle
    34. }