Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

EventBus.Trigger not called MyCustomEvent

Discussion in 'Visual Scripting' started by IAndrewNovak, Sep 26, 2021.

  1. IAndrewNovak

    IAndrewNovak

    Joined:
    Nov 29, 2013
    Posts:
    133
    VS Package 1.6.1
    Unity 2021.1.22

    Code (CSharp):
    1. private void OnTriggerEnter(Collider other)
    2. {
    3.     var player = other.GetComponent<Player>();
    4.     if (player)
    5.     {
    6.         EventBus.Trigger(EventNames.MyCustomEvent, gameObject, 2);
    7.     }
    8. }
    Code (CSharp):
    1. using Unity.VisualScripting;
    2. using UnityEngine;
    3.  
    4. //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.
    5. public static class EventNames
    6. {
    7.    public static string MyCustomEvent = "MyCustomEvent";
    8. }
    9.  
    10. [UnitTitle("On my Custom Event")]//Custom EventUnit to receive the event. Adding On to the unit title as an event naming convention.
    11. [UnitCategory("Events\\MyEvents")]//Setting the path to find the unit in the fuzzy finder in Events > My Events.
    12. public class MyCustomEvent : EventUnit<int>
    13. {
    14.    [DoNotSerialize]// No need to serialize ports.
    15.    public ValueOutput result { get; private set; }// The event output data to return when the event is triggered.
    16.    protected override bool register => true;
    17.  
    18.    // Adding an EventHook with the name of the event to the list of visual scripting events.
    19.    public override EventHook GetHook(GraphReference reference)
    20.    {
    21.        return new EventHook(EventNames.MyCustomEvent);
    22.    }
    23.    protected override void Definition()
    24.    {
    25.        base.Definition();
    26.        // Setting the value on our port.
    27.        result = ValueOutput<int>(nameof(result));
    28.    }
    29.    // Setting the value on our port.
    30.    protected override void AssignArguments(Flow flow, int data)
    31.    {
    32.        flow.SetValue(result, data);
    33.    }
    34. }


    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 :(
     
    Last edited: Sep 26, 2021
  2. IAndrewNovak

    IAndrewNovak

    Joined:
    Nov 29, 2013
    Posts:
    133
  3. IAndrewNovak

    IAndrewNovak

    Joined:
    Nov 29, 2013
    Posts:
    133
    It's the issue or am I doing something wrong?
     
  4. MasterSubby

    MasterSubby

    Joined:
    Sep 5, 2012
    Posts:
    252
    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.
     
  5. IAndrewNovak

    IAndrewNovak

    Joined:
    Nov 29, 2013
    Posts:
    133
    Thanks, Bro.

    GameObjectEventUnit is what I need.
    It is a pity that an example with its use is not described in the documentation.
     
  6. ChaosCraft

    ChaosCraft

    Joined:
    Feb 8, 2022
    Posts:
    8
    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.
     
  7. IAndrewNovak

    IAndrewNovak

    Joined:
    Nov 29, 2013
    Posts:
    133
    I make custom VS event to Take damage from Player. For this you need Player, PlayerMessageListener, TakeDamageEvent classes

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using Unity.VisualScripting;
    4. using UnityEngine;
    5.  
    6. public class Player : MonoBehaviour
    7. {
    8.     // Start is called before the first frame update
    9.     IEnumerator Start()
    10.     {
    11.         while (true)
    12.         {
    13.             yield return new WaitForSeconds(2);
    14.             TakeDamage(10);
    15.         }
    16.     }
    17.  
    18.     public void TakeDamage(float amount)
    19.     {
    20.         EventBus.Trigger(EventNames.TakeDamageEvent, gameObject, amount);
    21.     }
    22. }
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using Unity.VisualScripting;
    4. using UnityEngine;
    5.  
    6. [UnityEngine.AddComponentMenu("")]
    7. public class PlayerMessageListener : MessageListener
    8. {
    9.    
    10. }
    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using Unity.VisualScripting;
    5. using UnityEngine;
    6.  
    7. public static class EventNames
    8. {
    9.     public static string TakeDamageEvent = "TakeDamageEvent";
    10. }
    11.  
    12. [UnitTitle("Take Damage Event")]//Custom EventUnit to receive the event. Adding On to the unit title as an event naming convention.
    13. [UnitCategory("Events\\MyEvents")]//Setting the path to find the unit in the fuzzy finder in Events > My Events.
    14.  
    15. public class TakeDamageEvent : GameObjectEventUnit<float>
    16. {
    17.     public override Type MessageListenerType => typeof(PlayerMessageListener);
    18.  
    19.     protected override string hookName => EventNames.TakeDamageEvent;
    20.    
    21.     [DoNotSerialize]// No need to serialize ports.
    22.     public ValueOutput result { get; private set; }// The Event output data to return when the Event is triggered.
    23.  
    24.     protected override void Definition()
    25.     {
    26.         base.Definition();
    27.         // Setting the value on our port.
    28.         result = ValueOutput<float>(nameof(result));
    29.     }
    30.  
    31.     protected override void AssignArguments(Flow flow, float args)
    32.     {
    33.         base.AssignArguments(flow, args);
    34.         flow.SetValue(result, args);
    35.     }
    36. }
    37.  
    Only Player component and Script Machine added to the same GameObject

    My Embed Script graph


    Good luck
     
  8. VOXELIUM

    VOXELIUM

    Joined:
    Nov 24, 2022
    Posts:
    50
    i try to call my custom event from my C# script but i get syntax Error

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using Unity.VisualScripting;
    4. using UnityEngine;
    5.  
    6. public class CodeTriggerCustomEvent : MonoBehaviour
    7. {
    8.  
    9.     void Update()
    10.     {
    11.         if (Input.anyKeyDown)
    12.         {
    13.             //Trigger the previously created Custom Scripting Event MyCustomEvent with the integer value 2.
    14.             EventBus.Trigger(EventNames.MyCustomEvent, 2);
    15.         }
    16.     }
    This code from Unity documentation

    My screenshot
    error_custom_event.png
     
  9. IAndrewNovak

    IAndrewNovak

    Joined:
    Nov 29, 2013
    Posts:
    133
    What error message do you have?
     
  10. VOXELIUM

    VOXELIUM

    Joined:
    Nov 24, 2022
    Posts:
    50
    i show it on my screenshot
     
  11. IAndrewNovak

    IAndrewNovak

    Joined:
    Nov 29, 2013
    Posts:
    133
    Give me pls error message from Log window
     
    Last edited: Mar 24, 2023
  12. VOXELIUM

    VOXELIUM

    Joined:
    Nov 24, 2022
    Posts:
    50
    Error is here. This is enough to make the code not work
    Log clean.
    910B0844-68F7-4A39-A6CC-6413A198D04D.jpeg
     
  13. VOXELIUM

    VOXELIUM

    Joined:
    Nov 24, 2022
    Posts:
    50
    Sorry
    Log contains error message

    Снимок экрана 2023-03-25 в 09.30.27.png
     
  14. VOXELIUM

    VOXELIUM

    Joined:
    Nov 24, 2022
    Posts:
    50

    This code from Unity documentation
    https://docs.unity3d.com/Packages/c...reate-own-custom-event-node-trigger-code.html
     
  15. IAndrewNovak

    IAndrewNovak

    Joined:
    Nov 29, 2013
    Posts:
    133
    You are missing a close bracket here
    upload_2023-3-26_23-12-48.png
     
  16. VOXELIUM

    VOXELIUM

    Joined:
    Nov 24, 2022
    Posts:
    50
    Ok but it doesn't fix the error.
    The code still doesn't work

    Снимок экрана 2023-03-27 в 09.06.50.png
     
  17. IAndrewNovak

    IAndrewNovak

    Joined:
    Nov 29, 2013
    Posts:
    133
    It's another error. You are missing class name EventNames

    upload_2023-3-27_11-5-41.png
    You can add this class to your script or create a new script with the name and code that I show in the screen
     
  18. VOXELIUM

    VOXELIUM

    Joined:
    Nov 24, 2022
    Posts:
    50
  19. IAndrewNovak

    IAndrewNovak

    Joined:
    Nov 29, 2013
    Posts:
    133
  20. VOXELIUM

    VOXELIUM

    Joined:
    Nov 24, 2022
    Posts:
    50
    Ok, I started reading this article not at first. Thanks for helping me understand
     
  21. IAndrewNovak

    IAndrewNovak

    Joined:
    Nov 29, 2013
    Posts:
    133
    Was glad to help. GL