Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Feedback Event system and Addressables?

Discussion in 'Open Projects' started by Peter77, Dec 27, 2020.

  1. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,589
    I was reading through the Event system documentation and looked at how it's used in the project. I think it's a very interesting way to implement such system.

    Have you used this system in other projects already and what types of issues did it cause?

    I'm asking because once you start using asset bundles, you must be very careful that Unity does not duplicate assets. In terms of just assets (textures, meshes, sounds, etc) this is not a big deal, it just wastes memory.

    However, since the event channel assets drive game-logic, I assume duplicated event channel assets can be problematic.

    For example, if the same event channel asset is pulled into two asset bundles, each having a copy of that asset, one game system might listen to a completely different event channel in memory than another game system is broadcasting to.

    This probably can be fixed by storing the event channel assets in an asset bundle that is a dependency of "everything" else, but I think it's still quite error prone in this regard and getting asset dependencies in asset bundles right is a very tedious task and quite a black-box too imo. You can basically break the game-logic with a content change if suddenly event channel assets are duplicated across bundles.

    Did you ever run into this issue? What's your strategy to combat this issue?

    @cirocontinisio
     
    Last edited: Dec 28, 2020
    M_MG_S and laurentlavigne like this.
  2. _markL

    _markL

    Joined:
    Apr 27, 2019
    Posts:
    1
  3. cirocontinisio

    cirocontinisio

    Joined:
    Jun 20, 2016
    Posts:
    884
    It's a very good point, @Peter77. We haven't run into the issue yet as we haven't yet sectioned the game into bundles (except the Localisation files which do it on their own).

    What I believe is that we could have the EventChannels all in one bundle, which would of course a local one (side note, in this game all bundle will be local) so included in the build and unique. This should make it so any other bundle referencing it will find it. But I'll double check just in case.

    Thanks for bringing it up!

    @_markL you can read more about it on the wiki of the project.
     
  4. laurentlavigne

    laurentlavigne

    Joined:
    Aug 16, 2012
    Posts:
    6,221
    Why do you create a class off the UnityEvent?
    upload_2021-1-8_17-18-22.png
     
  5. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,589
    It says so in the documentation:
    upload_2021-1-9_7-30-33.png
    https://docs.unity3d.com/ScriptReference/Events.UnityEvent_1.html

    Perhaps this limitation has been lifted in Unity 2020.x with the overhauled Generics serialization, but Open-Project-1 is using Unity 2019.4 at the moment.
     
    cirocontinisio likes this.
  6. laurentlavigne

    laurentlavigne

    Joined:
    Aug 16, 2012
    Posts:
    6,221
    Last edited: Jan 9, 2021
  7. laurentlavigne

    laurentlavigne

    Joined:
    Aug 16, 2012
    Posts:
    6,221
    I'm starting to SO an existing project and might have to use addressables so thanks for bringing that up @Peter77

    What do you think of these?
    https://forum.unity.com/threads/v0-...erence-is-a-new-instance.642061/#post-4372054
    https://forum.unity.com/threads/scriptableobject-references-in-addressables.777155/
    It looks like local addressable would sort out these people's problems too.
     
    cirocontinisio likes this.
  8. cirocontinisio

    cirocontinisio

    Joined:
    Jun 20, 2016
    Posts:
    884
    You're so ahead!! :D:D
    Hopefully we'll migrate the project to 2020 LTS before it ends, and we'll use it too!

    Thanks for the links. I think a good answer lies in Bill's reply here, last paragraph:
    https://forum.unity.com/threads/v0-...erence-is-a-new-instance.642061/#post-4307485

    We will need to run a test though.
     
  9. cirocontinisio

    cirocontinisio

    Joined:
    Jun 20, 2016
    Posts:
    884
    Hey guys, I double checked with a person from Unity who deals with Addressables and AssetBundles a lot. Their reply confirmed what I was thinking:

    The Addressables system tracks dependencies of objects. If you reference a ScriptableObject from a GameObject in a scene, and you reference the same SO from another GO in another Scene, the system knows it the same SO.

    When we pack Scenes into Bundles, it's important to mark the SOs as Addressables.
    - If they are, then the system will recognise the dependency and make it so that (for instance) SceneA, contained maybe in Level1Bundle, correctly references SO1 in a certain AssetBundle we'll call SOBundle. SceneB (contained in Level2Bundle) also references SO1 in SOBundle. When requesting the load of either scene, SOBundle will be loaded too.
    - If the SOs are not explicitly marked as Addressables, they will be still pulled in these bundles because they are still a dependency. But this time they will be duplicated, giving the issue that you are describing.

    So, we just need to mark things as Addressables correctly. Makes sense?

    PS: for reference, it's the same discussion going on in issue #331. I'm going to close that soon to avoid confusion.
     
    firstuser likes this.
  10. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,589
    Do you then need to replace all hard references to event SOs with an Addressables.AssetReference? Or is it irrelevant and it's ok if some MonoBehaviours reference SOs hard and some through AssetReference?
     
  11. Neto_Kokku

    Neto_Kokku

    Joined:
    Feb 15, 2018
    Posts:
    1,751
    I think it's worth to point out that there are drawbacks to using event-based architectures:

    - It makes navigating the code harder, because you can no longer simply F12 your way into the callled function. Instead you have to search for all references to that event channel to find the methods that subscribed to it. This added friction can add up to the total work time and makes code discovery harder.
    - Dispatching an event is more expensive than calling a method due to the added layer of indirection, which can impact performance in "hot" code.

    In this particular case, problem #1 is made much, much worse because the event channel is supposed to be assigned via the inspector. Since Unity doesn't offer any way to find which objects/assets reference a specific asset, the only way to know who's listening to a channel is to place a breakpoint in the calling site and step through the dispatching. This can quickly become unmanageable if used too often.

    I worked on projects that abused Unity events and the problems were very similar, the difference being you have no idea of who is calling what.
     
    laurentlavigne likes this.
  12. laurentlavigne

    laurentlavigne

    Joined:
    Aug 16, 2012
    Posts:
    6,221
    Amen!! How do you structure your projects nowadays?

    When a dependency visualizer is added to the editor it'll be workable but for now a safety label is needed on that SO Kool-Aid bottle: "drink in moderation".

    That'll be good for some and bad for others, you def should not have both on github like you do, it's confusing.
     
  13. cirocontinisio

    cirocontinisio

    Joined:
    Jun 20, 2016
    Posts:
    884
    If we port the project to 2020 LTS we won't have another copy in 2019.4 LTS.

    What do you mean it's confusing as it is now? The Devlog branches?
     
  14. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,589
    It actually turned out to be a problem. Unity Technologies mentioned this in one of their videos, starts at 4:21.

    I wished you had credited me by name in that video ;)

     
    cirocontinisio likes this.
  15. cirocontinisio

    cirocontinisio

    Joined:
    Jun 20, 2016
    Posts:
    884
    It doesn't let me link a specific time, but yeah, we did mention that it was noted by a contributor at 4:28 - though not by name.

    But definitely thanks for bringing it up. More than an issue though it's actually an important topic when talking AssetBundles that is worth to touch on, that's why we included it in the video.

    Btw, if anyone is looking for it in the docs: https://docs.unity3d.com/Packages/c...emoryManagement.html#assetbundle-dependencies
     
    Peter77 likes this.
  16. joanpescador

    joanpescador

    Joined:
    Dec 21, 2016
    Posts:
    122
    That's cool @cirocontinisio. Anyway, what happens if you have pack a scene IN A DIFFERENT UNITY PROJECT. Loaded to a remote server and downloaded in a main different project. Is there anyway to get contact between scriptable objects with the same name? It would be great, isn't?