MultisceneTest with multiple files
mrcoxall last edited by
I am looking to use the code for creating multiple scenes ( from here https://gist.github.com/omz/4059061 ) but I would like to seperate the code over several files. I want the make sure it will work over several files, so I can have students working on different scenes and then bring everything together.
What I would like to do is remove the 3 scene classes from main and move them to the 3 scene files. Not sure what I am doing wrong to get it working. I have tried various "import" statements but I can not get it working.
JonB last edited by JonB
The way you are headed, scene1.py needs to import from scene and scene2.
scene2 needs to import from scene and scene3.
scene3 needs to import from scene and scene1.
This sets up a nasty circular import, but as long as you don't put any executable code outside of the class statements, which refernces anything that is imported, you should be ok.... although if you forget that rule later, it will make for confusing error messages.
Your main would import from scene1, in order to get access to Scene1 class.
Only the main needs to import from multiscene, since nobody else needs to instantiate a multiscene object.
I would suggest that MultiScene sets the main_scene attribute of new_scene in switch_scene
def switch_scene(self, new_scene): self.active_scene = new_scene new_scene.main_scene=self new_scene.setup()
so that you don't need to rely on globals (the scenes would then use
Another approach that avoids circular imports would be to do all the imports in main, instantiate the scenes, and set a
next_sceneattribute(for obvious reasons you cannot have this in the constructor, since you would not be able to create all three at once) pointing to the next scene to present. This also makes it easier to stitch together multiple students contributions, since an individual level or whatever does not need to know what level comes next, rather this is defined in main. (you could also def a next_level() in main which cycles through the classes... then each scene simply calls next_level())
ccc last edited by ccc
My recommendation would be that you change your scene naming from: FirstScene, SecondScene, ThirdScene to Scene_0, Scene_1, Scene_2, Scene_3 so that your program can find scenes programmatically at startup time. Each time your students add new scenes, the program could find them automagically.
ccc last edited by ccc
https://github.com/Mr-Coxall/MultisceneTest/pulls contains 5 pull requests... If you accept them all and then you run SceneSwitcher, you should get the requested functionality. It will load up to 999 scenes (Scene0.Scene0, Scene1.Scene1, etc.) from separate .py files that could be independently created by independent teams. Each scene can also run standalone to ease development.
I know this works on the Beta but I can not be sure that it works on Pythonista v1.5.