How to Restart App and Reinitialize all variables?

In JQuery I can simply do:

location.reload()

It reloads the index.html file and reinitializes all the variables. Essentially restarts the app without closing it.

What is the equivalent in Corona SDK?

Thanks,

–Scot

It’s much more complex with Corona SDK. There are multiple conditions and coding practices that make it hard to have a one liner like that.   In the case of an app with no screen management going on, you would have to code a function that reset all your variables to a default  state.  If you’re using a scene manager like Composer or Storyboard, you can have it unload your modules and when you go back to them, they are fresh, but your main.lua would not reset.  We did a tutorial on reloading storyboard that might be of interest:

http://coronalabs.com/blog/2013/08/20/tutorial-reloading-storyboard-scenes/

Rob

Thanks.  Seems odd for a platform that makes it much simpler than Javascript to write an app.

It’s much more complex with Corona SDK. There are multiple conditions and coding practices that make it hard to have a one liner like that.   In the case of an app with no screen management going on, you would have to code a function that reset all your variables to a default  state.  If you’re using a scene manager like Composer or Storyboard, you can have it unload your modules and when you go back to them, they are fresh, but your main.lua would not reset.  We did a tutorial on reloading storyboard that might be of interest:

http://coronalabs.com/blog/2013/08/20/tutorial-reloading-storyboard-scenes/

Rob

Thanks.  Seems odd for a platform that makes it much simpler than Javascript to write an app.

hi! what about director? Do we have a way to restart the app with director?

In the browser world, you’re not really reloading your app from scratch (though that is the effect).  You’re telling the browser to re-download the existing page again.  Mobile apps are not really based on pages that need fetched from the server.  You can reload the data, but in mobile apps, the UI doesn’t usually get destroyed.  If you think about the browser, the browser is the app, the page is the data and your javascript reload() doesn’t recreate the browser chrome. 

Mobile Apps are (as required by Apple) supposed to be more complex than showing a web page on the screen.  It’s supposed to have more interactivity, more features that make it an app and not just a web page.   Because there are so many different ways to build apps, the idea of a general reset just isn’t practical.

Director is a scene manager, like Storyboard and Composer.  Director is not supported and it’s not been updated to work with the Graphics 2.0 engine.  Now scenes are probably the easiest quantum of the app to do a reset in Corona, so what I’m about to say would apply to Director as well as it would Storyboard and Composer. 

Each of them loads a Lua module.  Each of them can unload a Lua module which gets rid of its contents such than when that scene reloads it starts from scratch.  There are however caveats and that’s the world of Globals.  Even the Javascript example, you can write things outside of the page that reloading doesn’t reset.   With Lua if you change a global variable, it won’t be reset with the scene, unless your scene initializes the variable to what you want.  Another trouble spot is Runtime listeners.  These are attached to the global Runtime and survive a scene’s destruction and re-creation.  Finally you can setup event listeners for things like the system, keyboard handling, and such that react to events that are not scene specific.  Finally due to Corona SDK’s method of working with OpenGL, any native.* object created (text fields, maps, etc.) exist outside of the scene even though it may be created by the scene.

With Storyboard and Composer, you have the option to remove the scene, i.e. composer.removeScene(“myscene”, false) that will destroy the scene (assuming it’s not the current scene) and when you go back it will start like it’s brand new with these “global” cavaets aside.

Reloading a scene directly is more tough and we did a tutorial on this:  http://coronalabs.com/blog/2013/08/20/tutorial-reloading-storyboard-scenes/

This is storyboard related, but the concepts are the same.  If you program the scene properly and avoid some common pitfalls, then the .reloadScene() method (.gotoScene() in Composer) will reset things.  But you really have to understand what scene events happen when and why.

Rob

hi! what about director? Do we have a way to restart the app with director?

In the browser world, you’re not really reloading your app from scratch (though that is the effect).  You’re telling the browser to re-download the existing page again.  Mobile apps are not really based on pages that need fetched from the server.  You can reload the data, but in mobile apps, the UI doesn’t usually get destroyed.  If you think about the browser, the browser is the app, the page is the data and your javascript reload() doesn’t recreate the browser chrome. 

Mobile Apps are (as required by Apple) supposed to be more complex than showing a web page on the screen.  It’s supposed to have more interactivity, more features that make it an app and not just a web page.   Because there are so many different ways to build apps, the idea of a general reset just isn’t practical.

Director is a scene manager, like Storyboard and Composer.  Director is not supported and it’s not been updated to work with the Graphics 2.0 engine.  Now scenes are probably the easiest quantum of the app to do a reset in Corona, so what I’m about to say would apply to Director as well as it would Storyboard and Composer. 

Each of them loads a Lua module.  Each of them can unload a Lua module which gets rid of its contents such than when that scene reloads it starts from scratch.  There are however caveats and that’s the world of Globals.  Even the Javascript example, you can write things outside of the page that reloading doesn’t reset.   With Lua if you change a global variable, it won’t be reset with the scene, unless your scene initializes the variable to what you want.  Another trouble spot is Runtime listeners.  These are attached to the global Runtime and survive a scene’s destruction and re-creation.  Finally you can setup event listeners for things like the system, keyboard handling, and such that react to events that are not scene specific.  Finally due to Corona SDK’s method of working with OpenGL, any native.* object created (text fields, maps, etc.) exist outside of the scene even though it may be created by the scene.

With Storyboard and Composer, you have the option to remove the scene, i.e. composer.removeScene(“myscene”, false) that will destroy the scene (assuming it’s not the current scene) and when you go back it will start like it’s brand new with these “global” cavaets aside.

Reloading a scene directly is more tough and we did a tutorial on this:  http://coronalabs.com/blog/2013/08/20/tutorial-reloading-storyboard-scenes/

This is storyboard related, but the concepts are the same.  If you program the scene properly and avoid some common pitfalls, then the .reloadScene() method (.gotoScene() in Composer) will reset things.  But you really have to understand what scene events happen when and why.

Rob

@ Rob

Hi , i’ve been searching a lot on the website but didn’t find the answer to my problem…

I would like to know if there is way to “refresh” or “reload” many objects in a group?

Ex : i have 4 objects in a group “my group” and i select one but then when i tap a button “refresh” i would like to refresh everything so i can start from “zero” to select another object…

PS :  i don’t want to restart the app just refresh my objects inside my group

Thank you

local ObjectCache = {} local myobject = display.newRect(group, 0, 0, 200, 200) ObjectCache[#ObjectCache + 1] = myobject --destroy objects for i = 1, #ObjectCache do ObjectCache[i]:removeSelf() ObjectCache[i] = nil end

place the destroy objects in a function then when you hit your refresh button just same something like.

DestroyGroupA()

BuildGroupA()

@ Rob

Hi , i’ve been searching a lot on the website but didn’t find the answer to my problem…

I would like to know if there is way to “refresh” or “reload” many objects in a group?

Ex : i have 4 objects in a group “my group” and i select one but then when i tap a button “refresh” i would like to refresh everything so i can start from “zero” to select another object…

PS :  i don’t want to restart the app just refresh my objects inside my group

Thank you

local ObjectCache = {} local myobject = display.newRect(group, 0, 0, 200, 200) ObjectCache[#ObjectCache + 1] = myobject --destroy objects for i = 1, #ObjectCache do ObjectCache[i]:removeSelf() ObjectCache[i] = nil end

place the destroy objects in a function then when you hit your refresh button just same something like.

DestroyGroupA()

BuildGroupA()