SOLVED: Restarting a Scene Error.

SOLVED: See the last couple of posts for explanation for similar issues. Good Day!

Hi,

This will look like a lot but the code is easy to follow. So please bare with me. ^-^
I’m getting some strange errors when my level restarts by death but not by restart button.
I am using the Director class, and i created several groups.

This is the top of my level .lua file

module(..., package.seeall);  
function new()  
 local localGroup = display.newGroup();  
  
 -- Create ui display group. (to follow the player but not be scrolled around)  
 local ui = display.newGroup();  
  
 -- Create master display group. (for global "camera" scrolling effect)  
 local game = display.newGroup();  
 game.x = 0;  
 game.y = 0;  

Because my camera scrolls i seperate my images into the different groups.
I think this bit is VITAL, game.x and game.y is the whole level except the UI. (because that shouldn’t scroll)
My restart code is this:

function restartScene()  
 Runtime:removeEventListener( "enterFrame", move );  
 Runtime:removeEventListener( "enterFrame", moveCamera );  
 Runtime:removeEventListener( "enterFrame", boundreset );  
 Runtime:removeEventListener( "enterFrame", Cinematic );  
 cancelAllTimers();  
 cancelAllTransitions();   
 audio.play(btnpress);  
 physics.start();  
 director:changeScene(e.target.scene,"fade", "white");  
 package.loaded["W1-2"] = nil  
 \_G["W1-2"] = nil  
 return true  
 end  

If I die, I call the restart function to restart the level.
I recieve an error even though the restart scene is the same as the button’s restartScene.
(Maybe because i call it from a timer or collision?)

"
Runtime Error
attempt to compare number with nil value,

.lua:706
"

This leads me to a function boundreset()
This function checks the game.x and game.y (director group)
for camera scroll.

function boundreset

local function boundreset()  
 if (game.x \> 0) then  
 game.x = 0;  
 --return true  
 end  
 if (game.x \< -480) then  
 game.x = -480;  
 --return true  
 end  
 if (game.y \> 0) then  
 game.y = 0;  
 --return true  
 end  
 if (game.y \< -320) then  
 game.y = -320;  
 --return true  
 end  
 end  

It would seem it can no longer read the game.x and game.y. (They are nil? :s)

I would like to repeat if I press a button I created to call the restartScene, it does the exact same thing and has no problem with game.x and game.y, it simply resets them back to 0 , 0 (At the start of the file) and the level starts anew.

I’d appreciate brainstorming here i suppose.
Is there something that could cause this from calling from a timer or collision?
Am I nil’ing the game.x and game.y in some way that prevents them getting reset to 0, 0 when the level restarts?
Thanks for your consideration.
Yours Sincerely
Matt. [import]uid: 91798 topic_id: 17148 reply_id: 317148[/import]

Oooh!! That is going to be a little difficult
bare google

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 17148 reply_id: 64551[/import]

Haha Yes! I want everyone to get naked with me and brainstorm. JayantV.

I’ve narrowed it down to timers not being cancelled if they’re in progress.

timer.performWithDelay(2000, DeathTest)

and DeathTest’s restartScene function (code in post #1)
causes game.x and game.y to become ‘nil’ and not come back when the level is reloaded.

I’m trying to keep it easy to grasp.

Thanks again! [import]uid: 91798 topic_id: 17148 reply_id: 64559[/import]

why not add a check if the game variable is *not* set?

if not game then return end

that will not execute the rest of the function if the game variable is not set

then when ever your game object is initialised, the x and y will be available and the function will run than give you errors…

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 17148 reply_id: 64562[/import]

I’d second Jayant’s suggestion above.

Your comment; "Haha Yes! I want everyone to get naked with me and brainstorm." is by far my favorite of the week. Nicely done :wink:

Peach :slight_smile:

[import]uid: 52491 topic_id: 17148 reply_id: 64581[/import]

For anyone else looking, I added the following code a the top of the function that was given me trouble. (boundreset shown above)

if(game.x == nil and game.y == nil) then  
 return 1  
else  
... -- usual code  

Thats done the job, and I’m a very happy SpoOok now.

But i hope no ones started getting dressed yet as I have another inquiry.

This questions about Texture Memory.
Between level switches, it is supposed to reset to 0 correct?
(Sortof - before the next level is loaded anyway)

Well i have 2 levels, one using more graphical stuff than the other.

Level 1 uses 4.292608 Texture Memory.
Level 2 uses 9.498624 Texture Memory.

However if I enter level 2 then go to level 1, Level 1’s usage goes up to 10.059776.

Is this a dreaded Texture Memory Leak?
I use director as i’ve said and my changeScene looks like this atm.

function changeScene(e)  
 if(e.phase == "began") then   
 Runtime:removeEventListener( "enterFrame", Cinematic );  
 Runtime:removeEventListener( "touch", stop );  
 Runtime:removeEventListener( "enterFrame", move );  
 Runtime:removeEventListener( "enterFrame", moveCamera );  
 Runtime:removeEventListener( "enterFrame", boundreset );  
 cancelAllTimers();  
 cancelAllTransitions();  
 --ui:removeSelf();  
 --game:removeSelf();  
 --localGroup:removeSelf();  
 audio.play(btnpress);  
 physics.start();  
 preloader:changeScene(e.target.scene,"fade");  
 package.loaded["W1-2"] = nil  
 \_G["W1-2"] = nil  
 return true  
 end  
 end  

It’s quite a large amount of Texture Memory being continued onto level 1, is it possible that the director only removes the localGroup() group?
And i have to somehow remove the ui:group and game:group i created manually?
(Since i’d say all my images fall into those 2 groups. Level 1 and level have images in common, so math wise it makes sense to be the reason.)

Thanks again!
Stay free!

PS: Also Peach, considering it’s Monday today, your favourite comment of the week, really isn’t that great of an achievement.
Might as well give it the ‘first line i’ve read this week therefore the best’ achievement too.
haha :stuck_out_tongue:

[import]uid: 91798 topic_id: 17148 reply_id: 64630[/import]

What goes ‘Bump’ in the night?

Me…

How to remove groups manually when changing scene?
I’m thinking director class only removes the main one ‘localGroup’

And i created 2 extra groups, ‘ui’ and ‘game’

I think thats the reason my Texture Memory is remains high from the high graphic level when i go back to the low graphic level.
[import]uid: 91798 topic_id: 17148 reply_id: 64730[/import]

I don’t know the answer to your question, but I thought it might help if I share how mine looks like. Please see below. Notice how LEVELS.lua changes from 1651.712 to 8662.016, then back down to 2894.848, and then eventually return to 1651.712?

I vaguely remember reading something about how memory may not be released the moment garbage is collected. Anyhow, based on the terminal output, I’d say my texture memory is properly handled by the director class – even though, at times, it looks as if texture memory is climbing at an alarming rate.

So… if you are looking at the texture memory increase upon a single scene change, you may not be getting the whole picture. (Again, this may not be applicable to you, but I thought it may help in terms of *sanity check*)

Naomi


>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> LEVELS.lua
MemUsage (in KB): 292
textureMemoryUsed (in KB): 1651.712

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> LEVEL1.lua
MemUsage (in KB): 459
textureMemoryUsed (in KB): 8662.016

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> LEVELS.lua
MemUsage (in KB): 444
textureMemoryUsed (in KB): 8662.016

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> LEVEL2.lua
MemUsage (in KB): 453
textureMemoryUsed (in KB): 4467.712

>>>>> hit the last line of LEVELS.lua reached
MemUsage (in KB): 436
textureMemoryUsed (in KB): 2894.848

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> LEVEL3.lua
MemUsage (in KB): 471
textureMemoryUsed (in KB): 4729.856

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> LEVELS.lua
MemUsage (in KB): 453
textureMemoryUsed (in KB): 4729.856

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> LEVEL4.lua
MemUsage (in KB): 483
textureMemoryUsed (in KB): 5008.384

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> LEVELS.lua
MemUsage (in KB): 465
textureMemoryUsed (in KB): 2911.232

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> MENU.lua
MemUsage (in KB): 305
textureMemoryUsed (in KB): 1651.712

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> LEVELS.lua
MemUsage (in KB): 305
textureMemoryUsed (in KB): 1651.712

[import]uid: 67217 topic_id: 17148 reply_id: 64742[/import]

Interesting hypothosis. I know you can’t help but thanks for the thoughts!

I do actually have a Level Select Screen and 15 test levels made up. Most of them are just a Background and Text though.

So i can actively go between levels with ease.
I have the Texture Memory print on a every frame listener.
I wait but it doesn’t go down.
Here’s a run down.

Level Select
Texture Memory:
1.3 --(MB I imagine)

Onto Level 3 (Background & Text)
Texture Memory:
1.1

Back to Level Select
Texture Memory:
1.3 --This proves no leak on bare bones level

Onto Level 1 (Lower Graphical)
Texture Memory:
4.2

Back to Level Select
Texture Memory:
2.0 – So there is a leak here.From Level 1.

Onto Level 3 (Background & Text)
Texture Memory:
1.7 – The previous leak continues on.

Back to Level Select
Texture Memory:
2.0

Onto Level 2 (Higher Graphical)
Texture Memory:
9.4

Back to Level Select
Texture Memory:
7.8 – Larger Memory Leak from Level 2, More Graphics unremoved I imagine.

Onto Level 3 (Background & Text)
Texture Memory:
7.5 – Continued to other levels.

As you can see this will be game breaking if i don’t figure it out.
I’ve just gone into my level one and changed all my

ui:insert(...)  
game:insert(...)  

into localGroup:insert(...)

And that has NOT made any difference to the Texture Memory upon leaving the level.
So i was probably barking up the wrong tree there?
I’m not sure what else it could be.

What I use in Level 1:
-local Spritegrabber spritesheet declarations (Cant insert them)

-local Sound declarations (Cant insert them)

-local Variables (Should be forgotten in scene change?)

-local Display Objects (All inserted into ui or game groups)

-local Functions (dont have to do anything with them)

-EventListeners (I remove Runtime event listeners.)

Unless it’s not cleaning my groups like it says it is then i dunno what else to do.
Bare bones levels reset Texture Memory ok.

Phew!
Good Night -.-z
[import]uid: 91798 topic_id: 17148 reply_id: 64777[/import]

I do a couple of things that you may not be doing:

  1. I use Spriteloq for my spritesheets, and it comes with dispose function that clears out the spritesheets from memory. I don’t know how you’re disposing your spritesheet and removing them from memory.

  2. Spriteloq also comes with loq_util API. Because I’m using module(…, package.seeall) to load modules, I use this loq_util to unrequire them when I’m done with the module. Calling the unrequire function clears out the loaded module from memory. That said, loading/unloading module shouldn’t affect testure memory.

So… maybe, your issue is related to how you may or may not be disposing your spritesheets?

Naomi [import]uid: 67217 topic_id: 17148 reply_id: 64779[/import]

Not disposing of my spritesheets does make sense for a reason of the texture memory leak.
(As i use alot of spritesheets, so it weighs in right-ish size wise if they’re not getting disposed of.)

I’m going to re-read your last post over 4 or 5 times, look into Spriteloq, look at SpriteGrabber,
rinse and repeat.

And call the doctor in the morning!

I’ll give an update tomorrow on the situation of this investigation.

Thankyou, Naomi. [import]uid: 91798 topic_id: 17148 reply_id: 64780[/import]

Literally 7 minute has passed.

Looked for a spriteGrabber dispose tool as you described.

myspritesheet:remove(); !!!

Called that for my all my spritesheets before the changeScene and everythings back to normal!
Thankyou so much Naomi!

I am very happy, and it’s good cause i hate to go to bed unhappy :slight_smile:
I’ll sleep well today!

Thanks again, i want to repay you but i know not how! [import]uid: 91798 topic_id: 17148 reply_id: 64782[/import]

So glad to hear you’ve found your solution. I will always need all the help I can get – so let’s help each other as we journey down the game development path with Corona SDK.

Sleep tight, and have a lovely dream!

Naomi [import]uid: 67217 topic_id: 17148 reply_id: 64785[/import]

For future people looking in for answers.
Tell me the lottery numbers!

Also, I had a little trouble with spriteGrabber and disposing on certain spritesheets.

I’ll show 2 ways: 1 incorrect, 1 correct.

First you have to grab your spritesheets. Notice it’s called Belt.

 local Belt = grabber.grabSheet("Belt"); 

When you come to create the display object, be sure it’s name is not the same as the grabbed sheet. Notice here it is Belt like the above, That is bad.
This causes errors when creating multiple images using the same spriteSheet and disposing.
Even though it still allows image creation and usage

local Belt = Belt:grabSprite("Belt", true, {Anim = {1,12,2000,0} });

What i recommend is simply numbering your display objects thusly.

local Belt1 = Belt:grabSprite("Belt", true, {Anim = {1,12,2000,0} });

And thats history, future people! [import]uid: 91798 topic_id: 17148 reply_id: 64787[/import]