Giving objects/tables a custom type

Is there an official way of typing objects/tables in Lua or Corona?

I was thinking of simply adding a “type” attribute to my display objects so I can identify what type of object was tapped. For example, was a player or enemy tapped. [import]uid: 52771 topic_id: 19274 reply_id: 319274[/import]

See “Data Types” here; http://blog.anscamobile.com/2011/06/understanding-lua-tables-in-corona-sdk/

The whole tutorial is very useful and wonderfully written.

Peach :slight_smile: [import]uid: 52491 topic_id: 19274 reply_id: 74358[/import]

Thanks! It’s an excellent tutorial but doesn’t seem to answer my question. I wanted to know if there was some way to do type checking.

I wanted to distinguish my enemy display objects from my friendly display objects. That way, I could perform different actions depending on what the user taps on.

In Flash, I would do this by creating an Enemy class and linking it to a enemy symbol in my library. Likewise, I would create a Player class and link it to the player symbol. Then, if the user clicks on an enemy, I can check it’s type with the [as3]is[/as3] keyword:

[as3]
if (clickedObject is Enemy) {

}
[/as3]

Based on how events all have a [lua]name[/lua] attribute, I’m guessing that it might be easiest to simply set a similar attribute on my objects and check for that when they’re tapped. [import]uid: 52771 topic_id: 19274 reply_id: 74360[/import]

Ah OK, well you’d add your objects to a table and using the data types (from the link) you could check like this;

[lua]if event.target.type == “bad” then
– do stuff
end[/lua]

In the above “type” would be a property you chose and added using obj.type = “whatever”.

Does that make sense?

Peach :slight_smile: [import]uid: 52491 topic_id: 19274 reply_id: 74378[/import]

Thanks, that confirms it. [import]uid: 52771 topic_id: 19274 reply_id: 74415[/import]

Excellent - best of luck with your project :slight_smile: [import]uid: 52491 topic_id: 19274 reply_id: 74481[/import]