populating tables with a dictionary index

How to approach this?

I have seen the blog post on tables, read lua manuals, seen other forum posts and nothing has worked for me.

I want to create an object, and insert that object into a table not with a numerical index but with a named index.

I should be able to do this…
thecubeTable = {}

runALoop…
local thecube = mynewobject

assign physics, etc.

increment newName…

insert into table…

thecubeTable[newName] = thecube

or supposedly…

thecubeTable.newName = thecube

Neither one populates for me. I can provide additional information as to what I’m specifically doing if required. Just weary of banging my head against the wall.

[import]uid: 42417 topic_id: 16593 reply_id: 316593[/import]

The question is a bit vague to understand.

What is newName, a variable or a string literal.

eg. do you want to do

[lua]local theTable = {}
local theCube = newObject()

theTable[“Jayant”] = theCube
OR
theTable.Jayant = theCube[/lua]

or what you are after is
[lua]local theTable = {}
local myName = “Jayant”
local theCube = newObject()

theTable[myName] = theCube[/lua]

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 16593 reply_id: 62004[/import]

Here is my actual code:

[code]local squareCount = 0
spawnTableSquares = {}

local function spawnSquare()

squareCount = squareCount + 1

local square = display.newImage( “square.png” )

local squareName = “square”…squareCount

spawnTableSquares[squareName] = square

end[/code]

and it just doesn’t seem to work for me. If I try to print out even the table length, I get 0. It seems to look the way it should but I must be doing something wrong.

edit - I’m running spawnSquare on a timer so it just increments the squareCount every time and creates a new square (which works fine). [import]uid: 42417 topic_id: 16593 reply_id: 62009[/import]

@Hive,

first, you need to read these http://howto.oz-apps.com/2011/09/if-i-were-carpenter-would-you-buy-my.html, < a href=“http://howto.oz-apps.com/2011/09/tables-part-2.html”>http://howto.oz-apps.com/2011/09/tables-part-2.html to get an idea on how the table work, and the difference between an array and a Dictionary Object.

if the way that you are employing to check if the code is working or not is by determining the length using #, or table.maxn then you will always get a 0 for a non array table.

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 16593 reply_id: 62011[/import]

Thank you. I understand but the part that baffles me is that if I use a numeric index on the table, I can properly print the length. Seeing that I am storing the same object in the same table - I kind of think I’m doing something wrong for a named index.

I should still be able to get a length back no? At least that’s what all of the corona docs and external lua resources would have me believe. Tables should act like arrays if treated as such.

Still baffled but I must be close.

edit - I am reading your article now. I’m hoping it can shed some light as it looks good so far.

I have also tried inserting as…

local spawnTableSquares = {squareName = square}

to no avail.
i have been trying similar ways as shown in your previous response and it doesnt work for me. frustrating to say the least.
[import]uid: 42417 topic_id: 16593 reply_id: 62014[/import]

If you have read the article, then you must have understood this by now

Array => Numerical index ==> returns length

Dictionary => Named Index ==> returns length as 0

local a = {b=c} is *NOT* an array, it becomes a named index which is equivalent of saying

a[“b”] = c or a.b = c

so either you are missing something while reading or at the least when you are expressing your feelings on what you are after and *WHY* you suggest that it is not working for you.

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 16593 reply_id: 62299[/import]

Cool, thanks for staying with me. I’m going to test something and come back and post specifically in this regard. I shouldn’t have gotten stuck on length - it just confused things. I DO understand that part. Somehow my named references aren’t clicking for me but I’ll run a test and see how it goes for further clarification as to why my code might be off.
Ok…

When I try this:

local spawnTableTwo = {}  
  
local object = display.newImage("ship.png")  
  
spawnTableTwo["myName"] = object  
  
print(spawnTableTwo["myName"].x)  

It doesn’t return my x value (for example). Why not? I know it’s obviously my fault but I don’t understand why even that simple example doesn’t work. [import]uid: 42417 topic_id: 16593 reply_id: 62302[/import]

Instead of newImage, have you tired newCircle or newRect, the reason being if the iamge that you specify is not loaded, the object returned is NIL and therefore there cannot be a nil.x

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 16593 reply_id: 62307[/import]

I created the whole project for you rather than a code snip, so copy paste that from the other thread and run it and see, rather than trying to *adapt* it to your code.

If that works as is, you know that there is *NO* problem with the build you have, as you have suspected. That will narrow down the issue to something in your code, then you can spend time in trying to debug your code than a generic table does not work.

Do you get what I am suggesting? That will save you a lot of time and you will be trying to fix the issue than hitting everywhere even where there is no issue.

I have a video of the same that you can see *WORKING* with that sample at http://files.oz-apps.com/ArrayObjects.mov please have a look at it as it will be removed in the next 30 minutes.

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 16593 reply_id: 62310[/import]

I’m at a loss lol. It loads to the stage just fine. I’ll try another experiment and post back.

Eureka, sort of. How about that? I can only get it to work if I’ve added it as a physics body. That’s good for now but I’d sure like to not have some objects as physics bodies to get simple information about them back.

Any idea why this might be?

You have been uber helpful and I am grateful you took the time to help me out. [import]uid: 42417 topic_id: 16593 reply_id: 62308[/import]

Posted a response above, it’s working thank you but why does everything have to be a physics object? Maybe there is something that I’m missing in my display declaration to just create an object from an image on stage and get it’s current x val?

I mean, I’m all for physics but there will be times where I just need to manipulate some objects (via tables) that aren’t physics bodies. [import]uid: 42417 topic_id: 16593 reply_id: 62314[/import]