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.

Question How to use Enum as variables?

Discussion in 'Visual Scripting' started by holicgl, Jul 27, 2022.

  1. holicgl

    holicgl

    Joined:
    Jun 23, 2019
    Posts:
    2
    upload_2022-7-27_19-7-35.jpeg
    “Error drawing Enuminspecto”. I tested if I skip setting this enum and other graphs are working.
     
  2. Creiz

    Creiz

    Joined:
    Jun 6, 2017
    Posts:
    119
    Sorry for the late reply, but you don't use enums like that.

    Unity_kkqkNBBw8a.png

    This is an example of how I'm using it.

    Enums, as I understand it, are "States". You can change its "result" by setting its state, like so:

    Unity_Sf4iWXrouk.png

    If I choose Dungeon in here, then the Enum will be set at "Dungeon". The "Switch on Enum" node will then go to the "Dungeon" arrow.
     
    holicgl likes this.
  3. PanthenEye

    PanthenEye

    Joined:
    Oct 14, 2013
    Posts:
    1,033
    The idea is somewhat correct, but enums are not directly connected to state machines even though it's a common use case.

    OPs problem is that he's setting the "State" variable to an integer value. This might work in C# because it's a strongly typed language and you can cast an int to enum (because that is what enums are in the background - int values), but in Visual Scripting variables are dynamically typed, so a different value type will overwrite the previous value. i.e. enum becomes an int and Switch on Enum can't parse that.

    OP should use Enum literal nodes to directly select the enum value and use that for Set Variable nodes. The system should have automatically generated "Pawn State of Character State Enum" literal.
     
    holicgl likes this.
  4. HyperFlame217

    HyperFlame217

    Joined:
    Jan 5, 2023
    Posts:
    2
    Can someone please tell me how to make a enum in visual scripting. I need it for a project and I can't seem to figure it out.
     
  5. PanthenEye

    PanthenEye

    Joined:
    Oct 14, 2013
    Posts:
    1,033
    You can only create them in C#.

    EDIT: Paste this in an empty C# script, save the script and then regenerate Nodes in Project Settings/Visual Scripting. It'll come up in search after that.

    Code (CSharp):
    1. public enum WeaponType
    2. {
    3.       Sword,
    4.       Bow,
    5.       Spear
    6. }
     
  6. HyperFlame217

    HyperFlame217

    Joined:
    Jan 5, 2023
    Posts:
    2
    Thanks it's working now