Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Run Visual Script from code and get return value

Discussion in 'Visual Scripting' started by sdebaun_unity, May 12, 2021.

  1. sdebaun_unity


    Mar 12, 2021
    (I am aware of CustomEvents and use them a bunch but don't see how they could solve this use case, because I need the return value)

    A third party asset requires me to inherit from their base classes to extend their system. One of the methods I must overload has to have a return value that the underlying system relies on.

    I'd like to be able to do something like:

    Code (CSharp):
    1. public class Foo : extends 3PartyThing.BaseClass // which extends monobehavior
    2. {
    3.   // flowmachine because i want it to manage its own state via variables in a scene
    4.   [SerializeField] public FlowMachine targetFlow; // attached to a gameobject in a scene
    6.   // 3p asset calls this and relies on the int returned to decide what to do
    7.   public void overload int OverloadedFunctionWithReturnValue
    8.   {
    9.     // obviously this isn't the right code, but is it even possible?
    10.     targetFlow.Invoke()
    11.     return targetFlow.valueOutput
    12.   }
    13. }
    With the goal that I can have a generic class that I use with the third party asset, and then just link it to different flowmachines wherever I use it, and keep my domain logic in visual scripts. With the idea that I could design those visual scripts like SuperUnits, having an Input and Output node.

    I found a couple threads that sort of reference calling visual scripts from code, but they dont cover this. I've written a bunch of debug code and plodded through the collections and methods for Flow, FlowGraph, GraphReference, but don't see a way to do this. The API docs are... underwhelmingly informative.

    Is this possible? Any ideas? Am I missing something that makes this impossible?

    io-games and Peter-Chen like this.
  2. Neodrop


    Oct 24, 2008
    You can send CustomEvent with some custome parameter (any your class instance for example) and fill this instance with returned data inside the bolt graph