Reduce Code

Hello,

I am trying to reduce the amount of code I’m using. I have a lot of repetitive code that I would like to get rid of an use only once. I was thinking of adding the code in a document called extraCode.lua, then on every level calling it. But it does not work, there are images and functions. I just want to reduce my code, any ideas?

Thanks. [import]uid: 23689 topic_id: 19376 reply_id: 319376[/import]

If you want to reduce your code, make a function in another document. Then whenever you want to activate that function, call the function like callMe(). You have to remember to do require “extraCode” in the beginning of you file. Also make sure that your functions are not local.

Regards,
Jordan Schuetz
Ninja Pig Studios [import]uid: 29181 topic_id: 19376 reply_id: 74845[/import]

the best way to create “external” modules is this:

external.lua

 -- create a table that holds your functions  
local funcs = {}   
  
-- create functions like this:  
funcs.myAwesomeFunction1 = function ( param1, param2 )  
 local p1 = param1  
 local p2 = param2   
 local p3 = p1 + p2  
 return p3  
end  
  
funcs.myAwesomeFunction2 = function ( param1, param2 )  
 local p1 = param1  
 local p2 = param2   
 local p3 = p1 - p2  
 return p3  
end  
  
-- at the end of your module return the functions-table  
return funcs  

if you want to load the module into another module, do

-- require external.lua module  
myFunc = require ("external") -- without .lua  
  
-- call a function from external.lua  
local newNumber = myFunc.myAwesomeFunction1 ( 3,2 )  
-- newNumber is 5  

that is in short what is illustrated here
http://blog.anscamobile.com/2011/09/a-better-approach-to-external-modules/

hope that helps

-finefin
[import]uid: 70635 topic_id: 19376 reply_id: 74874[/import]