Restarting/reloading a level

I have a game with levels and want to be able to pause the game, hit the restart level button and have it reload all of my objects as if it was loaded from the level select menu. I’ve done some research on how other people do it and tried a couple of ways.

I tried inserting a loading screen in between and the loading screen has nothing in it but a delay timer and then the director call to the gameScreen. This method was discussed here.

This method resulted in our backgrounds (parallax) getting loaded, but not the other objects.

I also tried to just reload all the variables with the method mentioned here but that doesn’t work either.

When I tried to reload the variables it was as if initVar() was being called twice because there would be twice as many objects.

Does anyone have any example code or advice on how you have reloaded your levels? Any assistance would be greatly appreciated. Thank you!

EDIT: I forgot to mention that we are using a database that does exactly what is mentioned here as well. [import]uid: 39480 topic_id: 8776 reply_id: 308776[/import]

So I moved onto a different issue and stumbled across the solution. I noticed all of my items weren’t getting removed once I stopped displaying the backgrounds to work on something else. Then it dawned on me. I copied in the cleanGroups() function from director.lua and used that function to clean the groups right before I called resetLevel(). Worked like a charm. Hopefully someone with the same problem can benefit from my pain. [import]uid: 39480 topic_id: 8776 reply_id: 31997[/import]

I struggled with this for a long time.

If, like me, you use modules to create instances of game objects where there are multiple objects required (like cards in a card game), then you’ll need to re-initialise those objects (and therefore the loaded module) at the start of every game.

But what I’ve discovered is that any modules you load from within your lua files aren’t removed even if the local variables you apply to them are set to nil.

So within my game.lua file, I require d a card module, a player module and a view module.

I originally thought doing

card, player, view = nil, nil, nil  

would clean them up, but it doesn’t, regardless of whether the lua module that loaded them is cleaned up.

so in addition to the above, I now use

package.loaded['card'] = nil  
package.loaded['player'] = nil  
package.loaded['view'] = nil  

Solved my restart problems completely! [import]uid: 52386 topic_id: 8776 reply_id: 38395[/import]