GameScene
Extends: GameEntity
A GameScene is a GameEntity that represents one distinct state of your game - a title screen, a menu, a level. Register scenes with Game.defineScene() and switch between them with Game.changeScene(). Only one GameScene is current at a time, and it gets the same per-frame hooks (onUpdate, onInput, ...) as any other entity.
Every scene change destroys all non-persistent entities and, by this class's default onChangeScene(), removes the outgoing scene's own drawables (anything added with addDrawable()/addImage()/etc. directly on the scene). Changing to the current scene again (Game.resetScene(), or changeScene() with the current scene's name, e.g. the next level of a level scene) works exactly like leaving it and entering it again.
To keep some or all of a scene's own drawables across a change:
- set
persistDrawablesAcrossSceneChange = trueto keep all of them, or - override
onChangeScene()with your own cleanup. An override replaces the default cleanup unless it callssuper.onChangeScene(newScene).
A scene is defined once and reused: leaving it (or resetting it) calls onChangeScene() then onDestroy() on it, and entering it again runs onLoad()/onCreate() again. Free whatever a visit set up in either hook, or let the scene do it: widgets added with addUi(), lights added with addLight() and assets loaded with loadSceneBitmap() (and friends) go when the game leaves the scene. Widgets added straight to game.gameUi, lights added with game.addLight() and assets loaded straight on the loader stay until you remove them (use those for things that should outlive the scene).
Properties
persistDrawablesAcrossSceneChange(boolean) — Whether this scene's own directly-added drawables survive a scene change (including onequalityOptions(BGE.SceneQualityOptions) — This scene's own render quality settings, applied once it has loaded (afteronCreate())trackedSolidWorld(BGE.SolidWorld) — A SolidWorld whose tracked bodies the game resolves every frame, right aftersceneUi(Array.<BGE.UI.UiWidget>) — Widgets added with addUi(), removed when this visit ends.sceneAssets(roAssociativeArray) — BGE.SceneAsset by key: what this visit owns (loadSceneBitmap() and friends)releasingSceneAssets(roAssociativeArray) — BGE.SceneAsset by key: the last visit's, between leaving and the next scene's onLoad(),
Constructor
new GameScene(
gameEngine: Game,
args?: roAssociativeArray,
): GameSceneParameters
gameEngine(Game)args(roAssociativeArray, optional, default: "{}")
Instance Methods
addUi(widget: BGE.UI.UiWidget): void
Adds a widget to game.gameUi for this visit only: it's removed (and its buttons unregistered from focus) when the game leaves or resets this scene, after onDestroy(). Add a widget to game.gameUi directly to keep it across scenes.
Parameters
widget(BGE.UI.UiWidget)
Returns
void
removeUi(widget: BGE.UI.UiWidget): void
Removes a widget added with addUi() from game.gameUi.
Parameters
widget(BGE.UI.UiWidget)
Returns
void
clearUi(): void
Removes every widget added with addUi(). The game calls this when the scene changes.
Returns
void
uiCount(): integer
How many widgets added with addUi() are still up.
Returns
integer
onLoad(loader: BGE.AssetLoader, args: object): void
Called when this scene is changed to, before onCreate(). Queue anything that takes time on loader - asset loads (loader.loadBitmap(), loader.load3dModel(), ...), your own BGE.LoadTasks, or a function called once per step (loader.addStep()). The engine runs them a little each frame, showing a loading screen, then calls onCreate(args). Queue nothing (the default) and onCreate() runs straight away.
The previous scene's entities are already gone. This scene doesn't get onUpdate(), onInput() etc. until onCreate() has run - nor do entities added during onLoad().
Parameters
loader(BGE.AssetLoader) — queue this scene's loading work hereargs(object) — the args passed tochangeScene()
Returns
void
loadSceneBitmap(
loader: BGE.AssetLoader,
bitmapName: string,
pathOrUrl: string,
options?: BGE.BitmapLoadOptions,
shared?: boolean,
): BGE.LoadTask
Queues a bitmap this scene owns: it's unloaded when the game leaves the scene, unless the next scene (this one again, on a reset or the next level) asks for it too, in which case it's kept for that scene without loading it again. A bitmap that's already loaded by anything else isn't queued, and stays theirs. Pass shared = true for one other scenes use too: it's loaded unless it's already loaded, and never unloaded by a scene.
Parameters
loader(BGE.AssetLoader) — the loaderonLoad()was givenbitmapName(string) — the name to get it by later (game.getBitmap())pathOrUrl(string) — local file path or URLoptions(BGE.BitmapLoadOptions, optional, default: "{}") — asAssetLoader.loadBitmap()shared(boolean, optional, default: false) — keep it after the scene ends
Returns
BGE.LoadTask— the queued task, or invalid if it didn't need loading
Example
override sub onLoad(loader as BGE.AssetLoader, args as roAssociativeArray)
m.loadSceneBitmap(loader, "townGround", "pkg:/sprites/town_ground.webp")
m.loadSceneBitmap(loader, "nightSky", "pkg:/sprites/night_sky.webp", {}, true)
end subloadSceneModel(
loader: BGE.AssetLoader,
modelName: string,
pathOrUrl: string,
options?: BGE.Model3dLoadOptions,
downloadOptions?: BGE.DownloadOptions,
shared?: boolean,
): BGE.LoadTask
Queues a .obj or .stl model this scene owns. See loadSceneBitmap() for when it's loaded and freed.
Parameters
loader(BGE.AssetLoader) — the loaderonLoad()was givenmodelName(string) — the name to get it by later (game.get3dModel())pathOrUrl(string) — local file path or URLoptions(BGE.Model3dLoadOptions, optional, default: "{}") — asAssetLoader.load3dModel()downloadOptions(BGE.DownloadOptions, optional, default: "{}") — asAssetLoader.load3dModel()shared(boolean, optional, default: false) — keep it after the scene ends
Returns
BGE.LoadTask— the queued task, or invalid if it didn't need loading
loadSceneSpriteAtlas(
loader: BGE.AssetLoader,
atlasName: string,
pathOrUrl: string,
options?: BGE.DownloadOptions,
shared?: boolean,
): BGE.LoadTask
Queues a packed sprite atlas (and its sheets) this scene owns. See loadSceneBitmap() for when it's loaded and freed.
Parameters
loader(BGE.AssetLoader) — the loaderonLoad()was givenatlasName(string) — the name to get it by later (game.getSpriteAtlasFrames())pathOrUrl(string) — the.json's local file path or URLoptions(BGE.DownloadOptions, optional, default: "{}") — asAssetLoader.loadSpriteAtlas()shared(boolean, optional, default: false) — keep it after the scene ends
Returns
BGE.LoadTask— the queued task, or invalid if it didn't need loading
loadSceneTiledMap(
loader: BGE.AssetLoader,
mapName: string,
pathOrUrl: string,
options?: BGE.DownloadOptions,
shared?: boolean,
): BGE.LoadTask
Queues a Tiled map (and its tileset images) this scene owns. See loadSceneBitmap() for when it's loaded and freed.
Parameters
loader(BGE.AssetLoader) — the loaderonLoad()was givenmapName(string) — the name to get it by later (game.getTiledMap())pathOrUrl(string) — local file path or URL of the mapoptions(BGE.DownloadOptions, optional, default: "{}") — asAssetLoader.loadTiledMap()shared(boolean, optional, default: false) — keep it after the scene ends
Returns
BGE.LoadTask— the queued task, or invalid if it didn't need loading
ownsSceneAsset(kind: BGE.SceneAssetKind, name: string): boolean
Whether this visit owns an asset (loaded with loadSceneBitmap() and friends).
Parameters
kind(BGE.SceneAssetKind)name(string)
Returns
boolean
getSceneAssets(): Array.<BGE.SceneAsset>
The assets this visit owns.
Returns
Array.<BGE.SceneAsset>
unloadSceneAssets(): void
Unloads every asset this visit owns now, e.g. to free memory early. The game frees them anyway when it leaves the scene.
Returns
void
isSceneAssetLoaded(
kind: BGE.SceneAssetKind,
name: string,
): boolean
Parameters
kind(BGE.SceneAssetKind)name(string)
Returns
boolean
unloadSceneAsset(asset: BGE.SceneAsset): void
Parameters
asset(BGE.SceneAsset)
Returns
void
beginReleasingSceneAssets(): void
Called by the game once this scene has been left (after onDestroy()): what this visit owned waits for the next scene's onLoad(), which can take it over.
Returns
void
unloadReleasedSceneAssets(): void
Called by the game right after the next scene's onLoad(): frees what this scene owned last visit that the next scene didn't take over.
Returns
void
handOverSceneAsset(key: string): boolean
Gives up an asset this scene owned last visit, so the next scene can have it. Used by the next scene's loadSceneBitmap() and friends.
Parameters
key(string)
Returns
boolean— true if this scene was releasing it
getLoadingScreen(): BGE.LoadingScreen
The loading screen to show while this scene loads. Return invalid (the default) to use game.loadingScreen, or the engine's default if that isn't set.
Returns
onPostUpdate(deltaTime: float): void
Called once per frame on the current scene after every entity has updated and moved (and tweens have ticked), before collisions and drawing. For work that needs everyone's final positions and shouldn't depend on entity update order, e.g. BGE.separateCircles().
Parameters
deltaTime(float) — seconds since last frame, as onUpdate() gets
Returns
void
resolveTrackedSolids(): void
Called by the game right after onPostUpdate(). By default resolves trackedSolidWorld's tracked bodies, if it is set.
Returns
void
onChangeScene(newScene: GameScene): void
Parameters
newScene(GameScene)
Returns
void