I don't understand Scene.Name

Started by gasleet 3 posts 117 views

Reply on sbox.game
  1. #1
    gasleet 6,923
    I'm not able to fetch the name of the currently active scene.
    Just for testing purposes i created the script beneath and attached it to a gameobject.

    using Sandbox;
    public sealed class SceneManagerLogic : Component
    {
      protected override void OnStart()
      {
       GameObject.Flags |= GameObjectFlags.DontDestroyOnLoad;
      }
      protected override void OnUpdate()
      {
       if ( Input.Pressed( "Attack1" ) ) { Log.Info("Current Scene: " + this.GameObject.Scene); }
       if ( Input.Pressed( "Attack2" ) ) { Scene.LoadFromFile( "scenes/new scene.scene" ); }
      }
    }


    After loading i still get the old scene name when pressing Mouse1. I don't understand why. 😣

    I also created a gameobject in the new scene with this script attached:

    using Sandbox;
    public sealed class SpamSceneName : Component
    {
      protected override void OnUpdate()
      {
      Log.Info( $"Scene Spammer: {this.GameObject.Name} {this.GameObject.Scene.Name} {Game.ActiveScene}" );
      }
    }


    But even this just just logs "minimal" into the console, when first loading the minimal scene. It works fine when starting with the new scene. 

    I'm lost. Send help 🙏
  2. #2
    gasleet Started this 6,923
    I still don't know how to do this. ChatGPT is unable to help aswell. 
     
    Are scenes not supposed to be used like "levels"?  Should i destroy and create everything i need instead? 
    Should i just hardcode what to load without knowing which scene is active?

    If someone can explain where i'm going wrong, i would appreciate it. :)
  3. #3
    Zontax 8,642
    Scene.Source.ResourceName this should help you.  Simply Scene.Name is a property from the GameObject class because the scene inherits it, and you take the scene name from the scene resource class, it is just in the Scene.Source. There is also a second way https://sbox.game/dev/doc/scene/scenes/scene-metadata/