Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

[resolved] Get scene name

RMMV / JS

Hi,

I'm doing some work in a sprite class, which could be loaded from any scene.

I want to add some code that's only executed by certain scenes.

Is there a way to check what scene you're in?

In XP I would have done: if $scene.scene_is?(Scene_Map)

Or something like that.
 
Solution
Javascript has the "instanceof" operator and there's the SceneManager which has the current scene as _scene.
SceneManager._scene instanceof Scene_Map


You can also do
SceneManager._scene.constructor == Scene_Map
which is more MV specific, I think I'd prefer that to instanceof personally as the SceneManager already uses this with the _previousClass member (used with popping the scene stack).


I'd write any code that only executes in particular scenes to be in those actual Scene classes, rather than in the sprite class - that's the more logical place to do it, however it's probably not as flexible. As I think about it, having a sprite be aware of its scene feels like the better "cool" way to me as you wouldn't need to...
Javascript has the "instanceof" operator and there's the SceneManager which has the current scene as _scene.
SceneManager._scene instanceof Scene_Map


You can also do
SceneManager._scene.constructor == Scene_Map
which is more MV specific, I think I'd prefer that to instanceof personally as the SceneManager already uses this with the _previousClass member (used with popping the scene stack).


I'd write any code that only executes in particular scenes to be in those actual Scene classes, rather than in the sprite class - that's the more logical place to do it, however it's probably not as flexible. As I think about it, having a sprite be aware of its scene feels like the better "cool" way to me as you wouldn't need to worry about compatibility with other Plugins if the sprite is handling the logic.

If you want the scene's string name then I think there's SceneManager._scene.constructor.name - the actual object type has a .name member (all functions have .name and Javascript classes are just weird functions).
 
Solution

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top