Injecting Entities into Scenes using setScene

16 views
Skip to first unread message

Rob M

unread,
Sep 8, 2014, 12:54:45 AM9/8/14
to aw...@googlegroups.com
Hi,

Currently I have a function in a level selector scene as such:

private function onPlayClick():Void {
_kernel.scenes.setScene(EScene.GAME);
}

The button that's clicked holds a property of the level selected.  I'd like to be able to inject a level Entity into the scene Game to determine various characteristics of the game (different levels), is there a way to use setScene to do this or is there another method I should be using to get to the right "level" of the game scene?

Thanks,

Rob

Rob Fell

unread,
Sep 9, 2014, 12:51:18 PM9/9/14
to aw...@googlegroups.com

Hi, there's probably a dozen ways to skin this cat?  Which is best comes down to what's in your levels, whether this is also represented in the level button, and how few dependencies you want between scenes and entities.
I'll give a few options and then if you can share more about your game design one of them might be more appropriate than others ...

1) The Session
In Session add a property called levelValue:Int.
Write to this when a LevelButton is clicked.
Then setScene to EScene.GAME as normal
Either have GameScene read session.levelValue during _init or pass it through from Factory in the createScene method (preferred).
Feed levelValue into GameScene's child Entities as needed - avoid the temptation for each Entity to read from session directly.

2) The Extended EScene
Create a new enumerator for the Scene types in your game (e.g. myproject.ESceneGame). 
Include ESceneGame.LEVEL( p_levelValue:Int )
Have each LevelButton setScene( EScene.SUB_TYPE( ESceneGame.LEVEL( levelValue ) ) )
Add extra checks into Factory's scene methods to accommodate the extended EScene (this is already in the templates with a commented switch) 
GameScene can retrieve levelValue from type using Type.enumParameters, or a switch.
Advanced - instead of passing levelValue, you can pass Level, or a level related definition (see below), but ensure there is a null check in place in Game (also required for testing)

3) EVE (Enumerated Valueobject Entity)
A generic design pattern for data driven subtyping.
Level is a three pack consisting of ELevel:Enum, LevelVo:Valueobject, Level:IEntity
The Valueobject contains read only access to data about the Level (e.g. LevelVo.name, LevelVo.isLocked).  Internally it may access, cache or share this data by more elaborate means.
LevelVo requires levelType:ELevel in the constructor, and uses this internally to determine which data to return.
Level requires levelType:ELevel in the constructor.
Level creates it's own internal LevelVo for access to the data.
Other Level related classes (e.g. LevelButton) can use the same approach, and thereby share read only data (from the Valueobject) by simple type injection.
Apply ELevel to methods 1 or 2 above.
Using ELevel is arguably safer than Int for levelValue, and encourages usage of switch (which is always a good thing imo).

4) Static Global
If Level is used across two scenes and referenced in both by class (rather than interface) then, arguably(?), setting and getting a static levelValue property inside it is an option.
Reply all
Reply to author
Forward
0 new messages