Implement Scene activation context and data passing
See original GitHub issueContext
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:
- Created 3 years ago
- Reactions:4
- Comments:9 (8 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@eonarheim oops, it seems the types are a bit off.
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:… 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 tounknown
. That way it can be inferred by the given generic forctx: ex.SceneActivationContext
inonActivate
, and also shouldn’t break any types that just requireex.Scene
like I ran into before.before:
after:
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!
Hmm indeed, in that case I think providing it in the
onActivate
might be the best path forward 👍