Reloading modules code and composer scene lua file code from top to bottom

Hi. I’m new to solar2d, but not quietly new. I just want to know how to reload modules or costomized modules and composer scene lua fie like running it’s code from top to bottom every time I reload it or open it, or how to have a very fresh start like the program has been relaunched using file --> relaunch but specifically only for that built in or costomized modules or composer scene lua file.

Please help me, thank you.

Thank you, but what I want is to run the code of a composer scene lua file or a module from top to bottom every time i open it or load it.

I just want to run the code from top to bottom every time I load a composer scene, so that they will have a very fresh start

Unrequire your module and then require it again. Although this is not the way to achieve what you want.

Make a function that resets all variables in the module.

Thank you SGS, but I already know how to unrequire modules. can you show me even in a very short example how to make a function that reset all the variable in the module?

Do you mean that you need to load same scene again while it is already loaded?

Yes Andrey5045. That is what exactly I want to do, reload the same scene like it was reloaded before for the first time.

Gotcha!

I give code that I use for myself.
The idea is to have a so-called “transition scene”, in other words YOU CANNOT go from scene A to scene A, instead you can go to scene A to scene B and go back to scene A.

  1. Create a transition scene something like “transition.lua” the code is simple:
 --
 -- EMPTY SCENE to organase transitions between the pages
 
 local scene = composer.newScene()
 composer.recycleOnSceneChange = true
 
 local tmp_str_a = composer.getSceneName("previous")
 if tmp_str_a ~= nil then
 	composer.removeScene(tmp_str_a)
 end
 
 ----------
 
 function scene:create(event)
 	local phase = event.phase
 	local sceneGroup = self.view
 end
 
 function scene:show(event)
 	local phase = event.phase
 	if phase == "will" then
 	elseif phase == "did" then
 		local the_effect_str = composer.getVariable("effect")
 		local scene2go_str = composer.getVariable("scene")
 		local time_int = composer.getVariable("time")
 		if time_int == nil then
 			time_int = 200
 		end
 		-- cleaning up
 		composer.setVariable("scene", nil)
 		composer.setVariable("effect", nil)
 		composer.setVariable("time", nil)
 		composer.gotoScene(scene2go_str, {effect = the_effect_str, time = time_int})
  	end
 end
 
 function scene:hide(event)
 	local phase = event.phase
 	if event.phase == "will" then
 	elseif phase == "did" then
 	end
 end
 
 function scene:destroy(event)
 	local sceneGroup = self.view
 	local phase = event.phase
 end
 
 -- Listener setup
 scene:addEventListener("create", scene)
 scene:addEventListener("show", scene)
 scene:addEventListener("hide", scene)
 scene:addEventListener("destroy", scene)
 
 return scene
  1. anywhere in your scene A when you need to realod it, use the following code:

composer.setVariable(“effect”, “crossFade”)
composer.setVariable(“scene”, “YOUR_SCENE_TO_RELOAD”)
composer.setVariable(“time”, 200)
composer.gotoScene(“transition”, {time = 0}) – transition.lua

I hope the code is self-explanatory for you :slight_smile:

Thank you very much Andrey5045, this is a resolution for my problem

1 Like

Hi Andrey5045. Now, I’m working with overlay scene, is there any way to reset the overlay scene back to when I load it for the first time?

This isn’t really how you want to do this, like @anon63346430 already stated.

Since you know what values all of your variables need to have, you should just create a function where you set them their initial values. Then, whenever you want, you can just call that function and reset all of the variables. This approach works with all types of scenes.

By leaving a scene, then destroying it and immediately returning to it, you are wasting precious time and resources, as well as making your project more difficult to maintain.

I guess the approach is the same: destroy & recreate. Also, as suggested, make an initialisation function what will be used to make an “as-if-a-fresh-start”.
@XeduR Is it OK to delete the screen objects of a scene and then create them again in this case?

Destroying all of the display objects (or the entire scene), especially with overlay scenes, will likely result in perceivable lag spikes and poor user experience via “flashing/disappearing” objects.

When done with regular scenes, the lag spike may not be as visible since it will be hidden in the transitions, but with overlay scenes, it becomes very visible since the underlying scene remains in view.