private void OnTriggerEnter(Collider other)
{
var player = other.GetComponent<Player>();
if (player)
{
EventBus.Trigger(EventNames.MyCustomEvent, gameObject, 2);
}
}
using Unity.VisualScripting;
using UnityEngine;
//Registering a string name for your custom event to hook it to an event. You can save this class in a separated file and add multiple events to it as public static strings.
public static class EventNames
{
public static string MyCustomEvent = "MyCustomEvent";
}
[UnitTitle("On my Custom Event")]//Custom EventUnit to receive the event. Adding On to the unit title as an event naming convention.
[UnitCategory("Events\\MyEvents")]//Setting the path to find the unit in the fuzzy finder in Events > My Events.
public class MyCustomEvent : EventUnit<int>
{
[DoNotSerialize]// No need to serialize ports.
public ValueOutput result { get; private set; }// The event output data to return when the event is triggered.
protected override bool register => true;
// Adding an EventHook with the name of the event to the list of visual scripting events.
public override EventHook GetHook(GraphReference reference)
{
return new EventHook(EventNames.MyCustomEvent);
}
protected override void Definition()
{
base.Definition();
// Setting the value on our port.
result = ValueOutput<int>(nameof(result));
}
// Setting the value on our port.
protected override void AssignArguments(Flow flow, int data)
{
flow.SetValue(result, data);
}
}
Expected result: Trigger with second param (gameObject) called event only in gameObject script machine Have result: Trigger not called MyCustomEvent if I set Target param.
EventBus.Trigger without second param works fine but Globally
I’m pretty sure you should be switching EventUnit to GameObjectEventUnit. Not all event types expect a target. You can make events that use totally different information to determine how and when they get triggered.
Then you should add the base of AssignArguments to ensure the game object is being assigned.
I don’t suppose either of you would like to drop a sample custom GameObjectEventUnit into the thread here?
In particular I’m not sure what MessageListenerType should be?
I’m trying to build a CustomEventNode but as per the OP, the event is never being recognised, and it looks like GetHook is never called to register the node.
I make custom VS event to Take damage from Player. For this you need Player, PlayerMessageListener, TakeDamageEvent classes
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
public class Player : MonoBehaviour
{
// Start is called before the first frame update
IEnumerator Start()
{
while (true)
{
yield return new WaitForSeconds(2);
TakeDamage(10);
}
}
public void TakeDamage(float amount)
{
EventBus.Trigger(EventNames.TakeDamageEvent, gameObject, amount);
}
}
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
[UnityEngine.AddComponentMenu("")]
public class PlayerMessageListener : MessageListener
{
}
using System;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
public static class EventNames
{
public static string TakeDamageEvent = "TakeDamageEvent";
}
[UnitTitle("Take Damage Event")]//Custom EventUnit to receive the event. Adding On to the unit title as an event naming convention.
[UnitCategory("Events\\MyEvents")]//Setting the path to find the unit in the fuzzy finder in Events > My Events.
public class TakeDamageEvent : GameObjectEventUnit<float>
{
public override Type MessageListenerType => typeof(PlayerMessageListener);
protected override string hookName => EventNames.TakeDamageEvent;
[DoNotSerialize]// No need to serialize ports.
public ValueOutput result { get; private set; }// The Event output data to return when the Event is triggered.
protected override void Definition()
{
base.Definition();
// Setting the value on our port.
result = ValueOutput<float>(nameof(result));
}
protected override void AssignArguments(Flow flow, float args)
{
base.AssignArguments(flow, args);
flow.SetValue(result, args);
}
}
Only Player component and Script Machine added to the same GameObject
i try to call my custom event from my C# script but i get syntax Error
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
public class CodeTriggerCustomEvent : MonoBehaviour
{
void Update()
{
if (Input.anyKeyDown)
{
//Trigger the previously created Custom Scripting Event MyCustomEvent with the integer value 2.
EventBus.Trigger(EventNames.MyCustomEvent, 2);
}
}