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.
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?
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.
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
anywhere in your scene A when you need to realod it, use the following code:
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.