How can I "require" this?

I am making a base defense game and I am storing data for my characters in one lua file, I need to access the information in the “units” and “sprites” table of this file:

local units = { local girl = {img = "sprites/girl.png", timeBetweenAtk = 2350} } local sprites = { local girlsprites = { local walkingsprite = { local walkingGirlSheetData = {width = 309, height = 494, numFrames = 10, sheetContentWidth = 1545, sheetContentHeight = 988} local walkingGirl = graphics.newImageSheet("sprites/walking-girl.png", walkingGirlSheetData) local walkingGirlSequenceData = {name = "walking", start = 1, count = 10, loopCount = 0, loopDirection = "forward"} local attackingsprite = { local meleeGirlSheetData = {width = 385, height = 477, numFrames = 10, sheetContentWidth = 1925, sheetContentHeight = 954} local meleeGirl = graphics.newImageSheet("sprites/melee-girl.png", meleeGirlSheetData) local meleeGirlSequenceData = {name = "melee", start = 1, count = 10, loopCount = 0, loopDirection} } } } }

I tried this:

local characters = require "characters" print(characters.units[1].timeBetweenAtk)

But it did not work. Thanks in advance!

You don’t need to “require it” as in your code it is already declared.

The code is in a different file though. The second code block I put was in a different file than the first. Sorry, I did not specify that.

Various things are wrong here. Firstly, you don’t put a local before tables that are inside tables. 

[lua]

local units = {

  girl = {img = "sprites/girl.png, timeBetweenAtk = 2350}

}

[/lua]

Secondly, in the above example, units[1] doesn’t exist anyway. You have defined units[“girl”] or units.girl.

Thirdly, in order for this data to be accessible in the scene that required it, the characters.lua module needs to return something. Here’s an example with blank tables.

[lua]

local m = {}

m.units = {}

m.sprites = {}

return m

[/lua]

You can then access these via character.units and character.sprites.

Finally I’m not sure I like the structure you’re going for with the sprite table. The aim should be that you don’t need to know the names of the various tables for any given character. For example, when you later try to create a boy character, you’ve got to specifically access a table called walkingBoySheetData. Ideally you should be able to create a character just by referencing the type, i.e. “girl”, “boy”, “monster”, “rabbit”, and it will fetch the images, attributes and sprites it needs.

Generally though I do think you’re trying to run before you can walk. I’m not really one to talk (my first aborted attempt to make a football management game was when I was 9), but when I first discovered Corona I did at least start with little quiz games that gave me an insight into tables and how to use them efficiently, so when I started to build something more complex I didn’t paint myself into a corner right form the start.

How would I do this the way you described it?

Couldn’t agree more based on the volume of posts!!!

Hi.  I am not 100% sure what you’re trying to do here and the use of the word sprites in your structure is a bit of a misnomer, but one possible way to organize this data as a module is:

-- save in a file called "spriteData.lua" for this example... -- local walkingGirl = {} walkingGirl.data = { width = 309, height = 494, numFrames = 10, sheetContentWidth = 1545, sheetContentHeight = 988} walkingGirl.sheet = graphics.newImageSheet("sprites/walking-girl.png", walkingGirl.data ) walkingGirl.seq = { name = "walking", start = 1, count = 10, loopCount = 0, loopDirection = "forward"} local meleeGirl = {} meleeGirl.data = { width = 385, height = 477, numFrames = 10, sheetContentWidth = 1925, sheetContentHeight = 954} meleeGirl.sheet = graphics.newImageSheet("sprites/melee-girl.png", meleeGirl.data) meleeGirl.seq = { name = "melee", start = 1, count = 10, loopCount = 0, loopDirection } local m = {} -- I'm not clear what this is m.units = {} m.units.girl = {img = "sprites/girl.png", timeBetweenAtk = 2350} } -- You're not storing sprites in this module, but parts used to make sprites, so I'm changing the -- names a bit from your original request. m.parts = {} m.parts.walking = walkingGirl m.parts.melee = meleeGirl return m

 
Then you could make sprites as follows
 

local sd = require "spriteData" local meleeSprite = display.newSprite( sd.parts.melee.sheet, sd.parts.melee.data ) local walkingSprite = display.newSprite( sd.parts.walking.sheet, sd.parts.walking.data )

This example still sucks though… keep reading below.

I do think however that your original concept was too deep  and was combining unrelated data in a single module.
 
I would do this instead:
 

-- save in a file called "girlParts.lua" for this example... -- local m = {} m.walking = {} m.walking.data = { width = 309, height = 494, numFrames = 10, sheetContentWidth = 1545, sheetContentHeight = 988} m.walking.sheet = graphics.newImageSheet("sprites/walking-girl.png", m.walking.data ) m.walking.seq = { name = "walking", start = 1, count = 10, loopCount = 0, loopDirection = "forward"} m.melee = {} m.melee.data = { width = 385, height = 477, numFrames = 10, sheetContentWidth = 1925, sheetContentHeight = 954} m.melee.sheet = graphics.newImageSheet("sprites/melee-girl.png", m.melee.data) m.melee.seq = { name = "melee", start = 1, count = 10, loopCount = 0, loopDirection } return m

Now assuming you had a similar boy set of art you could do this:

-- save in a file called "boyParts.lua" for this example... -- local m = {} m.walking = {} m.walking.data = { width = 309, height = 494, numFrames = 10, sheetContentWidth = 1545, sheetContentHeight = 988} m.walking.sheet = graphics.newImageSheet("sprites/walking-boy.png", m.walking.data ) m.walking.seq = { name = "walking", start = 1, count = 10, loopCount = 0, loopDirection = "forward"} m.melee = {} m.melee.data = { width = 385, height = 477, numFrames = 10, sheetContentWidth = 1925, sheetContentHeight = 954} m.melee.sheet = graphics.newImageSheet("sprites/melee-boy.png", m,melee.data) m.melee.seq = { name = "melee", start = 1, count = 10, loopCount = 0, loopDirection } return m 

Then you could make sprites as follows
 

local girlParts = require "girlParts" local boyParts = require "boyParts" local meleeGirl = display.newSprite( girlParts.melee.sheet, girlParts.melee.data ) local walkingBoy = display.newSprite( boyParts.walking.sheet, boyParts.walking.data ) -- Yes... I made a little joke there too.

Now I would take this one step further.  Only expose parts you need like this:

 

-- save in a file called "girlParts.lua" for this example... -- local m = {} m.walking = {} local data = {width = 309,height = 494,numFrames = 10,sheetContentWidth = 1545,sheetContentHeight = 988} m.walking.sheet = graphics.newImageSheet("sprites/walking-girl.png", data ) m.walking.seq = { name = "walking", start = 1, count = 10, loopCount = 0, loopDirection = "forward"} m.melee = {} local data = {width = 385,height = 477,numFrames = 10,sheetContentWidth = 1925,sheetContentHeight = 954} m.melee.sheet = graphics.newImageSheet("sprites/melee-girl.png", data) m.melee.seq = { name = "melee", start = 1, count = 10, loopCount = 0, loopDirection } return m

Repeat this small change for boyParts.lua

Then you could make sprites as follows
 

local girlParts = require "girlParts" local boyParts = require "boyParts" local meleeGirl = display.newSprite( girlParts.melee.sheet, girlParts.melee.data ) local walkingBoy = display.newSprite( boyParts.walking.sheet, boyParts.walking.data ) -- Yes... I made a little joke there too.

_ girlParts.lua _   oh the connotations that spring to mind…

See  I made a little joke. :slight_smile:

local boyParts = require "girlParts"

Thanks! I am going to be doing this with dozens if not hundreds of sprites.

>> How would I do this the way you described it?

No offense intended, but your (many) questions rarely get “good” replies, because they’re rarely “good” questions…

Your questions are of the paraphrased form “here is some random code, that won’t even compile, let alone do anything useful, how can I get it to function properly, but in a completely different way?”

I myself usually won’t even bother reading such questions, because most could be answered by simply referring to existing docs, and if the OP can’t be bothered to do that, then I can’t be bothered to do it for them.  Either…

  1.  for Corona-specifics, the Corona docs

  2.  for Lua-specifics, either the reference, the PIL or the wiki

(plus, all of those more-formal resources will be more thorough than anything you’ll get as a forum response)

for instance, here’s everything you should need to know re your original implied question “how to make/use a module”

http://lua-users.org/wiki/ModulesTutorial

I’ll be keeping this in mind when writing my questions. (from now on)   :slight_smile:

You don’t need to “require it” as in your code it is already declared.

The code is in a different file though. The second code block I put was in a different file than the first. Sorry, I did not specify that.

Various things are wrong here. Firstly, you don’t put a local before tables that are inside tables. 

[lua]

local units = {

  girl = {img = "sprites/girl.png, timeBetweenAtk = 2350}

}

[/lua]

Secondly, in the above example, units[1] doesn’t exist anyway. You have defined units[“girl”] or units.girl.

Thirdly, in order for this data to be accessible in the scene that required it, the characters.lua module needs to return something. Here’s an example with blank tables.

[lua]

local m = {}

m.units = {}

m.sprites = {}

return m

[/lua]

You can then access these via character.units and character.sprites.

Finally I’m not sure I like the structure you’re going for with the sprite table. The aim should be that you don’t need to know the names of the various tables for any given character. For example, when you later try to create a boy character, you’ve got to specifically access a table called walkingBoySheetData. Ideally you should be able to create a character just by referencing the type, i.e. “girl”, “boy”, “monster”, “rabbit”, and it will fetch the images, attributes and sprites it needs.

Generally though I do think you’re trying to run before you can walk. I’m not really one to talk (my first aborted attempt to make a football management game was when I was 9), but when I first discovered Corona I did at least start with little quiz games that gave me an insight into tables and how to use them efficiently, so when I started to build something more complex I didn’t paint myself into a corner right form the start.

How would I do this the way you described it?

Couldn’t agree more based on the volume of posts!!!