Module throws error when re-entering scene.

>> Error message: “attempt to call method ‘insert’ (a nil value)”

I have a module ‘m’ that inserts a bunch of objects in to a display group, say, m.view.

This group is then inserted to the main view/group on Storyboard’s enterscene().

All is well, until the scene is exited and re-entered.  I get the above error.
It appears m.view is 'nil’ed out on exitScene(), and I am no longer able to use it on enterscene() again.

—  

module.lua:

  1. local m = {}
  2.   m.view = display.newGroup ()
  3.    …
  4.   function m.spawnStuff ()
  5.     – …  stuff = a new display object of any kind
  6.     m.view:insert (stuff)
  7.   end
  8.  return m

foobar.lua:

  1. function scene:enterScene (event)
  2.    local group = self.view
  3.    …
  4.    group:insert (m.view)
  5. end

The module works perfectly without Storyboard.  So my question is, does exitScene() obliterate m.view so that m.spawnStuff() can no longer be used afterwards?  What is the best way around this problem, if possible without modifying the module?

You didn’t say which line causes error but nevertheless, always read errors messages with understanding. Error sad that you (or some code) are trying to call insert() function which is nil value (doesn’t exists). Insert is the method of displayGroup object so that means that object on which you are calling insert (not object which is being inserted) is not of displayGroup type.

Thanks for that; the line that throws the error is 6: attempt to call method ‘insert’ (a nil value).

If there was a problem with that line, then why doesn’t it throw the error the first time it enters enterScene()?

It sounds like it is saying *method* m.view:insert no longer exists, and not the ‘stuff’ that has the problem.

DId exitScene() removed the method along with m.view entirely, and because m.view is declared locally in the module and not from the scope of enterScene, it is not created again on re-entering enterScene()?

Error is quite easy

First notice. Dispaly groups (like tables and al objects having table-like interface) are always passed by reference.

Notice 2. ExitScene() of storyboard scene is function clearing your screen, right? So what it does is looping through all object insterted inside scene’s view and removing it.

Merging this facts:
On enterScene() you insert your ‘view’ from module inside scene’s view (display group). What you are passing is reference to original. When scene exits, it clears itself removing all objects inserted to scene’s view. So during this process the ‘view’ from module is also removed. You probably still can call function because removeSelf() function strips Corona object to naked lua tables. So insert() function of Corona object is removed from table. So when you try to call insert() it is no longer there.

Right, so exitScene() does obliterate m.view().  So then what is the solution, if possible, without modifying the module?

What do you wanna do precisely?

I would like to keep the module the way it is inserting all its object to m.view.

I would also like to be able to insert m.view to storyboard’s self.view.

Since, existScene() obliterates m.view,

I would like it m.view to be created again on enterScene().

Make some function in module recreating group :slight_smile:

For example
[lua]
local m = {}

m.view = nil

function m.createView ()
m.view = display.newGroup()
end

– other code

return m
[/lua]

Piotrz55, thank you!  Not a dramatic modification to the module at all, and that fixed everything.  Can’t believe how simple that was.  haha.

You didn’t say which line causes error but nevertheless, always read errors messages with understanding. Error sad that you (or some code) are trying to call insert() function which is nil value (doesn’t exists). Insert is the method of displayGroup object so that means that object on which you are calling insert (not object which is being inserted) is not of displayGroup type.

Thanks for that; the line that throws the error is 6: attempt to call method ‘insert’ (a nil value).

If there was a problem with that line, then why doesn’t it throw the error the first time it enters enterScene()?

It sounds like it is saying *method* m.view:insert no longer exists, and not the ‘stuff’ that has the problem.

DId exitScene() removed the method along with m.view entirely, and because m.view is declared locally in the module and not from the scope of enterScene, it is not created again on re-entering enterScene()?

Error is quite easy

First notice. Dispaly groups (like tables and al objects having table-like interface) are always passed by reference.

Notice 2. ExitScene() of storyboard scene is function clearing your screen, right? So what it does is looping through all object insterted inside scene’s view and removing it.

Merging this facts:
On enterScene() you insert your ‘view’ from module inside scene’s view (display group). What you are passing is reference to original. When scene exits, it clears itself removing all objects inserted to scene’s view. So during this process the ‘view’ from module is also removed. You probably still can call function because removeSelf() function strips Corona object to naked lua tables. So insert() function of Corona object is removed from table. So when you try to call insert() it is no longer there.

Right, so exitScene() does obliterate m.view().  So then what is the solution, if possible, without modifying the module?

What do you wanna do precisely?

I would like to keep the module the way it is inserting all its object to m.view.

I would also like to be able to insert m.view to storyboard’s self.view.

Since, existScene() obliterates m.view,

I would like it m.view to be created again on enterScene().

Make some function in module recreating group :slight_smile:

For example
[lua]
local m = {}

m.view = nil

function m.createView ()
m.view = display.newGroup()
end

– other code

return m
[/lua]

Piotrz55, thank you!  Not a dramatic modification to the module at all, and that fixed everything.  Can’t believe how simple that was.  haha.