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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Resolved Calling a method on a singleton? Equivalent in visual scripting

Discussion in 'Visual Scripting' started by Maskedchamo, Aug 27, 2021.

  1. Maskedchamo

    Maskedchamo

    Joined:
    Mar 12, 2018
    Posts:
    23
    Hi,

    I am learning visual scripting. I used to create singletons in C# and call some of their public methods from other classes.

    I can't find an equivalent in visual scripting. Passing information from a script to another seems to be reliant on events only. It doesn't seem to be a bad thing, but before I create too many of these events I'd like to be sure I am not missing out on something.

    Thanks in advance.
     
  2. PanthenEye

    PanthenEye

    Joined:
    Oct 14, 2013
    Posts:
    1,791
    Yea, communication between graphs right now is entirely event based via Custom Events which need a target GameObject. No global events are currently available by default.

    One solution is to target both the trigger and the receiver of a Custom Event to the same GameObject which serves as sort of central event distributor GameObject.

    If that does not work for you. You can try Bolt Community Addons Defined Events which do have a global broadcasting variant. Those need a lil bit of C# scripting, however. Tutorial/docs for them are here: https://github.com/RealityStop/Bolt.Addons.Community/releases/tag/2.4

    Bolt Addons also have UnityVS compatible branches.
     
    Maskedchamo likes this.
  3. Maskedchamo

    Maskedchamo

    Joined:
    Mar 12, 2018
    Posts:
    23
    Thanks a lot PanthenEye. It's great to know how visual scripting supposed to be used.

    And thanks a bunch for the link. The targeted defined events will probably help me out.

    Cheers!
     
  4. PanthenEye

    PanthenEye

    Joined:
    Oct 14, 2013
    Posts:
    1,791
    Personally, in a previous project that used Bolt (now called UnityVS) I mixed and matched graphs with C# by calling methods from C# singletons in those graphs.

    In Project Settings/Visual Scripting you can expose C# script public members to UnityVS via reflection. UnityVS then automatically generates reflected nodes for your public C# script variables, properties and methods as long as they're public. Any graph then can access a C# singleton instance from anywhere.
     
  5. Maskedchamo

    Maskedchamo

    Joined:
    Mar 12, 2018
    Posts:
    23
    Oh that's great! That sounds like a really good way to mix and get the best of both worlds.

    Thanks again for the tip.
     
  6. huulong

    huulong

    Joined:
    Jul 1, 2013
    Posts:
    223
    Great! Looks like scripts in the global assembly (where MonoBehaviours are added by default in the project) are automatically parsed, no need to add them to the list of Type Options!

    I just had to click Regenerate Units (since I've added methods since enabling Visual Scripting and generating UnitOptions.db for the first time), and the latest properties and methods appeared!

    So I used the nodes: MySingleton - Get Instance > MySingleton - MyMethod and it worked.
     
  7. Nianyi_Wang

    Nianyi_Wang

    Joined:
    Sep 9, 2021
    Posts:
    17
    Sorry for the late bump. I just wanna share my solution and maybe it'd help other late-comers.

    I'm used to write a helper class derived from ScriptableObject.
    It should be serving as a hub of public interfaces.
    You create an instance of this type as asset, then you can call any of its public methods from any event slot, whether in-scene or off-scene.
     
    Last edited: Apr 9, 2023
    Maskedchamo likes this.