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. Dismiss Notice

Resolved Cant get GameObjectEventUnit<T> to work

Discussion in 'Visual Scripting' started by Love_Friberger, Aug 16, 2023.

  1. Love_Friberger

    Love_Friberger

    Joined:
    Sep 5, 2017
    Posts:
    2
    Unity 2022.1.14f1
    Visual Scripting 1.7.8

    I want to trigger an event that will only fire on the script machine that's on the same game object as the triggering script. Following this thread i created the following code and graph

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Unity.VisualScripting;
    5.  
    6. public class InteractionRunGraph : Interaction
    7. {
    8.  
    9.     [SerializeField]
    10.     InteractionType interactionType = InteractionType.None;
    11.  
    12.     public override InteractionType InteractionType => interactionType;
    13.  
    14.     public override void Interact(PlayerController playerController)
    15.     {
    16.         EventBus.Trigger(EventNames.PlayerInteractionEvent, gameObject, playerController);
    17.     }
    18.  
    19. }
    Code (CSharp):
    1. using System;
    2. using Unity.VisualScripting;
    3. using UnityEngine;
    4.  
    5. [UnitTitle("On Player Interaction Event")]
    6. [UnitCategory("Events\\Custom")]
    7. public class PlayerInteractionEventNode : GameObjectEventUnit<PlayerController>
    8. {
    9.     [DoNotSerialize]
    10.     public ValueOutput result { get; private set; }
    11.  
    12.     public override Type MessageListenerType => typeof(InteractionMessageListener);
    13.  
    14.     public override EventHook GetHook(GraphReference reference)
    15.     {
    16.         return new EventHook(EventNames.PlayerInteractionEvent);
    17.     }
    18.  
    19.     protected override void Definition()
    20.     {
    21.         base.Definition();
    22.         result = ValueOutput<PlayerController>(nameof(result));
    23.     }
    24.  
    25.     protected override void AssignArguments(Flow flow, PlayerController data)
    26.     {
    27.         base.AssignArguments(flow, data);
    28.         flow.SetValue(result, data);
    29.     }
    30. }
    Code (CSharp):
    1. using Unity.VisualScripting;
    2.  
    3. [UnityEngine.AddComponentMenu("")]
    4. public class InteractionMessageListener : MessageListener
    5. {
    6. }
    Code (CSharp):
    1. public static class EventNames
    2. {
    3.     public static string PlayerInteractionEvent = "Player Interaction Event";
    4. }
    View attachment 1287882

    The InteractionRunGraph and a script machine that has the PlayerInteractionEventNode in its graph are added to the same game object and EventBus.Trigger is called, but nothing happens. Not sure how to go from here?
     
  2. gavinprior

    gavinprior

    Joined:
    Jul 27, 2020
    Posts:
    40
    You don't need a custom event for what you are trying to do (if I am reading it correctly).
    The first image shows my Game Object called "0" triggering a Unity Event on itself from the VisualScript called "WhichButton" (at the bottom of the image).

    Screenshot 2023-08-17 at 17.35.26.png

    The Unity Event that I want to action is called 'WhichItem' that is then triggered within the script (below).

    Screenshot 2023-08-17 at 17.36.20.png

    Hopefully, that makes sense(!) and is what you were wanting to do.
     
  3. Love_Friberger

    Love_Friberger

    Joined:
    Sep 5, 2017
    Posts:
    2
    While I still don't know why the my custom event didn't work, this solved my problem. Thank you!
     
  4. gavinprior

    gavinprior

    Joined:
    Jul 27, 2020
    Posts:
    40
    I would expect to have seen some error messages in the console if it didn't work. My first thoughts were that the EventNames script is wrong:

    public static string PlayerInteractionEvent = "Player Interaction Event";

    Should be:

    public static string PlayerInteractionEvent = "PlayerInteractionEventNode";

    As that script is what you are trying to call (I think)?
     
  5. S2NX7

    S2NX7

    Joined:
    Aug 17, 2021
    Posts:
    26
    Its probably because you are trying to use a message listener just put the MessageListenerType to Null