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

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.

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

You could add a new enum property to Lesson class called LessonType, then switch on enum - LessonType.

@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!

Hey i made custom node that allows you to switch on type here is the node9280702--1300210--Explanation.png

9280702–1300213–SwitchOnType.cs (2.41 KB)

just put the script in your project and regenerate the nodes

How are you adding Lessons to the Chapter List? By hand or via a script?

I’m adding them by hand via a Scriptable Object.

using UnityEngine;
using Unity.VisualScripting;
using System.Collections;
using System.Collections.Generic;

[CreateAssetMenu(fileName = "New Chapter", menuName = "Chapter", order = 11)]

public class Chapter : ScriptableObject
{
    [SerializeField]
    public string chapterName;

    [SerializeField]
    public List<Lesson> thisChapter;

}

Wow, thank you very much!

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.

using UnityEngine;
using System.Collections.Generic;

[CreateAssetMenu(fileName = "New Chapter", menuName = "Chapter", order = 11)]

public class Chapter : ScriptableObject
{
    [SerializeField] public string chapterName;

    [SerializeField] public List<Lesson> thisChapter = new();

#if UNITY_EDITOR
    private void OnValidate()
    {
        foreach (Lesson lesson in thisChapter)
        {
            if (lesson is Story)
            {
                lesson.LessonType = LessonTypes.Story;
            }
            if (lesson is Puzzle)
            {
                lesson.LessonType = LessonTypes.Puzzle;
            }
        }
    }
#endif
}

public enum LessonTypes
{
    Story,
    Puzzle
}

Here is one more Unity Visual Scripting Unit. For Select On Type.
SelectOnType_UVSUnit.cs (2.5 KB)