Forum rules - please read before posting.

On scene end event

Hi,

is there anything like "On scene end" event in AC ? I can see there is "On Start" event for scene (we can set it through AC game editor) but I cannot find anything like "On End" event in manual or forums.

I need to turn off my game UI when loading screen starts, and turn it on when loading screen ends, I dont know how to efficiently do this without "On end" event. The need for this "on end" event occured in few more cases.

So my question is; is there anything like "On end" event, and if not, what is the best way to turn on game UI when loading screen ends.

Cheers!

Joakim

Comments

  • edited December 2016
    To elaborate a bit more.

    I found out that Loading scene is, quote from AC:
    "Bypassing regular AC startup because the current scene is the 'Loading' scene."
    So on start does not get executed for loading scene.

    Now I am trying to achieve menu on/off on loading scene start/end with this script below.
    Tryed attaching it to object on Loading scene, on game engine and on separate object which has DontDestroyOnLoad, but nothing seems to work, I am trying to figure it out.
    MyOnBeforeChangeScene() and MyOnAfterChangeScene() functins do get executed but it seems they
    execute at wrong times.
    public class SceneChangeHandler : MonoBehaviour {

        public string LoadingScreenName = "LoadingScene";
        public string InGameMenuName = "InGame";   

        void Awake ()
        {
            AC.EventManager.OnBeforeChangeScene += MyOnBeforeChangeScene();
            AC.EventManager.OnAfterChangeScene += MyAfteChangeScene();
        }



        private AC.EventManager.Delegate_NoParameters MyOnBeforeChangeScene()
        {
            string currentSceneName = UnityVersionHandler.GetCurrentSceneName();
            Debug.Log(currentSceneName);
            if (currentSceneName == LoadingScreenName)
            {
                AC.Menu inGameMenu = AC.PlayerMenus.GetMenuWithName(InGameMenuName);
                inGameMenu.TurnOn();

            }
            return null;
        }

        private AC.EventManager.Delegate_NoParameters MyAfteChangeScene()
        {
            string currentSceneName = UnityVersionHandler.GetCurrentSceneName();
            Debug.Log(currentSceneName);
            if (currentSceneName == LoadingScreenName)
            {
                AC.Menu inGameMenu = AC.PlayerMenus.GetMenuWithName(InGameMenuName);
                inGameMenu.TurnOff();        
            }
            return null;
        }
    }
  • What is your InGame Menu's Appear type field set to?

    If it's set to During Gameplay, then it will abide by that rule for when its displayed - as opposed to TurnOn / TurnOff function calls.  Instead, you will have to lock the Menu to prevent it from showing when it's appear conditions are met:

    inGameMenu.isLocked = true;

    Alternatively, if your InGame Menu is rendered with AC's built-in Menu system, then it will not be displayed if the loading scene itself is not an "AC" scene, i.e. one with a GameEngine prefab.
  • Chris thanks for your answer.

    My InGame Menu's Appear type field is set to "During Gameplay".
    So if I switch inGameMenu.TurnOff() with inGameMenu.isLocked = true, and inGameMenuTurnOn() with inGameMenu.isLocked = false, my script should do the trick ?
    I did switch TurnOff() and TurnOn() with isLocked and it does not seem to work.

    Here's my modified script:

    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;
    using AC;

    public class SceneChangeHandler : MonoBehaviour
    {

    public string LoadingScreenName = "LoadingScene";
    public string InGameMenuName = "InGame";


    void Awake()
    {
    DontDestroyOnLoad(this.gameObject);
    Debug.Log("AWAKE");

    // remove listeners binded on last awake (not the best way but should work)
    AC.EventManager.OnBeforeChangeScene -= MyOnBeforeChangeScene();
    AC.EventManager.OnAfterChangeScene -= MyAfteChangeScene();
    // add new listeners
    AC.EventManager.OnBeforeChangeScene += MyOnBeforeChangeScene();
    AC.EventManager.OnAfterChangeScene += MyAfteChangeScene();

    }


    private AC.EventManager.Delegate_NoParameters MyOnBeforeChangeScene()
    {
    string currentSceneName = UnityVersionHandler.GetCurrentSceneName();
    AC.Menu inGameMenu = AC.PlayerMenus.GetMenuWithName(InGameMenuName);

    if (currentSceneName == LoadingScreenName)
    {
    inGameMenu.isLocked = true;
    Debug.Log("MENU TURN OFF");

    }
    return null;
    }

    private AC.EventManager.Delegate_NoParameters MyAfteChangeScene()
    {
    string currentSceneName = UnityVersionHandler.GetCurrentSceneName();
    AC.Menu inGameMenu = AC.PlayerMenus.GetMenuWithName(InGameMenuName);

    if (currentSceneName == LoadingScreenName)
    {
    AC.Menu inGameMenu1 = AC.PlayerMenus.GetMenuWithName(InGameMenuName);
    inGameMenu.isLocked = false;
    Debug.Log("MENU TURN ON");
    }
    return null;
    }
    }


    My script is currently attached to empty object on Loading Scene so it gets executed every time loading scene loads.
    My Debug Log look's like this:
    MENU TURN OFF
    MENU TURN ON
    MENU TURN OFF
    MENU TURN ON

    Both functions on each loading scene load get executed twice, I dont know if that is a problem.

    Joakim

  • edited January 2017
    Is this just in your loading scene, or your regular scenes as well?  You should unset your events on OnDestroy, rather than Awake:

    void OnDestrory ()
    {
      AC.EventManager.OnBeforeChangeScene -= MyOnBeforeChangeScene();
      // etc
    }

    Calling TurnOn() TurnOff() should be unnecessary - amending the isLocked bool should be enough.

    However, the logs don't show the timing of when functions are called.  If you check Load scenes asynchronously?, you'll have an option to set a delay before and after loading.  If you increase it, it should slow down the process so that you can more clearly see what's going on.  Is it the case that "MENU TURN ON" is called at the wrong time, or is it that "MENU TURN OFF" is called but has no effect.
  • I am using this just in my loading scene.
    Now I unset events in OnDestroy and I have only one call for each function, cool.

    I use asynchronous loading with delay set to max (1 second)
    But still, the timing of function calls is identical, both MyOnBeforeChangeScene and MyOnAfterChangeScene get called in exact same time (to milisecond),
    but BeforeChangeScene is called before AfterChangeScene (logicaly :) )

    Now I managed to turn off my menu, but menu stops rendering only after loading scene is done, meaning new cene is loaded. So loading scene does turn off menu but that action has no visible effect while loading scene is running, only after new scene is loaded.

    I am wondering why menu does not disappear while loading scene is still running but disappears when next scene is loaded. I see my code does get executed when loading scene starts, not when it ends.

    Also I am wondering why delay with async loading is not applyed (both functions get called in the same milisecond), I will dig into this.

    And just to note, I had to call TurnOff() function in addition to setting isLocked = true.

    So to sum up my current problem:
    In spite of setting async loading with delay MyOnBeforeChangeScene() and MyOnAfterChangeScene() functions execute in the same milisecond so I can either choose to turn on menu or to turn it off at that point in time. Also, this turn on/off action has visible effect only after loading scene finishes, meaning the new scene is loaded.



  • Update, I'v managed to get desired functionality with different approach.

    Instead of listening to BeforeSceneChange / AfterSceneChange events i just implemented the logic which checks in each scene Awake() if for this particular scene we need to turn off menu then invoke ActionListAsset which contains actions {menu unlock -> menu turn off -> menu lock}, and if we need to turn on menu instead then invoke ActionListAsset which contains acitons {menu unlock -> menu turn on}.

    So I created two ActionListAssets (not local scene ActionLists but asset files)
    one containing  actions {menu unlock -> menu turn off -> menu lock}
    the other containing  actions {menu unlock -> menu turn on}

    And I wrote new script which I attached on GameEngine prefab (applyed this to prefab so GameEngine on each scene has this script attached, so script's Awake gets executed for each scene).

    The script looks like this:

    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;
    using AC;

    public class InGameUISwitcher : MonoBehaviour
    {
        public List<string> ScenesWhereTurnOff;  
       
        [Space(10)]

        [Header("Menus to turn on/off")]
        public string InGameMenuName1 = "InGame01";
        public string InGameMenuName2 = "InGame02";

        [Space(10)]

        public ActionListAsset turnOnMenusAL;
        public ActionListAsset turnOffMenusAL;

        void Awake()
        {
            SwitchInGameUIOnOff();
        }

        private void SwitchInGameUIOnOff()
        {

            string currentSceneName = UnityVersionHandler.GetCurrentSceneName();

            AC.Menu inGameMenu1 = AC.PlayerMenus.GetMenuWithName(InGameMenuName1);
            AC.Menu inGameMenu2 = AC.PlayerMenus.GetMenuWithName(InGameMenuName2);

            if (inGameMenu1 == null || inGameMenu2 == null)
                Debug.LogError("InGameUISwitcher : Cannot find inGameMenu!");
            else if (ScenesWhereTurnOff.Contains(currentSceneName))
            {
                turnOffMenusAL.Interact();
            }
            else
            {
                turnOnMenusAL.Interact();
            }
        }

    }


    Cheers !

    Joakim
  • Just to add one detail, 

    when manipulating visibility of menu with appear type "During gameplay" through Actions it is enough to Lock/Unlock menu (as Chris said), no need to turn it off/on, moreover AC will give warning that you are turning it on/off where you just need to Lock/Unlock.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.