Concatenation problem

I have a table of units with their sprite information in it. I want to spawn the sprites, based on their id, but I get an error. I’ve tried looking on the Lua and Corona documentation, as well as in the forums and Stack Overflow, but I could not find a question related to this. 

The error I am getting is <name> expected near ‘…’:

characters. ..characters.units[k].id.. walking.sheet --Line with error

I don’t know how to fix the syntax. Thanks in advance!

Again, the problem is you’re metaphorically trying to build a car engine before you’ve learnt to build an electrical circuit.

All I can tell you is you can’t have ‘characters.’ and then a space. The error is telling you it is expecting a ‘name’, or in other words a ‘child’ of characters, but it finds a space, and then the ‘…’, neither or which it wants.

Thanks for this, Also, I do realize I have (a lot) more to learn, I just found out that the id I was trying to retrieve was nil.

Oops, it was not nil, just had a typo.

In the future if you do something like this and it doesn’t work:

characters. ..characters.units[k].id.. walking.sheet --Line with error

debug it on your own by verifying the parts:

-- Print the parts to make sure they are all valid print( "Parts", characters, k, walking ) -- Then start combining, one level at a time, till you find the problem print( "Combined", characters.units, walking.sheet ) print( "Combined", characters.units[k] ) print( "Combined", characters.units[k].id ) characters. ..characters.units[k].id.. walking.sheet --Line with error

Of course, if you have SSK2, it provides table utilities to help with this.

print( k ) table.print\_r( characters ) table.dump( walking ) characters. ..characters.units[k].id.. walking.sheet --Line with error

Finally, if this code is based on our other talk here: https://forums.coronalabs.com/topic/69092-how-can-i-require-this

Then the code I’m seeing makes no sense.  You can’t concatenate tables or display objects with

walking.sheet is a sprite sheet instance.

Oh. 

Also, this is my current code:

--characters.lua local m = {} m.units = { {img = "sprites/girl.png", timeBetweenAtk = 2350, name = "girl", cost = 75} } m.girl = {} m.girl.walking = {} local data = {width = 309, height = 494, numFrames = 10, sheetContentWidth = 1545, sheetContentHeight = 988} m.girl.walking.sheet = graphics.newImageSheet("sprites/walking-girl.png", data ) m.girl.walking.seq = {name = "walking", start = 1, count = 10, loopCount = 0, loopDirection = "forward"} m.girl.primaryAtk = {} local data = {width = 385, height = 477, numFrames = 10, sheetContentWidth = 1925, sheetContentHeight = 954} m.girl.primaryAtk.sheet = graphics.newImageSheet("sprites/melee-girl.png", data) m.girl.primaryAtk.seq = {name = "melee", start = 1, count = 10, loopCount = 0, loopDirection = "forward"} m.girl.idle = {} local data = {width = 271, height = 473, numFrames = 10, sheetContentWidth = 813, sheetContentHeight = 1892} m.girl.idle.sheet = graphics.newImageSheet("sprites/idle-girl.png", data) m.girl.idle.seq = {name = "idle", start = 1, count = 10, loopCount = 0, loopDirection = "forward"} return m 

--main.lua for k = 1, #imgSet do local identifier = characters.units[k].name local function spawnCharacter(event) if event.phase == "began" then if moneyCount \< characters.units[k].cost then print("insufficient funds") else moneyCount = moneyCount - characters.units[k].cost moneyText.text = moneyCount .. " / " .. moneyWallet local unit = display.newSprite(characters.identifier.walking.sheet, characters.identifier.walking.seq) end end return true end imgSet[k]:addEventListener("touch", spawnCharacter) end 

I was just trying to find a good way to spawn an individual unit if there are 10 to choose from.

I have tried concatenating the string into the image file, but I can’t use the data, because I can’t concatenate.

for i = 1, #m.units do identifier = m.units[i].name end function m.spawnUnit(identifier) local unit = ???? end

If this was for only two characters it would be easy, but I want this to work for dozens if not hundreds of characters.

I have one thing to say (based on all your posts)… walk before you run!

Start simple and when you completely understand then add more functionality bit by bit.

You are right, I think I have put too much on top of the building, instead of focusing on the base. 

Would you just happen to know where I can find the info for a way to do this?

“concatenation” is probably not the word.  seems the real topic s/b this access method:

characters[identifier].walking.sheet

instead of existing:

characters.identifier.walking.sheet

I did not think of that! That could actually work. I’m just having trouble adjusting my code.

Finally got it!

--characters.lua m.units = { {img = "sprites/girl.png", timeBetweenAtk = 2350, name = "girl", cost = 75, respawnTime = 3750, state = active} } m.girl = {} m.girl.walking = {} local data = {width = 309, height = 494, numFrames = 10, sheetContentWidth = 1545, sheetContentHeight = 988} m.girl.walking.sheet = graphics.newImageSheet("sprites/walking-girl.png", data ) m.girl.walking.seq = {name = "walking", start = 1, count = 10, loopCount = 0, loopDirection = "forward"} --main.lua local charToSpawn = characters.units[k] local identifier = charToSpawn.name if event.phase == "began" then if moneyCount \< charToSpawn.cost then print("insufficient funds") else moneyCount = moneyCount - charToSpawn.cost moneyText.text = moneyCount .. " / " .. moneyWallet local charWalkingData = characters[identifier].walking local unit = display.newSprite(charWalkingData.sheet, charWalkingData.seq) end end

Again, the problem is you’re metaphorically trying to build a car engine before you’ve learnt to build an electrical circuit.

All I can tell you is you can’t have ‘characters.’ and then a space. The error is telling you it is expecting a ‘name’, or in other words a ‘child’ of characters, but it finds a space, and then the ‘…’, neither or which it wants.

Thanks for this, Also, I do realize I have (a lot) more to learn, I just found out that the id I was trying to retrieve was nil.

Oops, it was not nil, just had a typo.

In the future if you do something like this and it doesn’t work:

characters. ..characters.units[k].id.. walking.sheet --Line with error

debug it on your own by verifying the parts:

-- Print the parts to make sure they are all valid print( "Parts", characters, k, walking ) -- Then start combining, one level at a time, till you find the problem print( "Combined", characters.units, walking.sheet ) print( "Combined", characters.units[k] ) print( "Combined", characters.units[k].id ) characters. ..characters.units[k].id.. walking.sheet --Line with error

Of course, if you have SSK2, it provides table utilities to help with this.

print( k ) table.print\_r( characters ) table.dump( walking ) characters. ..characters.units[k].id.. walking.sheet --Line with error