Modules... external files... require...

My first post…  :slight_smile:

I have been playing with Corona a while but want to get to work with it. Therefore I want to get som basic structure of my upcomming project. But I can’t really get it.

I would like to start my main.lua with some require…

local globals=require("script.globals") local sida1=require("ui.sida1") --local sida2=require("ui.sida2") local sida\_actions=require("script.sida\_actions") local gemensamma=require "script.gemensamma" 

in sida1, I’ve got this declaration

taenbildknapp = display.newImage("arr\_left.png",35,60)

and in the file script.sida_actions, I’ve got this reference to the object above:

taenbildknapp:addEventListener( "touch", TaEnBild )

But this don’t work. Cororna says “Attempt to index global ‘taenbildknapp’ (a nil value)”

Very basic question… but I would need some help.

Regards

Johan

@Johan,

That is a very unusual organization scheme and I don’t suggest you do that.

  1. You’re using globals when you don’t need them.

  2. You’re separating the building of objects from the code that operates on them.

  3. You don’t have (what I can detect) any clear linkage between code in separate files so you can maintain this later and remember h ow it works.

You’d be better off making modules that contain all of the following:

  1. Builder function for creating your objects.

  2. Listeners and other functions closely bound to the object.

  3. Other support code for the objects and listeners.  i.e. Utility code unique to this scope.

If on the other hand you’re trying to build a hierarchical system (not necessarily OOP) where there are multiple object types which share common listeners, etc you could do this:

-- common.lua -- contains commonly used listener for this example. local common = {} common.onTouch( self, event ) if( event.phase == "ended" ) then print(self.myName) end return true end return common

-- builder1.lua -- Builds simple object and uses common.lua listener local common = require "common" local builder = {} builder.create( x, y, name ) local tmp = display.newCircle( x, y, 10 ) tmp.myName = name tmp.touch = common.onTouch tmp.addEventListener( "touch" ) return tmp end return builder

-- builder2.lua -- Builds simple object and uses common.lua listener local common = require "common" local builder = {} builder.create( x, y, name ) local tmp = display.newRect( x, y, 20, 20 ) tmp.myName = name tmp.touch = common.onTouch tmp.addEventListener( "touch" ) return tmp end return builder

-- main.lua -- Simple example bringing it all together local builder1 = require "builder1" local builder2 = require "builder2" builder1.create( 100, 100, "Bill" ) builder2.create( 100, 150, "Bob" ) builder1.create( 200, 100, "Susie" ) builder2.create( 200, 150, "Sally" )

Hi, thanks!

My intension is to separate different  type of code in separate files in order to get it more easy to work with in the long run, gui as one type, scripts as another and so on. 

The example may be  what I am looking for. I have also been looking at dofile and this also seems to be a method to execute code put in separate files.

My Corona experience is only some hours… but I like it so far.

Hi again!

See below. Now I got working:

gemensamma.ReadSerialNummer(), great!

But I also require ui.sida2 where I define the GUI… BtnKamera is NIL and the insert to a displaygroup isn’t workning. Why?

-- -- main.lua -- ----------------------------------------------------------------------------------------- -- INCLUDE & GLOBALER local globals=require("script.globals") require "ui.sida1" ----------------------------------------------------------------------------------------- --local sida2=require("ui.sida2") local gemensamma=require "script.gemensamma" --MAIN gemensamma.ReadSerialNummer() --working! local AppSidaGrupp1 = display.newGroup() print(BtnKamera) -- BtnKamera = nil, isn't working --AppSidaGrupp1:insert( sida1.BtnKamera ) --isn't workning.

Code in ui.sida1, where BtnKamera is defined. Why are these controls not accessable from main.lua?

display.setDefault("background" , 1, 115/255, 0 ) BtnKamera = display.newImage("cam.png",140,60) BtnKamera:addEventListener( "touch", TaEnBild ) BtnArkiv = display.newImage("arkiv.png",250,60) BtnArkiv:addEventListener( "tap", HamtaEnBild )

forget the last post… I have now got it to work…  :smiley:

@Johan,

That is a very unusual organization scheme and I don’t suggest you do that.

  1. You’re using globals when you don’t need them.

  2. You’re separating the building of objects from the code that operates on them.

  3. You don’t have (what I can detect) any clear linkage between code in separate files so you can maintain this later and remember h ow it works.

You’d be better off making modules that contain all of the following:

  1. Builder function for creating your objects.

  2. Listeners and other functions closely bound to the object.

  3. Other support code for the objects and listeners.  i.e. Utility code unique to this scope.

If on the other hand you’re trying to build a hierarchical system (not necessarily OOP) where there are multiple object types which share common listeners, etc you could do this:

-- common.lua -- contains commonly used listener for this example. local common = {} common.onTouch( self, event ) if( event.phase == "ended" ) then print(self.myName) end return true end return common

-- builder1.lua -- Builds simple object and uses common.lua listener local common = require "common" local builder = {} builder.create( x, y, name ) local tmp = display.newCircle( x, y, 10 ) tmp.myName = name tmp.touch = common.onTouch tmp.addEventListener( "touch" ) return tmp end return builder

-- builder2.lua -- Builds simple object and uses common.lua listener local common = require "common" local builder = {} builder.create( x, y, name ) local tmp = display.newRect( x, y, 20, 20 ) tmp.myName = name tmp.touch = common.onTouch tmp.addEventListener( "touch" ) return tmp end return builder

-- main.lua -- Simple example bringing it all together local builder1 = require "builder1" local builder2 = require "builder2" builder1.create( 100, 100, "Bill" ) builder2.create( 100, 150, "Bob" ) builder1.create( 200, 100, "Susie" ) builder2.create( 200, 150, "Sally" )

Hi, thanks!

My intension is to separate different  type of code in separate files in order to get it more easy to work with in the long run, gui as one type, scripts as another and so on. 

The example may be  what I am looking for. I have also been looking at dofile and this also seems to be a method to execute code put in separate files.

My Corona experience is only some hours… but I like it so far.

Hi again!

See below. Now I got working:

gemensamma.ReadSerialNummer(), great!

But I also require ui.sida2 where I define the GUI… BtnKamera is NIL and the insert to a displaygroup isn’t workning. Why?

-- -- main.lua -- ----------------------------------------------------------------------------------------- -- INCLUDE & GLOBALER local globals=require("script.globals") require "ui.sida1" ----------------------------------------------------------------------------------------- --local sida2=require("ui.sida2") local gemensamma=require "script.gemensamma" --MAIN gemensamma.ReadSerialNummer() --working! local AppSidaGrupp1 = display.newGroup() print(BtnKamera) -- BtnKamera = nil, isn't working --AppSidaGrupp1:insert( sida1.BtnKamera ) --isn't workning.

Code in ui.sida1, where BtnKamera is defined. Why are these controls not accessable from main.lua?

display.setDefault("background" , 1, 115/255, 0 ) BtnKamera = display.newImage("cam.png",140,60) BtnKamera:addEventListener( "touch", TaEnBild ) BtnArkiv = display.newImage("arkiv.png",250,60) BtnArkiv:addEventListener( "tap", HamtaEnBild )

forget the last post… I have now got it to work…  :smiley: