Search Unity

Question Is it possible to "extend" graphs through prefab instances in some way?

Discussion in 'Visual Scripting' started by Andrew900460, Sep 19, 2021.

  1. Andrew900460

    Andrew900460

    Joined:
    Aug 9, 2017
    Posts:
    39
    I've started messing with the bolt graphs recently because I discovered they are in the URP project template.

    The one thing that I found very impressive is that it automatically provides access to individual C# Api's through each node. Like functions, variables, various data types. etc
    So I naturally assumed that Bolt was designed with the intent that anything you can do in C#, you can do in Bolt.

    I still think I will use C# for lower-level systems that I will need to code. But Bolt looks like an amazing solution for high-level things, or creating some very basic/specific control logic that you don't want to have to make another Mono Class for.

    Actual question starts here

    For example, buttons.

    Say I want to use Bolt to develop some shared logic for ALL buttons in my game. Buttons are "open-ended" of course, and can be made to do whatever you want. But I do want ALL buttons to share a common behavior. Like playing a click sound.

    My initial idea was that I could make a Button Prefab that would hold an Audio Source, and a Script Machine component. Inside the Script Machine, I will have some nodes set up for playing a click sound when the button component fires it's OnButtonClickEvent. But after all that "common logic" happens, I want to execute some other logic that could be different among each button.

    I thought that I could just edit the prefab instances in the scene to extend the flow graph, but it says I can't and shouldn't use embedded graphs on prefab instances. So that apparently is a dead end.

    I know there are other ways I can go about doing this, but I was curious if there was a supported way to go about this sort of mechanic. Where I can take an existing graph and "extend" it with more logic. Or chain different graphs? Maybe I just need to learn more about the system.

    Alternatively, I can make a Graph Asset which has all the common button logic inside of it. And then maybe treat it like a super graph, but also have any other logic be completely separate? Idk, that sentence didn't make complete sense.
    It's a bit disappointing that embedded graphs can't be used in prefabs, it makes them less versatile.
     
  2. MasterSubby

    MasterSubby

    Joined:
    Sep 5, 2012
    Posts:
    252
    Your best bet is having a secondary script graph asset for each button extension. In your main button graph trigger another custom event for anything that extends it at that moment. Only use that name for the "override" functionality. If it's there, it will trigger that at the moment you need it to.

    Benefit is it's totally anonymous. So you can swap that for anything using the event without assigning anything or doing anything extra. Just have an extra graph with that event on the same object, or with the same target.
     
  3. Andrew900460

    Andrew900460

    Joined:
    Aug 9, 2017
    Posts:
    39
    ok, that makes sense.

    Before reading this, I did make it so I had 2 separate "Script Machine" components on the button, one which I would not change, for each button instance, and holds the "shared button logic". And the other component would do whatever I needed it to do. And each one just listened for the "OnButtonPress" to be invoked. So I don't know if things happened in a particular order, but it works.

    But to be clear, what you are saying is that I would need 2 Script Machine components on the button, and they would both use Graph Assets, but I can of course swap the graph assets to change behavior. So I'll probably just use it that way.

    Now that I think of it, I am also wondering if there is a way to have 2 Script Machine components directly communicate with each other.. Like through a function call. Because then I could just have the one graph communicate directly. idk

    But you basically answered my question.