Closed mjkalyan closed 2 years ago
I don't have a direct answer right now, but instead I'm going to throw two monkey wrenches into this discussion, which may counterintuitively make the problem easier to reason about...
*scenes*
and ensuring all the variables have unique names? How would you approach this if you didn't have to worry about name collisions?Okay, sorry for my late response on this. I think we're falling victim to over-engineering here. We're kind of trying to treat the game loop and scenes as intertwined, but they're two separate entities. Also remember that the game loop runs ~60x/sec, so waiting until the beginning of the loop to switch scenes is not the end of the world.
So I propose:
with-scene-objects
to handle multiple scenes, expose all of a scene's objects by default, and anything else we want to do in that vein.And the results of that:
*scene*
and switch-scene
affect which scene gets loaded/unloaded, but nothing about bindings*scene*
:
with-scene-objects
scene-object
set-up-scene
draw-scene
etc.*scene*
the default scene value in some of these calls. Easy bit of sugar.I'll wait to see if you have any other thoughts before closing this. But I think we want to clean up and merge that PR, and any other bigger commits can go into the next one.
I think we're falling victim to over-engineering here.
My subconscious must have known this while I was trying to figure it out.
Beef up
with-scene-objects
to handle multiple scenes, expose all of a scene's objects by default, and anything else we want to do in that vein.
Okay, so are we working around name collisions by prepending the scene name, or just expecting the user to use unique names?
Okay, so are we working around name collisions by prepending the scene name, or just expecting the user to use unique names?
The user can use unique names or set unique variable bindings like
(with-scene-objects ((*level1* (ball1 ball)) (*level2* (ball2 ball)))
(move ball1 42.2 24.4)))
Hopefully that syntax isn't too clumsy.
This pertains to work in https://github.com/defun-games/claylib/pull/18
Currently, users are expected to use
with-scene-objects
to get bindings for a scene's objects. To makedo-game-loop
smarter, and in keeping with the new*scene*
variable representing a current scene, we'd like to expose all of*scene*
's objects automatically upon switching scenes withswitch-scene
.Ideally we'd have something like this:
Approach 1 (bad): put the
symbol-macrolet
inside thedo
loop. This is potentially slow and doesn't actually give us new bindings immediately after switching scenes, we'd have to wait for the next loop iteration.Approach 2 (bad): place a handler-case around the
symbol-macrolet
which is itself around thedo
loop. Then, signal the condition to be handled inswitch-scene
so we can go back and start the loop again with a newsymbol-macrolet
.Barring other possible issues, this still wouldn't restart at the point right after
switch-scene
is called.So we either unwind too far and can't pick up where we left off or not far enough and end up making new bindings each loop iteration.
So... What about global symbol macros? Approach 2.5 (unknown): Make
switch-scene
define global symbol macros withdefine-symbol-macro
. I'm not yet sure how to unbind global symbol macros, but overwriting occurs properly. So*level-1*
'sball
symbol macro would get overwritten when we switch to*level-2*
, which also has aball
.