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?
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.
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.
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;
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?
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.
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.