Forum rules - please read before posting.

Action: Switch Scene +1

Hello!
Is it possible to create an Action Scene/Switch next (the opposite of Scene/Switch previous)? I tried to modify the script, but my ignorance of C# is devastating.
Could someone help me with that?
Thank you,
Suely

Comments

  • edited November 2022

    To clarify: the "Switch previous" command refers to the last-visited scene - not the next scene up in Unity's build settings.

    But as for a custom Action, try this:

    using UnityEngine;
    using System.Collections.Generic;
    
    #if UNITY_EDITOR
    using UnityEditor;
    #endif
    
    namespace AC
    {
    
        [System.Serializable]
        public class ActionSceneNext : Action
        {
    
            public bool assignScreenOverlay;
    
            public bool relativePosition = false;
            public Marker relativeMarker;
            protected Marker runtimeRelativeMarker;
            public int relativeMarkerID;
            public int relativeMarkerParameterID = -1;
            public bool forceReload = false;
    
    
            public override ActionCategory Category { get { return ActionCategory.Scene; }}
            public override string Title { get { return "Switch next"; }}
            public override string Description { get { return "Moves the Player to the next scene in the build settings."; }}
            public override int NumSockets { get { return (isAssetFile) ? 1 : 0; }}
    
    
            public override void AssignValues (List<ActionParameter> parameters)
            {
                runtimeRelativeMarker = AssignFile <Marker> (parameters, relativeMarkerParameterID, relativeMarkerID, relativeMarker);
            }
    
    
            public override float Run ()
            {
                int currentSceneIndex = KickStarter.kickStarter.gameObject.scene.buildIndex;
                int runtimeSceneIndex = currentSceneIndex + 1;
    
                if (relativePosition && runtimeRelativeMarker != null)
                {
                    KickStarter.sceneChanger.SetRelativePosition (runtimeRelativeMarker);
                }
    
                KickStarter.sceneChanger.ChangeScene (runtimeSceneIndex, true, forceReload, assignScreenOverlay);
    
                return 0f;
            }
    
    
            #if UNITY_EDITOR
    
            public override void ShowGUI (List<ActionParameter> parameters)
            {
                forceReload = EditorGUILayout.ToggleLeft ("Reload even if scene is already open?", forceReload);
                relativePosition = EditorGUILayout.ToggleLeft ("Position Player relative to Marker?", relativePosition);
                if (relativePosition)
                {
                    relativeMarkerParameterID = Action.ChooseParameterGUI ("Relative Marker:", parameters, relativeMarkerParameterID, ParameterType.GameObject);
                    if (relativeMarkerParameterID >= 0)
                    {
                        relativeMarkerID = 0;
                        relativeMarker = null;
                    }
                    else
                    {
                        relativeMarker = (Marker) EditorGUILayout.ObjectField ("Relative Marker:", relativeMarker, typeof(Marker), true);
    
                        relativeMarkerID = FieldToID (relativeMarker, relativeMarkerID);
                        relativeMarker = IDToField (relativeMarker, relativeMarkerID, false);
                    }
                }
    
                assignScreenOverlay = EditorGUILayout.ToggleLeft ("Overlay current screen during switch?", assignScreenOverlay);
                if (isAssetFile)
                {
                    EditorGUILayout.HelpBox ("To perform any Actions afterwards, 'Is skippable?' must be unchecked, and 'Survive scene changes?' must be checked, in the ActionList asset's properties.", MessageType.Info);
                }
            }
    
    
            public override void AssignConstantIDs (bool saveScriptsToo, bool fromAssetFile)
            {
                AssignConstantID (relativeMarker, relativeMarkerID, relativeMarkerParameterID);
            }
    
    
            public override bool ReferencesObjectOrID (GameObject gameObject, int id)
            {
                if (relativePosition && relativeMarkerParameterID < 0)
                {
                    if (relativeMarker && relativeMarker.gameObject == gameObject) return true;
                    if (relativeMarkerID == id && id != 0) return true;
                }
                return base.ReferencesObjectOrID (gameObject, id);
            }
    
            #endif
    
    
            public override int GetNextOutputIndex ()
            {
                if (NumSockets > 0)
                {
                    return 0;
                }
                return -1;
            }
    
        }
    
    }
    
  • Thank you very much!
    Sorry to bother you again, but I tried placing it in several folders (inclusing a Custom Actions folder) and keep getting a message saying that the script "must derive fro AC's Action class in order to be available as an Action".
    Could you help me with this as well?
    Thank you
    Suely

  • The AC debug logger:

    -> AC debug logger
    UnityEngine.Debug:LogError (object,UnityEngine.Object)
    AC.ACDebug:LogError (object,UnityEngine.Object) (at Assets/AdventureCreator/Scripts/Static/ACDebug.cs:46)
    AC.AdventureCreator:AddActionsFromFolder (AC.ActionsManager,string,System.Collections.Generic.List`1<AC.ActionType>) (at Assets/AdventureCreator/Scripts/Managers/Editor/AdventureCreator.cs:590)
    AC.AdventureCreator:RefreshActions () (at Assets/AdventureCreator/Scripts/Managers/Editor/AdventureCreator.cs:495)
    AC.AdventureCreator:OnEnable () (at Assets/AdventureCreator/Scripts/Managers/Editor/AdventureCreator.cs:39)

  • Check that the script's filename matches the class, i.e. ActionSceneNext.cs, and make that the scripts inside the folder it's placed in are exclusively Action scripts.

  • Naming as your example did the trick, thank you!

  • As stated in the previous comment, the error message has been solved. However, the action does not run :(
    I have tried with normal scene names ('start' and 'second') and naming with numbers ('1' and '2'). I have also tried creating another hotspot switching to the second scene by name and coming back to the first one, just in case.
    No error message, though: it identifies the area as a hotspot (or button), but it is as if no action had been specified.

  • Try running the ActionList its placed in manually, by clicking "Run now" in its Inspector.

    Are your scenes added to the build settings, and do any messages appear in the Console?

  • nope... :(
    I have a second hotspot and button going to the next scene by name and they work. No error message either.
    I placed a build here (B&W arrow uses the next scene name, button and white arrow use the next scene script) here https://drive.google.com/drive/folders/1xAe1P7CW4zIFjrKcaxs6dkMQW1OENyDF?usp=sharing in case you want to see it, but I suspect I should try to find alternatives.

  • I must apologise - it was a typo in the script.

    I've amended it above, please give it another go.

  • It works!
    No reason to apologize: you are the best!

  • Hello, Chris. You have made this script for me a year ago, making it possible to use a "switch next" action which should make the Player to the next scene in the build settings. This has been very important for my game (and thank you again!)
    During playtests I discovered that if, for any reason, the player returns to a previously visited scene, the "switch next" action sends to the scene last visited instead of the next scene in the build settings.
    I'm not sure the explanation above is clear, so I'll explain another way:
    Player is in scene 1 -> action sends to scene 2
    Player is in scene 2 -> action sends to scene 3
    Player is in scene 3 -> action sends to scene 4
    Game throws player back to scene 1 -> action sends to scene 4

    Would it be too complicated to adjust it so
    Player is in scene 1 -> action sends to scene 2
    Player is in scene 2 -> action sends to scene 3
    Player is in scene 3 -> action sends to scene 4
    Game throws player back to scene 1 -> action sends to scene 2
    ?

    Thank you in advance

  • Game throws player back to scene 1 -> action sends to scene 2

    That's the intent behind the script - it should do that already. I can't recreate any other behaviour.

    Let's try some debugging. Above the line:

    KickStarter.sceneChanger.ChangeScene (runtimeSceneIndex, true, forceReload, assignScreenOverlay);
    

    Copy/paste the following:

    Log ("Change scene from " + currentSceneIndex + " to " + runtimeSceneIndex);
    

    What does the Console show when it takes you to the wrong scene?

  • thank you for your quick response. I am glad it happened during recorded sessions, otherwise I would think I dreamt it.
    Sorry to have bothered you unnecessarily
    [I'll have more playtests next week and will let you know if it happens again]

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.