question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Implement Scene activation context and data passing

See original GitHub issue

Context

Thinking about usage of scenes as navigation structures, you go between scenes almost like a page router. Typically with navigation APIs, you can pass data (like a URL or route data). In Excalibur, when you call Engine.goToScene(sceneKey) it might be nice to pass data to the Scene.onActivate hook.

Proposal

We’d need to figure out a way to indicate to TypeScript how to type this but something like this:

class Level extends Scene {
  onActivate(context) {
    console.log(context.data); // "overworld"
  }
}
game.add('level', new Level());
game.goToScene('level', 'overworld');

The data passed can be arbitrary, no reason to restrict it necessarily. By default, undefined is used.

interface SceneActivationContext<TData = undefined> {
  data: TData;
  previousScene: Scene;
  engine: Engine;
}

Maybe some type helpers on Scene?

class Level extends Scene<string> {
  onActivate(context /* type: SceneActivationContext<string> */) {
    console.log(context.data); // "overworld"
  }
}

Or can we override with optional generic?

class Scene {
  // ...
  public onActivate<TData = undefined>(context: SceneActivationContext<TData>) {
    // override me
  }
}

class Level extends Scene {
  onActivate<string>(context) {
    console.log(context.data);
                        ^ string
  }

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:4
  • Comments:9 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
mattjenningscommented, Jul 1, 2022

@eonarheim oops, it seems the types are a bit off.

CleanShot 2022-07-01 at 10 55 56@2x

This is because the class methods take generics, i.e onActivate<TData = undefined>. Which means the inheriting class also has to match the same definition of generics:

class MyScene extends ex.Scene {
    public onActivate<string>(ctx: ex.SceneActivationContext<string>) {
    }
}

… which is, of course, not ideal.

So a solution would be to go back to having a generic of TActivationData on the Scene, but have it default to unknown . That way it can be inferred by the given generic for ctx: ex.SceneActivationContext in onActivate, and also shouldn’t break any types that just require ex.Scene like I ran into before.

before:

export class Scene
  extends Class
  implements CanInitialize, CanActivate, CanDeactivate, CanUpdate, CanDraw {

  public onActivate<TData = undefined>(_context: SceneActivationContext<TData>): void {}
}

after:

export class Scene<TActivationData = unknown>
  extends Class
  implements CanInitialize, CanActivate<TActivationData>, CanDeactivate, CanUpdate, CanDraw {

  public onActivate(_context: SceneActivationContext<TActivationData>): void {}
}

I’ll make a PR shortly with the changes and you can review if it makes sense. Sorry for the back and forth on this!

1reaction
eonarheimcommented, Jun 30, 2022

Hmm indeed, in that case I think providing it in the onActivate might be the best path forward 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to work with multiple scenes in Unity - YouTube
Unity Tutorial: Preserving Data between Scene Loading/Switching · Game architecture with ScriptableObjects | Open Projects Devlog · How to make ...
Read more >
Specifying the scenes your app supports - Apple Developer
Specifying the scenes your app supports. Tell the system about your app's scenes, including the objects you use to manage each scene and...
Read more >
using allowSceneActivation - Unity Forum
I suspect the gameObject that is running the coroutine is being destroyed before level progress reaches %100. Use DontDestroyOnLoad on your go ...
Read more >
How to load a scene in Unity - Game Dev Beginner
Learn how to load levels, build progress bars and pass data between Scenes in my in-depth beginner's guide to loading Scenes in Unity....
Read more >
How to pass data (and references) between scenes in Unity
You only need to use this if the data to keep or pass to the next scene inherits from Object , Component or...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found