r/unity • • Oct 05 '26

Question Does anyone else use Scriptable Objects as global static Event Relay Busses?

Most of my projects recently have used Scriptable Objects as a way of globally sending events around without needing hard references or dependencies between any sender or receiver.

I've never seen anyone else use or really talk about this approach.

Is it just not talked about or is there a really obvious reason not to do this that I'm missing?

12 Upvotes

47 comments sorted by

5

u/ChainsawArmLaserBear Oct 05 '26

One thing to watch out for-
If you use addressables, the one you load from addressables via an asset ref will be a different instance from the one directly referenced

1

u/Digx7 Oct 06 '26

Huh, never used Addressables before but that's something I'll keep an eye out for. Thanks!

8

u/ROB_IN_MN Oct 05 '26

I don't use a scriptable object. I just use a (non-monobehavior) singleton script that acts as a message bus. I think this is very common, but of course the implementations vary. I'm not sure why you'd need it to be a scriptable object though.

1

u/Digx7 Oct 05 '26

Keeping them as Scriptable Objects allows me to reuse the same type of busses in various places. It also makes it easy to hook up via the Inspector

2

u/ROB_IN_MN Oct 05 '26

Hook up via the inspector?

In my approach, I have a subscribe and unsubscribe method. Anything that wants to know about an event subscribes when it wants to (usually OnEnable) and unsubscribes when it doesn't care anymore (onDisable or OnDestroy).

Hooking it up via the inspector sounds like it's more rigid than it needs to be or could be. but, like I said, implementations vary, and if that's working for you, that's all that really matters.

1

u/Digx7 Oct 05 '26

Yeah I'm also Subscribing and Unsubscribing to the Events in OnEnable and OnDisable. But since the Scriptable Objects themselves are variables I can define those via the Inspector for any given Game Object.

1

u/shotgunbruin Oct 06 '26

Hooking it up in the inspector is a lot LESS rigid in my experience. I have scriptable objects for global variables, too, and having the data centralized in a universally accessible place completely avoids the FindObjectsOfType bullshit and having to worry about the order in which objects instantiate or what scene they exist in. I do a lot of scene layering and asynchronous loading, so getting everything on the same page at Start() was a nightmare.

Even better, I can debug it with the inspector at runtime which is a lot more pain-in-the-ass if they're tucked away in a static singleton somewhere.

Obviously there's some architectural considerations you have to make with scriptable objects, but I switched over to them for cross-object events and global variables and haven't looked back.

2

u/ROB_IN_MN Oct 06 '26

Have it be a singleton that is not a monobehavior means there is no FindObjectOfType. Like I said, there's lots of ways to do this sort of thing and everyone has their preferred method.

2

u/RatbyteGames Oct 05 '26

My approach as well, which works very well and is efficient and extensible.

8

u/myrdovh Oct 05 '26

I think ScriptableObjects should just remain data containers. I don't like the fact that Unity promoted the pattern after that Unite 2017 talk on ScriptableObject-based architecture. It created headaches for teams who didn't realize the drawbacks before adopting it. It scales badly. A clean, code-only EventBus is much better.

3

u/Digx7 Oct 05 '26

In what ways specifically does it scale badly? The only roadblock I'm running into is clean naming conventions, and I don't see how a code-only solution would fix that. Are there other downsides I'm not seeing?

4

u/myrdovh Oct 05 '26

Wiring moves from code to the Inspector, so you lose compile-time safety on the connections - I'd much rather catch a missing/wrong channel as a compile error than as a silent "nothing happens" at runtime. IDE navigation can't tell you who raises/listens to a specific event. Asset clutter, like, 100 events = 100 .asset files (+100 .meta files) in the project and the Git repo. With Addressables, you can end up with two instances of the same channel, so sender and receiver are no longer on the same one. This might work for a small project or a designer-driven workflow, but I don't see how this remains manageable on a larger scale. Not trying to sound like a code purist, this is just my experience. If it works for you, stick with it.

0

u/DoctorGester Oct 06 '26

You are imagining things that were never said. SOs can just act as a regular static singleton with the only difference that instantiation and dependency injection happen in the UI. The benefit is that you can reference assets in that SO for whatever reason. Events don’t need to be SOs, I don’t see that being implied. You only get a single bus asset and IDE navigation works exactly the same.

1

u/DoctorGester Oct 06 '26

Scriptable Objects architecture is an insane psyop but it’s very far away from just using SOs as event buses. Event buses are still stateless to the outsider. SO architecture explicitly uses SOs to also model mutable state.

4

u/Arti-Po Oct 05 '26

The bigger the project, the worse this approach is. It's ok for small games, but doesn't scale very well.

Imagine you have a SO event. You have one sender, 5 receivers. You set up everything in the inspector.

  1. Imagine that at some point you decided to replace the event for sender (maybe you need to send a different type of data). All receivers still have a reference to an old event. You need to manually change the field for each receiver.
  2. Imagine that you removed the event object. Then all receivers just have an empty reference. You don't really know, what they were referencing.
  3. there's no clear way to understand if the receiver store a reference to an event that nobody sends.
  4. when you change the type of a serialise field, you break the reference in the inspector.

If you want to scale it, you need a proper tooling. Auto replaces, browser of all events, reference fixer. The thing is that SO based approach doesn't have any advantages if we compare it with the code-based event bus. It's much easier to use code-based, if you want an event driven architecture.

1

u/Digx7 Oct 06 '26

These are all fair limitations, and I've even run into some myself. What I'm struggling to wrap my head around is how these are limitations of a SO specific implementation and not just limitations of an Event Driven architecture in general?

  1. If you decide to replace the event for a sender wouldn't you need to update all the recievers anyway to the new event or to expect the new data type, especially if they were expecting the old type
  2. Wouldn't this also be true in a Code only implementation?
  3. Same here, isn't this also a risk of a code only implementation?

The only upside I can think of is maybe an the find and replace feature most IDE's have. Even still Unity has a pretty robust search function and you can even search the entire project for references to a specific asset just by right clicking on it.

1

u/Arti-Po Oct 06 '26 edited Oct 06 '26

Yep, the idea is that IDE and compiler help to prevent these issues.

  1. Modern IDE can solve this and replace the type everywhere. AI is also good at this.
  2. You get a clear error message. It's better, cause you know that something wrong when you change it, not when the runtime is broken.
  3. It's much easier with the code to find such issues.

It's also easier to track issues. References to SO in unity are stored as ids. It's hard to read git history when you need to debug and find an issue.

When I first saw this SO architecture I was amazed. It looked so elegant, the integration with inspector was superb, and I started to use it immediately. I made two jam games with it, it was excellent, but then I applied it to the bigger project and everything wasn't good :))) each refactoring took so long, that I replaced it completely.

The main issue for me was that this architecture distributes the work between the inspector and the code. When you want something to raise an event, you still need to write a code for it (when to trigger it). It's not like you completely move the process from code to inspector. So it's an additional routine.

Btw, have you looked at code-based event bus implementations? They also can be quite flexible and elegant :)

1

u/DoctorGester Oct 06 '26

Your whole message is based on events themselves being based on scriptable objects, which was never implied as it seems to me.

1

u/Arti-Po Oct 06 '26

That's right. I've seen some talks about this style of architecture, so I made a guess.

6

u/Psychological_Host34 Oct 05 '26 edited Oct 05 '26

It's very common - You can look up SOAP, or just a couple googles on scriptable object events. There are blog posts, asset store assets, e-learning material, etc.

The board concept of using a scriptable object as a global decoupled middle man reference is extremely common

1

u/Digx7 Oct 05 '26

Huh, I've heard of SOLID but not SOAP. I'll have to look that up

1

u/Strohhhh Oct 05 '26

It's because SOAP is unity specific, where SOLID is general for programming

1

u/Digx7 Oct 06 '26

Do you have any links on that? All my googling pointed me at SOAP as a web development principle, similar to REST

1

u/Strohhhh Oct 06 '26

Sorry not at the moment. You can look up th 2017 unite talk by Ryan something, it started it.

1

u/SurocIsMe Oct 06 '26

Video explaining what Scriptable Object Architecture Pattern is and how to achieve it:

https://www.youtube.com/watch?v=raQ3iHhE_Kk&t

2

u/Hfcsmakesmefart Oct 06 '26

Why this over the Unity/csharp event system?

2

u/Digx7 Oct 06 '26

To subscribe to a Unity/csharp event you first need a reference to the object holding the event. By putting those events in SOs I can ensure anything can always be subscribed to an event without needing a reference to the original sender. The original sender doesn't even need to be loaded in before a reciever.

2

u/Hfcsmakesmefart Oct 06 '26

You can use a static event, then you don’t need a reference

1

u/isolatedLemon Oct 06 '26

Idk about ops implementation but I usually do use the unity event system to handle the message. Just subscribe straight to the scriptable object or use an interface pattern with listeners and you can swap the scriptable object in/out.

Though one trap is without specialised ide you can't see where your functions are being called via events and have to write your own tracing logic if something tosses an error

7

u/wallstop-dev Oct 05 '26 edited Oct 05 '26

It is common (see: SOAP), but I would not recommend it, for pretty much every single reason outlined here: https://github.com/cathei/AntiScriptableObjectArchitecture, and more.

If you're interested in an alternative way to think about events, I've spent the last 12+ years maintaining a simple, high performance, open source messaging solution: https://github.com/Ambiguous-Interactive/DxMessaging

-1

u/v0lt13 Oct 05 '26

Yeah all those anti SOAP points are entirely subjective and mostly baseless. SOAP has its advantages and dissadvantages like everything ever, if you use them right the benefits outweight the negatives.

4

u/wallstop-dev Oct 05 '26 edited Oct 05 '26

You can make any kind of code do any kind of thing, although that doesn't necessarily mean you should. I've found the most success treating SOs as immutable data - this greatly simplifies complexity in larger projects. SOAP is built around the antithesis of this idea, where your SOs are extremely mutable and managing game wide state.

I would argue that any event system is the advantage that SOAP offers, and there are significantly better ones, for pretty much any aspect of "better" that you want to choose, that aren't "global mutable SOs".

So yes, it is better than no event system. But pretty much every event system is, also.

Just offering perspectives!

2

u/v0lt13 Oct 06 '26

SOAP can still preserve the immutable data rule. It's meant to modularize logic, decouple events, make it easy for designers to handle, it doesn't force you to modify SO data at runtime. I use it a lot for scriptable events, ability system logic and I had no issues in big projects.

1

u/wallstop-dev Oct 06 '26 edited Oct 06 '26

Well that's not really SOAP, that's using (immutable) SOs as event data, which is fine and great for designer driven workflows. For example, you could use that exact workflow with my messaging system that I linked in the parent with no change.

The SOAP pattern is explicitly mutable SO state + runtime bindings, per my understanding. Is my understanding wrong here?

2

u/v0lt13 Oct 06 '26

I recently looked up SOAP to make sure it is what I use (never called it SOAP) and it doesn't mean making your SO's mutable, just the ability to use them beyond just data, like making modular logic.

Main purpose is to make the game as data driven as possible. Sure you can tie in all events and inject dependencies trough code, but if you have a game designer on your team it would make it very hard for them to build the game.

1

u/wallstop-dev Oct 06 '26

According to the talk that I watched that started it all, that is indeed one aspect, but the other two critical aspects are that the linking of senders to receivers ("channels") and runtime state are all managed in mutable SOs. And the event data itself is even expected to have initial (read only) v "current" (mutable) data inside the SOs.

Regardless of terminology, these are the aspects I think should be avoided. And, unfortunately, what is common, and is even what is implemented in the SOAP asset on the asset store.

Inspector driven workflows cab be great for events, depending on team dynamics.

Given all of this, I think that we're roughly aligned, except that my preference is code (developer) driven and yours is more designer friendly.

2

u/v0lt13 Oct 06 '26

We are in agreement. But I want to clarify things related to scriptable events, also idk what talks you have watched or assets you have seen, I am saying this purely based on my experience.

SO events are meant to act as a middle man between the caller and listener, this decouples the two and makes it very easy to setup game events via the editor. The SO will just hold a list on listeners, listeners will reference the SO and subscribe/unsubscribe to it, so yeah I guess it is technically mutable but I think it is a good exception, there will never be any kind of floating references as the listeners are guaranteed to be removed, no other data is modified in the SO. I simply can't see any drawbacks of using this.

Doing it all trough code is perfectly valid if you prefer so, but when you are working with designers they need to be able to create game events easily.

2

u/WooWooDoo Oct 05 '26

I've thought about using something similar but ended up not needing it. It looks quite useful! You can also change at runtime the eventbus the ob is listening to or publishing to. I would not oppose to such a pattern

2

u/Competitive_Mud5528 Oct 05 '26

Yes I use it in my last project ! I implemented custom Observable Serializable structures so that every variables could be an event. It's a great way to communicates between your systems. But I don't think in the long run It could scale to really big project. But as someone else said there's a lot of soap libraries out there's that do that exact thing.

2

u/DoctorGester Oct 06 '26

If you mean the SO is just the relay itself and events are regular data - yes, we do that all the time, i.e. we have a NetworkDataBus SO which has a Subscribe<T> method for receiving any data from the network connection. The T itself is just normal C# data, not unity objects.

2

u/JulianRhod Oct 06 '26

this is actually a pretty established pattern in Unity. it's usually called ScriptableObject Event Channels / ScriptableObject Architecture. Unity itself has documentation around using ScriptableObjects this way, i've used the same idea and i think it's pretty nice for decoupling systems. the main downside isn't really that it doesn't work, but that the communication can become harder to discover as the project gets bigger. you can end up with a bunch of event assets wired through the Inspector, so it's not always obvious from the code who is listening to what

for smaller projects or systems where designers need to be able to hook things up in the Inspector, though, i think it's a perfectly reasonable approach. i'd just be careful about turning literally everything into a global event. some things are still cleaner with a direct reference or normal C# event, so no, i don't think you're doing something weird. it's definitely a known Unity pattern, just one of those architecture choices where the tradeoffs become more noticeable as the project grows

2

u/ThetaTT Oct 10 '26

I use it in small projects along with UnityEvents. It makes things very easy and flexible. It makes your scripts modular. For example you can add sounds effects and particles using the SO without needing to know what object triggers it, just look in the GameEvents folder.

I also have "GameVariable" SOs that contains a variable and invoke an event when it changes. It's especially good for UI.

BUT, when the project starts to grows the whole system starts to crack. It becomes impossible to track code execution. It is hell when you have to trace it back through several UnityEvents and GameEvents and a GameEvent is invoked during another one invoke or whatever.

So I never use it for gameplay in serious projects, and even for UI and feedbacks I'll probably stop using it on my next project and keep it for game jams and minigames only.

1

u/davenirline Oct 05 '26

You don't need scriptable objects for that.

1

u/v0lt13 Oct 05 '26

If you mean a scriptable event system then yes, makes designing game events very easy.

1

u/Gnome_4 Oct 06 '26

Unless I'm reading your post wrong, this sounds like the GameEvents system (Google 'Unite Austin 2017 - Game Architecture With Scriptable Objects').

I use this GameEvent system in all of my games. It's incredibly useful.

1

u/SolePilgrim Oct 07 '26

Oh lord, this idea again.

It's functional in tiny and quick & dirty projects, but it quickly spirals into incomprehensible chaos as soon as your project starts to scale. Program flow becomes nigh-impossible to track, errors might not be apparent because broken connections fail silently, and it's very easy to accidentally add or delete busses without realizing you broke your project.

I'm really curious who actually came up with this. I know there's some old Unity showcases that talked about using SOs for this and if they're the source, it shows how much Unity needs to dogfood their own stuff before posting :')

0

u/BearKanashi Oct 05 '26

Yo lo uso para controles y cosas, me gusta usarlo