Search Unity

Question about initialization and event handling

Discussion in 'Open Projects' started by students_t, Jul 1, 2021.

  1. students_t

    students_t

    Joined:
    Jun 11, 2021
    Posts:
    5
    Hi there. This project is a very nice resource for learning about development in Unity, so thank you to everyone who is making it happen! I am only just starting out, so I am using it to learn. I actually have two questions.

    First, what is the advantage of using an initialization scene rather than simply starting with the menu screen which is loaded by the InitializationLoader script? Is it mainly a matter of decoupling of code, since that same menu screen can be reached upon saving/quitting? Are there any disadvantages?

    Second, the project works perfectly fine when I set it up according to the instructions on the wiki, so I must be doing something wrong when I try to set the code up in a different project (I am trying to make a very simple game using the core framework—as a way to force myself to learn/understand how it works). I actually even tried to put all of the code in a sub-directory of another project, but that didn't work. The "problem" I guess is that the SceneLoader within the PersistentManagers scene is somehow not picking up the MainMenu scene load request from the InitializationLoader script (I get the Debug.LogWarning from LoadEventChannelSO script (instantiated as the LoadMenu_Channel SO) for when OnLoadingRequested == null). Is the problem perhaps somehow related to asynchronous loading or the addressables setup? I definitely need to do more looking into these things anyway.

    Let me know if you need more details, and thank you for any help!
     
    cirocontinisio likes this.
  2. cirocontinisio

    cirocontinisio

    Joined:
    Jun 20, 2016
    Posts:
    884
    Hi @students_t and welcome to Open Projects!

    Most of this is explained in this video:

    It's a short one, but let me know if you have additional questions after watching it.

    Sorry, can you explain again what do you get to? So the InitializationLoader fires the event, the SO picks it up (right?), but the SceneLoader in the managers scene doesn't respond to it?
     
    students_t likes this.
  3. students_t

    students_t

    Joined:
    Jun 11, 2021
    Posts:
    5
    Hi @cirocontinisio! Thank you for your very helpful reply!

    The video was very helpful, and I guess I wonder if maybe I am running into the problem of asset duplication that it specifically talks about (the solution in the video was to mark the implicitly referenced SO's as being addressables to avoid duplication). I tried marking all of the scenes and event channel SO's as addressable, and I tried putting all of the addressables in a single group in the addressibles group manager, but this does not seem to resolve the "A Scene loading was requested, but nobody picked it up. Check why there is no SceneLoader already present, and make sure it's listening on this Load Event channel." situation.

    I guess that's what it seems like from the warning message that is written to the console.

    It goes all the way into the LoadMainMenu() method of the InitializationLoader, wherein the InitializationLoader's _menuToLoad field is passed to the LoadMenu_Channel SO's RaiseEvent() method [would this constitute firing the event/being picked up by the SO?]. However, the value for the OnLoadingRequested field of the LoadMenu_Channel SO is apparently null when this happens. It looks like OnLoadingRequested is supposed to be set by SceneLoader within its OnEnable() method, but I guess this doesn't happen? [Would this constitute the SceneLoader not responding to the fired event?]

    Another thing I tried was setting the SO references on the SceneLoader script itself [I guess this is how the meaning of the "default" keyword is determined?] because I had previously only been setting the references on the SceneLoader script component of the SceneLoader GameObject within the PersistentManagers scene. However, I still get the same warning message.
     
  4. cirocontinisio

    cirocontinisio

    Joined:
    Jun 20, 2016
    Posts:
    884
    It's kinda hard to debug without seeing the project.
    The only thing I can say is that if you're testing things from the editor in Play mode and the "Play Mode Script" option in the Addressable Groups window is set to Fast, you're not dealing with actually packed assets so there's no way that the duplication is your issue. The issue is either in the code or in the way you reference things in the Inspector, or... (not sure)
    https://docs.unity3d.com/Packages/c...AssetsDevelopmentCycle.html#play-mode-scripts
     
    students_t likes this.
  5. students_t

    students_t

    Joined:
    Jun 11, 2021
    Posts:
    5
    EDIT: I fixed the problem I was having by upgrading to the latest version of the Addressables package, so don't worry about troubleshooting this further. If you do have any suggestions on good resources for debugging tools/practices, I'd be down to be pointed in their direction (but don't stress too much about it!).

    -----------------[end EDIT]----------------

    Totally understandable. You've been extremely helpful with what little you've had to work with. I was mainly checking to see if I was making some sort of "common/obvious" mistake that might be immediately apparent to someone with more experience.
    This is very helpful. Even if I didn't give you what was needed to find the answer, you have still been very helpful in "closing off" possible sources of the problem like this. Thanks!
    I'll keep looking. Does any video/resource come to mind to recommend for someone like me that wants to learn how to do a better job of debugging in Unity? What I do now consists of crude insertion of Debug.Log()'s into various places in the code. For example, my super crude approach to check if there were two LoadMenu_Channel SO's was to add the following to the LoadEventChannelSO class:

    Code (CSharp):
    1.  
    2.     public string myName;
    3.  
    4.    private void OnEnable() {
    5.        myName += "_" + Random.Range(10, 100);
    6.        Debug.Log("Enabling " + myName);
    7.    }
    8.  
    9.     private void OnDisable()
    10.     {
    11.        Debug.Log("Disabling " + myName);
    12.     }
    13.  
    What a noob I must be! Surely there is a better way. But if a resource doesn't spring to mind, don't stress about it. You've already helped a ton!

    (And I take from your comment that you've done all you feel in a position to do, and that's OK if so. But just the same, perhaps in case anyone else wants to chime in, maybe, I'll mention what else I've seen so far. Using my crude logging approach, I see that
    1. There's only one LoadMenu_Channel SO (I think?)
    2. The LoadMenu_Channel SO is enabled at the very start
    3. SceneLoader then becomes enabled (thereby calling SceneLoader.OnEnable())
    4. SceneLoader.OnEnable() successfully adds LoadMenu to _loadMenu.OnLoadingRequested (where _loadMenu references the LoadMenu_Channel SO) such that _loadMenu.OnLoadingRequested != null
    5. Even though LoadMenu_Channel SO should already be enabled (i.e., I have not seen its OnDisable method fire yet), for some reason the OnEnabled method fires for a second time (!?)
    6. Within the RaiseEvent method of the LoadMenu_Channel SO, OnLoadingRequested is still == null somehow
    ...again, don't feel obligated to remote debug without even having access to the code; I'm just checking in case some mistake I am making is very obvious to someone with more experience.)
     
    Last edited: Jul 4, 2021
  6. students_t

    students_t

    Joined:
    Jun 11, 2021
    Posts:
    5
    Supposing it is the way I reference things in the inspector, are there any obvious/common ways that people do that incorrectly? I establish the references in the Inspector by dragging assets from the Project window and dropping them onto the relevant fields displayed in the Inspector.

    I went ahead and tried to more definitively check if two instances of the LoadMenu_Channel SO are being created by using the code snippet provided in this stackoverflow answer. Assuming that I correctly cargo-cult'ed over the code snippet, it looks like the pointer values for InitializationLoader._menuLoadChannel and SceneLoader._loadMenu are actually different at runtime. So maybe I have referenced things wrongly in the Inspector somehow?

    EDIT:

    OK, I changed some additional things around and noticed something noteworthy. I got rid of the hacky "myName" debug approach and just started using the object.name field along with the pointer value. When I did this, I noticed that in one place the name is "LoadMenu_Channel" but in another place the name is "LoadMenu_Channel(clone)".

    EDIT2:

    Further clarification: InitializationLoader._menuLoadChannel.name and SceneLoader._loadMenu.name are both "LoadMenu_Channel" but have the objects have different memory locations. Seemingly a new instance named "LoadMenu_Channel(Clone)" is created in addition to this.
     
    Last edited: Jul 4, 2021
  7. students_t

    students_t

    Joined:
    Jun 11, 2021
    Posts:
    5
    Annnnddddd... I have it working. For some reason, the version of the Addressibles package in my library was 1.18.2; when I upgraded to 1.18.11, I stopped having problems. Thanks for the help along the way!
     
    cirocontinisio likes this.
  8. cirocontinisio

    cirocontinisio

    Joined:
    Jun 20, 2016
    Posts:
    884
    Glad you fixed it. Yeah there was a small bug in some minor version, and we also upgraded. I guess the lesson is, always keep an eye on packages :D
    Sorry it took so long to fix.