Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Dynamically change ValueOutput node type based on ValueInput type

Discussion in 'Visual Scripting' started by toomasio, Apr 1, 2021.

  1. toomasio

    toomasio

    Joined:
    Nov 19, 2013
    Posts:
    198
    Hello,

    I am playing around with custom nodes and was wondering if there was a way to change my output based on the input, and have it update in the graph view.

    I do realize there are already InputSystem nodes built in, but humor me as I will probably want to do this in the future.

    example:
    _.jpg

    ^I would like the output node to change to a "bool" value in this case.

    I was also wondering if there is a proper method for updating the node if something changes in the node?

    Code (CSharp):
    1. protected override string hookName =>
    2.             InputSystem.settings.updateMode ==
    3.             InputSettings.UpdateMode.ProcessEventsInDynamicUpdate
    4.             ? EventHooks.Update
    5.             : EventHooks.FixedUpdate;
    6.  
    7.         [DoNotSerialize] public ValueInput inputAction;
    8.         [PortLabelHidden] public ValueOutput outputValue;
    9.  
    10.         protected InputAction action;
    11.         protected Guid lastAction;
    12.         protected string type;
    13.         protected bool actionRunning;
    14.         protected float _floatValue;
    15.         protected Vector2 _vector2Value;
    16.         protected bool _boolValue;
    17.  
    18.         public override EventHook GetHook(GraphReference reference)
    19.         {
    20.             action = Flow.FetchValue<InputAction>(inputAction, reference);
    21.             if (action.id != lastAction)
    22.             {
    23.                 Debug.Log(lastAction);
    24.                 lastAction = action.id;
    25.                 type = action.expectedControlType;
    26.  
    27.                 outputValue = null;
    28.  
    29.                 if (type == "Vector2")
    30.                     outputValue = ValueOutput<Vector2>(nameof(outputValue));
    31.                 else if (type == "Float")
    32.                     outputValue = ValueOutput<float>(nameof(outputValue));
    33.                 else if (type == "Button")
    34.                     outputValue = ValueOutput<bool>(nameof(outputValue));
    35.             }
    36.             return base.GetHook(reference);
    37.         }
    38.  
    39.         protected override void Definition()
    40.         {
    41.             base.Definition();
    42.             inputAction = ValueInput(nameof(inputAction), (InputAction)default);
    43.  
    44.             if (action == null) return;
    45.             type = action.expectedControlType;
    46.            
    47.             if (type == "Vector2")
    48.                 outputValue = ValueOutput(nameof(outputValue), _ => _vector2Value);
    49.             else if (type == "Float")
    50.                 outputValue = ValueOutput(nameof(outputValue), _ => _floatValue);
    51.             else if (type == "Button")
    52.                 outputValue = ValueOutput(nameof(outputValue), _ => _boolValue);
    53.         }
    When using the GetHook method, I keep getting a duplicate output error:
    Code (CSharp):
    1. ArgumentException: Duplicate output for 'outputValue'
    Is this possible?

    Thanks,