Howto outsource functions / avoid repetition

Hi,

I’ve successfully created my first few games with Corona and now I would like to clean up my code. I’ve seen that many people repeat their code (also in the latest source of Ghosts vs Monsters for example, same code written twice in the level files) across many files…but how can we outsource the logic of our levels to one central logic-file?
My screens / levels always do the same stuff so I would like to implement one “logic.lua” file where those functions are called.
The primary stuff is going on in my ontouch-method where my images are dragged…I tried outsourcing the functionality but whenever I hand over the parameters (event.target, display.contentHeight, etc…) of the event, my logic method says that it receives nil values in some parameters…
So it seems that some parameters can’t be given to external methods…?

How can I outsource mostly all of the logical functionality so that my screens only contain variable parameters for the level design?

[import]uid: 11219 topic_id: 4691 reply_id: 304691[/import]

One example: I’m checking whether my draggable items are still on the screen:

screen1.lua:

local function onTouch( event )   
 local t = event.target  
 ....  
 ...  
 logic:checkIfCharacterIsOutOfBounds(t, display)  
....  
end  

logic.lua:

function checkIfCharacterIsOutOfBounds(t, display)  
  
 if(t.x \< 10 or t.x \>= display.contentWidth) then  
 transition.to(t, { time=500, x=200, y=200} )  
 end  
  
 if(t.y \< 10 or t.y \>= display.contentHeight) then  
 transition.to(t, { time=500, x=200, y=200} )  
 end  
end  

But that simply won’t work. t.x is always nil or a “table value” when handed over to logic.lua.
I also tried handing over the direct values (t.x, t.y, display.contentHeight, …) to logic.lua…also not working. Also not as array / table. Some parameters are always nil or not working.

The same function implemented directly in screen1 works fine, but not if I outsource it…

how can I do it? [import]uid: 11219 topic_id: 4691 reply_id: 14855[/import]

I think it has to be
[lua]function logic:checkIfCharacterIsOutOfBounds(t, display)
end[/lua]
assuming the table variable name is logic of course e.g local logic = {} [import]uid: 11334 topic_id: 4691 reply_id: 14857[/import]

Thanks for your answer, but I’m trying to outsource the function to a different file and then it doesn’t seem to work the way you suggested.
Your way would work if logic:checkIfCharacterIsOutOfBounds() would be placed in the same file, wouldn’t it?

But I have

screen1.lua
logic.lua

and as soon as ontouch() is called in screen1.lua, I’m trying to delegate the logical part to logic.lua
[import]uid: 11219 topic_id: 4691 reply_id: 14858[/import]

try changing your function to

checkIfCharacterIsOutOfBounds = function( t, display)

I seem to have better luck setting up functions that I call from other files this way [import]uid: 7911 topic_id: 4691 reply_id: 14861[/import]

Hi,

that isn’t working, either. For some reason t.x and t.y are always nil when I implement the method in logic.lua

The same method in screen1.lua (the same file) works like a charm… [import]uid: 11219 topic_id: 4691 reply_id: 14863[/import]

sounds like the scope of the variable [import]uid: 7911 topic_id: 4691 reply_id: 14865[/import]

OK I think I got it now. It doesn’t work to outsource single methods. I had to put the whole functionality of my screen.lua (everything from initializing graphics, adding event listeners, etc) into my logic.lua file.

So in my screens I’m only setting up the variable data in my new() method and put that data into an array / table. Then I hand that table over to the init() method of my logic.lua file.
And this(!) method has to be declared

function logic:init(data)

like newbie101 wrote before.

And I also have to re-initialize all of the global variables in logic.lua each time its init() method is called, otherwise logic.lua still keeps the data of the previous screen.
Thanks for your help, guys! [import]uid: 11219 topic_id: 4691 reply_id: 14866[/import]

Here’s the method I use to inherit methods from other classes:
http://developer.anscamobile.com/forum/2010/12/12/finding-objects-parent#comment-14209 [import]uid: 12108 topic_id: 4691 reply_id: 14879[/import]

Inheritance is quite easy (and powerful) in Lua…read the Lua docs on setmetatable and all of your objects can inherit functionality from a base class… [import]uid: 6175 topic_id: 4691 reply_id: 15173[/import]

I just started a new thread with explanations and example code of one simple approach you could take:
http://developer.anscamobile.com/forum/2010/12/31/object-oriented-sample-game-framework-corona [import]uid: 12108 topic_id: 4691 reply_id: 15221[/import]