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 How to create Custom Event based on Enum?

Discussion in 'Visual Scripting' started by chumomo, Jul 26, 2023.

  1. chumomo

    chumomo

    Joined:
    Jun 30, 2017
    Posts:
    13
    The official sample is just for ONE event, how to make a node which has mutiple events from enum?
    Such as emum CustomEventEnum{GameStart,GameOver}
    Would someone give an example based on the official sample?
    THKS!

    Code (CSharp):
    1. using Unity.VisualScripting;
    2. using UnityEngine;
    3.  
    4. //Register a string name for your Custom Scripting Event to hook it to an Event. You can save this class in a separate file and add multiple Events to it as public static strings.
    5. public static class EventNames
    6. {
    7.   public static string MyCustomEvent = "MyCustomEvent";
    8. }
    9.  
    10. [UnitTitle("On my Custom Event")]//The Custom Scripting Event node to receive the Event. Add "On" to the node title as an Event naming convention.
    11. [UnitCategory("Events\\MyEvents")]//Set the path to find the node in the fuzzy finder as Events > My Events.
    12. public class MyCustomEvent : EventUnit<int>
    13. {
    14.   [DoNotSerialize]// No need to serialize ports.
    15.   public ValueOutput result { get; private set; }// The Event output data to return when the Event is triggered.
    16.   protected override bool register => true;
    17.  
    18.   // Add an EventHook with the name of the Event to the list of Visual Scripting Events.
    19.   public override EventHook GetHook(GraphReference reference)
    20.   {
    21.       return new EventHook(EventNames.MyCustomEvent);
    22.   }
    23.   protected override void Definition()
    24.   {
    25.       base.Definition();
    26.       // Setting the value on our port.
    27.       result = ValueOutput<int>(nameof(result));
    28.   }
    29.   // Setting the value on our port.
    30.   protected override void AssignArguments(Flow flow, int data)
    31.   {
    32.       flow.SetValue(result, data);
    33.   }
    34. }
     
  2. S2NX7

    S2NX7

    Joined:
    Aug 17, 2021
    Posts:
    26
    You can do this :
    Code (CSharp):
    1. using Unity.VisualScripting;
    2. using UnityEngine;
    3.  
    4. //Register a string name for your Custom Scripting Event to hook it to an Event. You can save this class in a separate file and add multiple Events to it as public static strings.
    5. public static class EventNames
    6. {
    7.     public static string GameStart = "GameStart";
    8.     public static string GameEnd = "GameEnd";
    9. }
    10.  
    11. //Type of events
    12. public enum MyEvents
    13. {
    14.     Start,
    15.     End
    16. }
    17.  
    18. [UnitTitle("On my Custom Event")]//The Custom Scripting Event node to receive the Event. Add "On" to the node title as an Event naming convention.
    19. [UnitCategory("Events\\MyEvents")]//Set the path to find the node in the fuzzy finder as Events > My Events.
    20. public class MyCustomEvent : EventUnit<int>
    21. {
    22.     //Create Enum in header
    23.     [UnitHeaderInspectable]
    24.     public MyEvents _Events;
    25.  
    26.     [DoNotSerialize]// No need to serialize ports.
    27.     public ValueOutput result { get; private set; }// The Event output data to return when the Event is triggered.
    28.     protected override bool register => true;
    29.  
    30.     // Add an EventHook with the name of the Event to the list of Visual Scripting Events.
    31.     public override EventHook GetHook(GraphReference reference)
    32.     {
    33.         switch (_Events)
    34.         {
    35.             case MyEvents.Start:
    36.                 return new EventHook(EventNames.GameStart);
    37.             case MyEvents.End:
    38.                 return new EventHook(EventNames.GameEnd);
    39.  
    40.                 default: return new EventHook(EventNames.GameStart);
    41.         }
    42.    
    43.     }
    44.     protected override void Definition()
    45.     {
    46.         base.Definition();
    47.         // Setting the value on our port.
    48.         result = ValueOutput<int>(nameof(result));
    49.     }
    50.     // Setting the value on our port.
    51.     protected override void AssignArguments(Flow flow, int data)
    52.     {
    53.         flow.SetValue(result, data);
    54.     }
    55. }
    You cannot change the enum in playmode nothing happens
     
    Last edited: Jul 26, 2023
  3. chumomo

    chumomo

    Joined:
    Jun 30, 2017
    Posts:
    13
    Thks!!
     
  4. S2NX7

    S2NX7

    Joined:
    Aug 17, 2021
    Posts:
    26
    np