include same source in all Scenes??

lets say I have 10 scenes
each contains
------>

function removeall()

cleanGroups(localGroup)
– several other functions

end

function cleanGroups ( objectOrGroup )
if objectOrGroup.numChildren then
while objectOrGroup.numChildren > 0 do
cleanGroups ( objectOrGroup[objectOrGroup.numChildren])
end
end
if (objectOrGroup ~= nil) then
objectOrGroup:removeSelf();
end
return
end

now I would like to place above two functions in a external file and include
simple (like in php) in my actual scene.

It seems it does not work with require “externalfile.lua”

as localGroup is defined in the scene itself.
its just one problem i have with that, but it does represent quiet good.

Should I have a option to do a include like in php, where it just insert the texts
from the external file, without any moduling and classifying it should work fine.

ANy Ideas (it would just save lots of code repetition)

thx
chris
[import]uid: 4795 topic_id: 21017 reply_id: 321017[/import]

You can do

local var = require(“mymodule”)

so you can assign it a name

then you access its methods via the local var [import]uid: 84637 topic_id: 21017 reply_id: 82982[/import]

:slight_smile: that i use in several other cases… but in above (try yourself)
it does not work…

–main.lua

local var = require(“mymodule”)

local background2 = display.newImage( “graphics/bg_mydays.png”, 0, 0, true )
localGroup:insert(background2)

var.removeall()

–mymodule.lua

function removeall()
cleanGroups(localGroup)
– several other functions
– more functions
end

function cleanGroups ( objectOrGroup )
if objectOrGroup.numChildren then
while objectOrGroup.numChildren > 0 do
cleanGroups ( objectOrGroup[objectOrGroup.numChildren])
end
end
if (objectOrGroup ~= nil) then
objectOrGroup:removeSelf();
end
return
end


will say " mymodule.lua attempt to index local ‘objectOrGroup’ (a nil value) "

… as localGroup is defined outside of mumodule.lua
it would work if i do var.cleanGroups(localGroup)
BUT its just an example
and there are even more things to clean (Runtime Listeners etc)

So the MODULE System, does not really work in here, without creating so much code
that i could keep it like it is right now :slight_smile:

For that… its really a search for a solution just to INCLUDE *like in php* external Code
without any module functions etc… just simple include external data.

I was reading in LUA it could be done with dofile()
but within Corona i get dofile() is not supported in Corona !!!

:frowning:

chris

[import]uid: 4795 topic_id: 21017 reply_id: 82984[/import]

I understand what you mean. There is probably a way to achieve this… I just can’t think of one right now :confused:

Edit : you could achieve this with a global master function perhaps.

like

function _G.superClass()

but there is probably a more elegant way of handling this [import]uid: 84637 topic_id: 21017 reply_id: 82985[/import]

I’m guessing that you could *maybe* do:
scene.modules = {}
scene.yourModule = require(“mymodule”);

and then just call it as scene.yourModule.whateverFunction/var

but, having not tested it and having not used scene much, it’s all just a guess… [import]uid: 19193 topic_id: 21017 reply_id: 83072[/import]

Yeah, so this works, in your main.lua:
[lua]local storyboard = require “storyboard”
storyboard.modules = {}
storyboard.modules.myModule = require(‘myModule’)[/lua]

in your myModule.lua:
[lua]local g = {}
g.hiScott = ‘HI!’
return g[/lua]

in your level1.lua:
[lua]print(storyboard.modules.myModule.hiScott)[/lua]

I did this using the game template from the simulator…

[import]uid: 19193 topic_id: 21017 reply_id: 83074[/import]

Hi there,

thanks all for your enthusiastic ideas.

I really appreciate…

Just keep in mind… don’t run over the goal.

In Programming there are always options to code from the back head through the eyes.
Also even if we work with modules, I do not see a change to define for example a
runtime listener in the main source and to remove it in the Module … its not just about to print a simple string.

AND Corona is about simplifying.
In that Case its really just about to have the option creating external files, with code pieces
and to “include” them like a CSS / JavaScript or PHP Code into the MainCode where 'include (“xxx”)
is defined.

Also the Argument about APPLE restrictions are not valid, should the Corona Simulator/Builder
just include that pieces in our main code, creating kind a temporary new full sourcecode, BEFORE RUnning or Compiling.

Therefore, there is no need for compiling while runtime (Apple restrictions)
And we would get a simple option to create kind a template code, that we just include wherever we need AND… if we need to change something (like in css) we just do it in one file and not in all places where we need it.

Thanks and greets Chris

ps. to avoid double posting… please continue answer on my questions in that topic:
https://developer.anscamobile.com/forum/2012/01/27/second-dofilefilename-its-lua-not-corona

[import]uid: 4795 topic_id: 21017 reply_id: 83104[/import]

Uh, …ok. [import]uid: 19193 topic_id: 21017 reply_id: 83134[/import]