require modules use

I know this is probably super easy for most, but I continue to struggle with using an external .lua file for simplification.

I recently got an error “more than 200 local variables”, so I would like to take segments of my code and put them in external files.  

Could someone help me with the easiest way to call function to and from parent to child files?

Please let me know if I need to clarify my thoughts.

Thanks, Dan

The easiest way of accomplishing this is to create a table in that external file of yours and add all functions you wish to call into said table. Then, at the end of the file, you just return the table.

For example,

file: hello.lua

local mod = {} function mod.greet( name ) print( "Hello, "..name ) end return mod

file: main.lua

local hello = require("hello") hello.greet( "Jack" ) --\> output "Hello, Jack"

Well having 200 variables in a given Lua file scoped to the entire file is a lot of variables… 

I ran into this with my first Corona game before I learned about modules, and I solved it with a basic Lua table trick.

If you have variables

local a, b, c, d, e, f, g

you can do

local t = {}

t.a = 0

t.b = 0

t.c = 0

t.d = 0

t.e = 0

t.f = 0

t.g = 0

You go from 7 local variables to 1 by using table’s to hold member variables. You can logically group these into tables that make sense.

For instance put all of your player related local variables in a table called player.  Put all of your enemy local variables in a table named enemies.

Once you’re there, the next step is to make functions that just work on the player, functions that just work on the enemy and then you have an easy path to create a player.lua and enemies.lua and move that code out of your lua file you’re having problems with and your life will get a lot easier.

I hate maintaining my first game because it’s a lot of scrolling in the editor, there is no code organization and to me represents what should not be done, but almost everyone new to Lua and Corona  will run in to.

Rob

Thank you Rob and XeduR…

Rob, you nailed it, my first completely unorganized game.  :mellow:

I’m not sure which method will work best here, maybe Rob’s.

Thanks again, wish me luck!

Both have their merits and should be used in tandem.

For instance, if, for some reason, you have 10 display groups, you could consider creating a table called  group and adding all those display groups in there so they are easier to keep track of and you also eliminate 9 local variables in the process. However, while they may be easier to keep track of, you should know that accessing table references is significantly slower than just accessing a local variable. Granted, the absolute difference in speed is so minuscule that this won’t matter for 99.99% of applications.

Now, creating external modules has several advantages over just stuffing everything into your scene files. Firstly, creating a lengthy functions in their own module file makes maintaining your code and debugging it easier, as well as reduce the overall clutter in the file where you require it.

Typically, there are functions that you’d want to take with you from one game/app to another. If you’ve written the function into a module, all you need to do is just copy the file and require it where you need it and you won’t need to worry about creating any new bugs. A lot of people take this a step further and create libraries, which are basically just modules that contain several similar functions, e.g. math library that contains math functions that the global math library lacks, like vector math.

I agree with XeduR@Spyric. It’s a process. It helps to use modules and is highly advisable to go that route, but an intermediate step is organizing your existing code so that you can modularize it. My suggestion gets rid of the specific error you’re getting, but it only half a solution. If you don’t go all the way, you’re still going to struggle with a huge long program.

Rob

I’m diving back in to a game I started about 8 years ago.  I know less now than I did then!  hahaha

I have lots of challenges to tend to, but this was a HUGE help!

I’ve implemented tables and that is a big help already.  Next will be making modules out of certain sections (menus, etc)

Thank you, both VERY much!

The easiest way of accomplishing this is to create a table in that external file of yours and add all functions you wish to call into said table. Then, at the end of the file, you just return the table.

For example,

file: hello.lua

local mod = {} function mod.greet( name ) print( "Hello, "..name ) end return mod

file: main.lua

local hello = require("hello") hello.greet( "Jack" ) --\> output "Hello, Jack"

Well having 200 variables in a given Lua file scoped to the entire file is a lot of variables… 

I ran into this with my first Corona game before I learned about modules, and I solved it with a basic Lua table trick.

If you have variables

local a, b, c, d, e, f, g

you can do

local t = {}

t.a = 0

t.b = 0

t.c = 0

t.d = 0

t.e = 0

t.f = 0

t.g = 0

You go from 7 local variables to 1 by using table’s to hold member variables. You can logically group these into tables that make sense.

For instance put all of your player related local variables in a table called player.  Put all of your enemy local variables in a table named enemies.

Once you’re there, the next step is to make functions that just work on the player, functions that just work on the enemy and then you have an easy path to create a player.lua and enemies.lua and move that code out of your lua file you’re having problems with and your life will get a lot easier.

I hate maintaining my first game because it’s a lot of scrolling in the editor, there is no code organization and to me represents what should not be done, but almost everyone new to Lua and Corona  will run in to.

Rob

Thank you Rob and XeduR…

Rob, you nailed it, my first completely unorganized game.  :mellow:

I’m not sure which method will work best here, maybe Rob’s.

Thanks again, wish me luck!

Both have their merits and should be used in tandem.

For instance, if, for some reason, you have 10 display groups, you could consider creating a table called  group and adding all those display groups in there so they are easier to keep track of and you also eliminate 9 local variables in the process. However, while they may be easier to keep track of, you should know that accessing table references is significantly slower than just accessing a local variable. Granted, the absolute difference in speed is so minuscule that this won’t matter for 99.99% of applications.

Now, creating external modules has several advantages over just stuffing everything into your scene files. Firstly, creating a lengthy functions in their own module file makes maintaining your code and debugging it easier, as well as reduce the overall clutter in the file where you require it.

Typically, there are functions that you’d want to take with you from one game/app to another. If you’ve written the function into a module, all you need to do is just copy the file and require it where you need it and you won’t need to worry about creating any new bugs. A lot of people take this a step further and create libraries, which are basically just modules that contain several similar functions, e.g. math library that contains math functions that the global math library lacks, like vector math.

I agree with XeduR@Spyric. It’s a process. It helps to use modules and is highly advisable to go that route, but an intermediate step is organizing your existing code so that you can modularize it. My suggestion gets rid of the specific error you’re getting, but it only half a solution. If you don’t go all the way, you’re still going to struggle with a huge long program.

Rob

I’m diving back in to a game I started about 8 years ago.  I know less now than I did then!  hahaha

I have lots of challenges to tend to, but this was a HUGE help!

I’ve implemented tables and that is a big help already.  Next will be making modules out of certain sections (menus, etc)

Thank you, both VERY much!