- Home /
Create custom Bolt visual scripting node (unit) from script
I would like to create a custom unit with properties. Currently, I can do this by creating a method in a MonoBehavior and then selecting the method from the flow graph manually.
public class GS_Bolt_Setting : MonoBehaviour
{
...
public void Variable(string type, string name)
{
}
}
This displays the unit with a type and a name
Is there a way to create this unit automatically from script?
Answer by SurreyMuso · Aug 17, 2021 at 09:28 AM
In your Project Assets window, create a C# script and paste the following. After saving and returning to Unity from your editor, it should compile automatically and be added to the list of Nodes in Fuzzy Finder.
using System;
using Unity;
using UnityEngine;
using Unity.VisualScripting;
[IncludeInSettings(true)]
[UnitTitle("Mult3")]
[UnitSubtitle("Multiply A x B x C")]
[TypeIcon(typeof(Multiply<float>))]
public class Mult3 : Unit
{
[DoNotSerialize] public ControlInput inTrigger;
[DoNotSerialize] public ValueInput inA;
[DoNotSerialize] public ValueInput inB;
[DoNotSerialize] public ValueInput inC;
[DoNotSerialize] public ControlOutput outTrigger;
[DoNotSerialize] public ValueOutput outA;
private float result;
protected override void Definition()
{
inTrigger = ControlInput("", (flow) =>
{
result = flow.GetValue<float>(inA) * flow.GetValue<float>(inB) * flow.GetValue<float>(inC);
return outTrigger;
});
outTrigger = ControlOutput("");
inA = ValueInput<float>("A");
inB = ValueInput<float>("B");
inC = ValueInput<float>("C");
outA = ValueOutput<float>("A x B x C", (flow) => result);
Requirement(inA, inTrigger);
Requirement(inB, inTrigger);
Requirement(inC, inTrigger);
Succession(inTrigger, outTrigger);
Assignment(inTrigger, outA);
}
}
Here is the official Unity documentation, which will help you customise my template. I have found that it's sometimes necessary to Regenerate Nodes in Project Settings > Visual Scripting.
You may prefer to remove the TypeIcon line or replace it with another icon (most icon names are obvious).
Feel free to ask away!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Custom Bolt Nodes Documentation 0 Answers
How can I make function run in the editor only when I press a button in the inspector? 1 Answer
Add lines to a script from another script 0 Answers
I need Line drawn in play mode to appear in editor even after exiting play mode. 1 Answer