Using modules, how do I call function in active scene?

I’m using external modules to handle some functions, but how do I call a function in my active scene (“scene1”).

[lua]

local scene1 = require(“scene1”)

scene1.test()

[/lua]

The above gives an error: loop or previous error loading module ‘scene1’

is the “scene1” module in the root directory, and is the filename itself all in lowercase?

Hi Alex, 

yes, and yes. All lower case and in the root directory. 

To clarify, i’m calling the function in my scene1 from an external module. 

Is the address correct if it’s within scene:show / did?

  1. scene1.function()

so this “external module” requires scene1, yes?

so you’d get that “loop” error if scene1 requires this external module.

(because it implies a never-ending recursive load)

probably time to refactor - ask:  why is a non-scene module “calling into” a scene?  typically if you need to do something like that you should be passing a reference to the external module of the scene, from the scene.  (if that makes sense)

for example, say you have a “helper” routine to populate a scene’s display during scene.create, then give that helper a reference to the scene, fe (incomplete, pseudocode, just to convey idea):

-- in scene1.lua local helper = require("helper") function scene:create() helper:createStuff(self) end -- etc -- in helper.lua local M = {} function M:createStuff(scene) -- scene passes in a ref to itself local stuff = display.newRect(scene.view, whatever...) -- or scene:test() as in your example, etc - do whatever you want with the scene ref return stuff end return M

Thanks for your help Dave. I understand what your saying and it makes sense. 

is the “scene1” module in the root directory, and is the filename itself all in lowercase?

Hi Alex, 

yes, and yes. All lower case and in the root directory. 

To clarify, i’m calling the function in my scene1 from an external module. 

Is the address correct if it’s within scene:show / did?

  1. scene1.function()

so this “external module” requires scene1, yes?

so you’d get that “loop” error if scene1 requires this external module.

(because it implies a never-ending recursive load)

probably time to refactor - ask:  why is a non-scene module “calling into” a scene?  typically if you need to do something like that you should be passing a reference to the external module of the scene, from the scene.  (if that makes sense)

for example, say you have a “helper” routine to populate a scene’s display during scene.create, then give that helper a reference to the scene, fe (incomplete, pseudocode, just to convey idea):

-- in scene1.lua local helper = require("helper") function scene:create() helper:createStuff(self) end -- etc -- in helper.lua local M = {} function M:createStuff(scene) -- scene passes in a ref to itself local stuff = display.newRect(scene.view, whatever...) -- or scene:test() as in your example, etc - do whatever you want with the scene ref return stuff end return M

Thanks for your help Dave. I understand what your saying and it makes sense.