How to invoke an action/event from visual scripting

I have an EventManager monobehavior which listens for events invoked from a GameEntity monobehavior.

public class EventManager : MonoBehaviour
{


    // Start is called before the first frame update

    void OnEnable()
    {
        GameEntity.playerStateChanged += doStuff;

    }


    void doStuff()
    {

  
    }

The GameEntity action variable and invoke are defined this way:

public class GameEntity : MonoBehaviour
{
    public static event Action playerStateChanged;

    private void OnHit()
    {
   
            Game.playerData.hitpointCurrent -= 1;
            Debug.Log("Updating Player Data");
            playerStateChanged?.Invoke();
       
    }
}

This works fine but there are cases where I need to also invoke the event from a visual script component on the same object as the GameEntity.

I’ve been looking through the documentation and the fuzzy search and it isn’t clear how to do this. Any advice?

Thank you.

Bolt/UVS does not support any kind of delegates natively. So you either code a custom event node or invoke a UVS Custom Event via code instead. Custom Events API is documented in the official docs.

How do I call a method of a component from UVS?

Sorry to keep asking you questions. There seems to be so much about bolt and visual scripting that isn’t documented. I’m using the documentation here:
https://docs.unity3d.com/Packages/com.unity.visualscripting@1.6/manual/vs-developers-guide.html

Is that correct place?

I’m doing a unit graph of This > Get Component > Invoke Monobehavior. Seems to work fine.

I’m assuming the function you are invoking is just a public function that invokes your action?
Or is it the subscriber of your action that you are invoking?

Invoke Monobehaviour will wait a frame. If you have a reference to that object you can call that function directly and not wait a frame.

https://docs.unity3d.com/Packages/com.unity.visualscripting@1.6/manual/vs-events-reference.html#events-api

This is probably the easiest way of triggering logic in a graph from a C# script currently. Note that Ludiq and Bolt namespaces are migrated to Unity.VisualScripting, the docs are out of date. But the custom event API syntax should be identical.

I’ve been hours and hours trying to work out the logic of triggering a custom event from a c# script to visual scripting (the one that is shown in your link), but I haven’t succeeded so far, even with the most basic script… so that makes me think that the API syntax has changed.

this is an example script that I used:

"
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Bolt;
using Ludiq;

public class RevertChanges : MonoBehaviour
{

void start()
{
CustomEvent.Trigger(enemy, ā€œDamageā€, 30);
}
}

"

and I got my Target Game Object (Enemy) already set up with its Custom Event (Damage) and everything.

These are the errors I keep getting:
(1)

Assets\Scripts\UI\RevertChanges.cs(4,7): error CS0246: The type or namespace name ā€˜Bolt’ could not be found (are you missing a using directive or an assembly reference?)

(2)
Assets\Scripts\UI\RevertChanges.cs(5,7): error CS0246: The type or namespace name ā€˜Ludiq’ could not be found (are you missing a using directive or an assembly reference?)


Sometimes when I don’t get those errors I get the ā€œ,ā€ expected (in a line and ch: in front of the ā€œtarget game objectā€) that does not make sense, and when I arbitrarily change anything to make that error disappear I get the previous 2 errors back.

Does anyone by any chance know what on Earth is going on?

For any future wanderer, I found the solution:

The API was in fact changed (for visual scripting). The updated link is this one:
https://docs.unity3d.com/Packages/com.unity.visualscripting@1.8/manual/vs-events-reference.html#events-api

The solution in the example I used would be:
(1) to replace both (using Ludiq;, and using Bolt;) by (using Unity.VisualScripting;)
(2) make a reference to the game object in the inspector (public GameObject (name it as you want, in this case will be: public GameObject enemy;)

and now the ā€œCustomEvent.Trigger(enemy, ā€œDamageā€, 30);ā€ should trigger just fine

PS: Sorry for necro-ing the post, I was kind of desperate at this point. At least I hope this can find and help anyone who got into the same issue as me.

1 Like

I actually wrote a package for that: https://assetstore.unity.com/packages/tools/visual-scripting/bolt-unity-events-175821

Originally it was written for bolt and still depends on it. But due to the positive reviews I think my fix mentioned on the asset-page-itself solves the compiler-issues one should expect adding this to the project.