Search Unity

Resolved Unable to Invoke Repeating for Custom Events?

Discussion in 'Visual Scripting' started by RimTobbins, Apr 20, 2022.

  1. RimTobbins

    RimTobbins

    Joined:
    Apr 9, 2022
    Posts:
    8
    I'm working on completing the Jr Programming pathway, but I'm using Visual Scripting instead of writing the code in C# for all of the lessons. I'm currently stuck on Step 2: "Spawn the animals at timed intervals" from this tutorial for background context.

    Issue: The Invoke Repeating step doesn't seem to be working. It's not spawning any objects, and in debug I get the following message: "Trying to Invoke method: Variables.SpawnRandomAnimal couldn't be called."

    Here is what my script currently looks like for setting up a custom event called SpawnRandomAnimal and having it Invoke Repeating on Start:
    upload_2022-4-19_19-36-12.png

    Using the Update event setup shown above I was able to test and confirm that pressing "S" key triggers the event and spawns the object as intended. (I'll be deleting this key-based functionality once i have the repeater working, this is just for testing that the custom event is working).

    The Invoke Repeating step doesn't seem to be working though. It's not spawning any objects, and in debug I get the following message: Trying to Invoke method: Variables.SpawnRandomAnimal couldn't be called.

    Did I set something up wrong or miss a step for getting the Invoke Repeating node to properly spawn objects at the set intervals? Are custom events not the Visual Scripting equivalent of custom methods? I'm really new to scripting so any help or clarification is greatly appreciated!
     
  2. PanthenEye

    PanthenEye

    Joined:
    Oct 14, 2013
    Posts:
    2,079
    UVS custom event has no relation to Monobehaviour methods, that's why it's not invoking. If you want to spawn multiple things On Start (or repeat the same logic X times in a row in general), you have to use a "for loop".
     
  3. RimTobbins

    RimTobbins

    Joined:
    Apr 9, 2022
    Posts:
    8
    Thank you for responding!

    That would definitely work, but I'm trying to recreate this tutorial directly using Visual Scripting instead of the C# the tutorial was written for - it's sort of my way of learning both C# and UVS as the same time. Do you know if there is a way to do this line of code from that tutorial but using nodes instead:

    InvokeRepeating("SpawnRandomAnimal", startDelay, spawnInterval);

    So far I've been able to recreate the tutorial step for step using UVS instead of C#, and I was hoping to try to get through the entire learning pathway in UVS instead, but I can't seem to figure out this Invoke Repeating step. Thanks again!

    Edit: Am I using Custom Events wrong? My understanding is that a custom event is the UVS version of a custom method, but if there is a different way to do custom methods then I'm guessing that's what I'm doing wrong.
     
  4. PanthenEye

    PanthenEye

    Joined:
    Oct 14, 2013
    Posts:
    2,079
    UVS Custom Events are closer to regular C# events or Unity events. They're used for data transmission/communication but do not contain any logic by themselves like a method would.

    The closest thing to a method in UVS is a nested subgraph with configured inputs/outputs. A method is a re-usable piece of logic. A nested asset subgraph also fits that role.

    But you've come across a point where visual scripting does not have a direct alternative to the thing you want to use. Monobehaviour.InvokeRepeating can only invoke methods in C# scripts that inherit from Unity's Monobehaviour. Graphs are not C# scripts, they don't have a class structure and don't have a concept of a method, which is why anything Monobehaviour specific won't directly apply to UVS systems like Custom Events.

    So you either write the method for Invoke Repeating in a C# script or you use a for loop together with time related nodes that can add the necessary time-step to achieve the same exact result of Invoke Repeating. Or you can maybe look into the Cooldown node, which is unique to UVS and doesn't have a direct C# alternative.
     
    Last edited: Apr 20, 2022
  5. RimTobbins

    RimTobbins

    Joined:
    Apr 9, 2022
    Posts:
    8
    Thanks for helping me understand why it wasn't working! I knew I would eventually run into a situation where there wasn't a direct correlation between the code and the nodes, just didn't think I would hit this wall so early in the learning pathway.

    I ended up using this approach as a workaround for anyone that is trying to do something similar - I didn't add in the 2sec delay on first run that the InvokeRepeating code could have done, but otherwise it serves the same purpose and spawns a new animal every second:
    upload_2022-4-20_13-10-18.png
     
  6. PanthenEye

    PanthenEye

    Joined:
    Oct 14, 2013
    Posts:
    2,079
    That's a good workaround. If you want to optimize it a lil' bit, you can put it in Start and have the Completed flow trigger a Custom Event that Starts the timer again. You can connect multiple flow starting green events to the same input connection. This way, the logic won't check the Once node every frame and only trigger when the Timer is done and in Start. And it would also allow the addition of the initial delay if you mark Start as a coroutine in the Graph inspector and add Wait for Seconds node.
     
    Last edited: Apr 21, 2022
    RimTobbins likes this.
  7. RimTobbins

    RimTobbins

    Joined:
    Apr 9, 2022
    Posts:
    8
    Love it, will definitely do that instead. Thanks again!!
     
  8. RimTobbins

    RimTobbins

    Joined:
    Apr 9, 2022
    Posts:
    8
    InvokeRepeating came up in another tutorial I was working on, so I decided to take some time and build out a nested subgraph version of an InvokeRepeating node per PanthenEye's suggestion. Here was the result for anyone trying to do the same:

    Inside my SpawnManager visuals script::
    upload_2022-4-21_15-21-12.png

    Inside my InvokeRepeating subgraph:
    upload_2022-4-21_15-23-43.png
     

    Attached Files: