Firstborn/Library/PackageCache/com.unity.visualscripting@1.../Documentation~/vs-create-custom-node-add-l...
Schaken-Mods b486678290 Library -Artifacts
Library -Artifacts
2023-03-28 12:24:16 -05:00

2.5 KiB

Add logic to a Custom C# node

Note

To add logic to a node, you must create a Custom C# node and add ports. [!includevs-tasks-note-end]

After you create a Custom C# node and add ports, you can add logic to a node. Add logic to tell Visual Scripting what the node does with any data it receives from its ports.

To add logic to a node:

  1. [!includeopen-project-window]

  2. [!includeopen-existing-external-code]

  3. In your external editor, add any logic for the node within the lambda expression that handles the assignment of the inputTrigger. For example, you can take the values of the two string input ports added in the previous example and concatenate them, as shown in the following code:

    using System;
    using Unity.VisualScripting;
    using UnityEngine;
    
    public class MyNode : Unit
    {
    [DoNotSerialize]
    public ControlInput inputTrigger;
    
    [DoNotSerialize]
    public ControlOutput outputTrigger;
    
    [DoNotSerialize]
    public ValueInput myValueA;
    
    [DoNotSerialize]
    public ValueInput myValueB;
    
    [DoNotSerialize]
    public ValueOutput result;
    
    private string resultValue;
    protected override void Definition()
    {
        //The lambda to execute our node action when the inputTrigger port is triggered.
        inputTrigger = ControlInput("inputTrigger", (flow) =>
        {
            //Making the resultValue equal to the input value from myValueA concatenating it with myValueB.
            resultValue = flow.GetValue<string>(myValueA) + flow.GetValue<string>(myValueB) + "!!!";
            return outputTrigger;
        });
        outputTrigger = ControlOutput("outputTrigger");
    
        myValueA = ValueInput<string>("myValueA", "Hello ");
        myValueB = ValueInput<string>("myValueB", String.Empty);
        result = ValueOutput<string>("result", (flow) => resultValue);
    }
    }
    
  4. [!includesave-script]

  5. [!includereturn-unity]

  6. Do one of the following:

Next steps

After you add logic to a node, add relations to ensure that the node displays correctly in Visual Scripting.