How do scenes get reloaded?

I know they can be, because I found the documentation here:

It clearly states:

If you want to skip the cutscene approach, you can reload a scene from itself by calling:

local currScene = composer.getSceneName( “current” )

composer.gotoScene( currScene )

Oh, I so very much want to skip the cutscene approach. I’m not making a game as much as a game tool, a series of menus with random content, that would be great if I could “re-roll” the content without leaving the screen.

Through the use of composer.recycleOnSceneChange = true I’m able to get the scenes to refresh when navigating between them, but this does allow re-navigation as it were.

Navigation is being handled via the TabBar widget, handled by main.

local function onStreetsView( event ) composer.gotoScene( "streets" ) end local function onBuildingsView( event ) composer.gotoScene( "buildings" ) end local function onPeopleView( event ) composer.gotoScene( "people" ) end

Ideally, the same TabButtons that load the scenes would also reload the scenes. But if I need to add another button for that, I’m open to the idea.

I tried putting the above code snippet into the on…View functions, for no effect. I found if I put in a composer.destroyScene() I can almost get something like it. It will reload once, and only once after launch, and then throw errors and trouble. Not a solution.

In the scene itself, in the scene:create, scene:show, scene:hide, scene:destroy and even at the module level itself, before and after the scene declaration: local scene = composer.newScene(). All of these locations do Bad Things™. Namely some variation of:

ERROR: Runtime error
                    bad argument #1 to ‘find’ (string expected, got nil)
                    stack traceback:
                        [C]: in function ‘error’
                        ?: in function ‘gotoScene’
                        main.lua:17: in function ‘_onPress’
                        ?: in function ‘_setSelected’
                        ?: in function ‘?’
                        ?: in function <?:190>

So, where am I supposed to put that code snippet? Or more to the point, what am I actually supposed to do to get scenes to reload without a cutscene?

You could just using goto a “empty transition scene” in your reload function of game play scene. in scene:create of that transition scene, put goto game play scene code in it.
it still kinda cut scene though but it’s way lot easier than trying to debug on the method you’d mentioned.

1 Like

Thanks for the reply phoenixongogo. Ultimately, I didn’t need an intermediate screen.

Coming back to it (after 3 years!) I realized that my issue was the lack of a place to put the function. I wanted to have the tab widget button reload the page, but haven’t found a way to get it to take another “press” when already active (though I thought about a touch detection for the area, but touch listener detection ends at the widget by default, so that’d be another series of hurdles). I suppose I could have added a reload tab button, but I suspected problems down that road, too.

What I was able to finally get working was a “reload” style touch action. Like many modern apps with a list of tables: drag your finger down far enough (100 units currently) and it will reload. And it works!

Almost that easy.

As I used it

local function touchListener ( event )
  if event.phase == "ended" then
    if event.y - event.yStart > 100 then
      local currScene = composer.getSceneName( "current" )
      composer.gotoScene( currScene )

And I added that listener to my background, to cover nearly the whole screen (minus tab bar).

(edit: I had thought getSceneName returned the wrong value to use with gotoScene, but I was mistakeningly using getScene)

You got me re-thinking my approach and what I actually needed to trigger a reload. After 3 years of dust and despair, my side project lives again!

That’s a brilliant appraoch, i could try to implement this method in my project with calling functions which reset variables in modules and keep global ones.

And glad to hear that your side project revived! Cheers :smiley:

A tip for who are new to Solar2d: debug “reload scene” frequently while your project is still in basic shape, there will be as more frustration upon your first try for debuging “reload scene” as your project grown bigger.