How do you access information of the "owner" of a variable?

I imagine this is a fairly simple question, but I don’t know the exact terminology for what I am trying to accomplish so searching has proven fruitless.

Basically I have a table (let’s call it card). card has various keys such as card.name and card.id and card.image. These are easy enough to access when dealing with the table (card), but when one of these keys is passed in (for example card.image) how do you “backtrace” to access other information about card? I can think of a few very cumbersome workarounds but I am hoping there is a good way that I am unaware of.

As a further analogy to explain what I am thinking: imagine the table (card) as a directory that includes subdirectories (name, id, image), I am trying to understand how you move backwards to the main card directory once you are already in a subdirectory.

The closest terminology I could think of here is that of parent/child, however when I tried using parent to backtrace it did not work (this was on a card.image, which is a display object and serves as the event.target during a touch event, so I think it started searching for characteristics that didn’t exist in a display group). I would like to be able to (by using that event.target) access the name of the card to which that image belongs for example.

In lua tables you have to address the entire attribute path:

card.name

You could do:

local myName = card.name

and myName would hold the value of card.name, but its no longer part of the table card.  So there really is no navigation up and down the chain like there would be in JavaScript moving up and down the DOM using parent and child relationships.  You just have to specify the full path to the table member.

Now for your event.target question, you can simply add additional members to the table and then access them:

card.type = “king”

then in a function where event.target is the card in question:

event.target.type

@Rob

Thank you for the response. Just to clarify:

In the current situation I have, when a touch event occurs, the touchCard function has been passed card.image as a whole to serve as the event.target. If I gave card.image further members such as card.image.type like you suggested, I could access them easily, but this would result in a lot of redundant members, or an entirely separate table, neither of which is favorable. Is there really no way to access card and ultimately card.name via the passed in card.image in this scenario? One cannot use a member of a table to figure out what table it is in so to speak? 

Accidental post. Apologies.

Not really.

Why not just make card the image and add your other members to card?  I guess the other way is to have a table of card’s and when you create the image give the image a .index member that points to the table entry of it’s parent.

I see. Well I am not using card as the image because most cards most of the time will not be visible, but I need to keep track of the cards, their stats, and their location in each deck. I assumed having lots of invisible card images floating around would be inefficient, but perhaps that’s wrong?

I was also curious about this in a more general sense. I can think of a lot of situations where it would be useful to be able to refer to a parent. I’ve considered the referencing technique you mentioned and only discarded it because it seemed like a workaround more than a proper solution, but if these are the only options I may have to use that technique.

In lua tables you have to address the entire attribute path:

card.name

You could do:

local myName = card.name

and myName would hold the value of card.name, but its no longer part of the table card.  So there really is no navigation up and down the chain like there would be in JavaScript moving up and down the DOM using parent and child relationships.  You just have to specify the full path to the table member.

Now for your event.target question, you can simply add additional members to the table and then access them:

card.type = “king”

then in a function where event.target is the card in question:

event.target.type

@Rob

Thank you for the response. Just to clarify:

In the current situation I have, when a touch event occurs, the touchCard function has been passed card.image as a whole to serve as the event.target. If I gave card.image further members such as card.image.type like you suggested, I could access them easily, but this would result in a lot of redundant members, or an entirely separate table, neither of which is favorable. Is there really no way to access card and ultimately card.name via the passed in card.image in this scenario? One cannot use a member of a table to figure out what table it is in so to speak? 

Accidental post. Apologies.

Not really.

Why not just make card the image and add your other members to card?  I guess the other way is to have a table of card’s and when you create the image give the image a .index member that points to the table entry of it’s parent.

I see. Well I am not using card as the image because most cards most of the time will not be visible, but I need to keep track of the cards, their stats, and their location in each deck. I assumed having lots of invisible card images floating around would be inefficient, but perhaps that’s wrong?

I was also curious about this in a more general sense. I can think of a lot of situations where it would be useful to be able to refer to a parent. I’ve considered the referencing technique you mentioned and only discarded it because it seemed like a workaround more than a proper solution, but if these are the only options I may have to use that technique.