How to get the type of an object?

How to get the type of an display object?

Creating a string in an object table or using object._defined is not suitable.

Can you explain what you mean by type of display object?  Do you mean what sort of shape (circle vs. square or polygon) or do you mean type as in an object with a fill like [lua] 

local paint = {  type  = “image” , filename = “texture1.png”. }

object.fill = paint1

[/lua]

yes, sort of shape

The easiest thing to do in that case is to just define it when you create an object, i.e.

local object = display.newRect( 20, 20, 20, 20 ) object.type = "rect"

I don’t think that there is a specific way to get the display object’s “shape” as you want out of the box. If this is a big part of your game, you could just create a custom function that will always add these shapes when you create a new display object.

I understand this, but I would really like to have this function out of the box for:

display.newCircle()

display.newContainer()

display.newEmbossedText()

display.newEmitter()

display.newGroup()

display.newImage()

display.newImageRect()

display.newLine()

display.newMesh()

display.newPolygon()

display.newRect()

display.newRoundedRect()

display.newSnapshot()

display.newSprite()

display.newText()

I use the @Spyric method and tag everything manually.  It’s not too bad, just one line of code and it will become automatic after a while.  

@pahuchiy, I agree that it would be a nice addition.  Do program in C by any chance?  You could design that feature and add it to the Corona repository since it is now open source.  That would be pretty cool.

If you absolutely need them, all you can do is create your own module that does all of that. Or, as Rob frequently points out, Corona is open source and you can add such simple things to Corona yourself.

unfortunately I do not know how in C

There is a _type property that will distinguish some of these. (This was once documented.) You’ll need to poke around to get further, e.g. with ShapeObject s look for certain properties belonging to each  path.

Doesn’t _type simply identify the rect, polygon and circle all to be a shapeObject?

Inside the _properties , you can also find radius for circle , path for rect , and no path for polygon. But, if I remember correctly, these are just a part of a single string that needs to be decoded with json, which makes it quite an arduous process.

Right, that’s what my last sentence was getting at. You’d probe for stuff like radius for circle, x1 for rect, etc. With any luck there’s enough there to set everything apart.

A little module of this sort has been on my mind; in my case I’d like to be able to clone an object on the spot. (For completeness this also needs some native support for image sheets, paints, et al. I have a rough idea of what to change but not begun anything yet.)

Yea, this would be a good open source project for the community to take on.

Rob

You could just replace the original display.*** methods, forward the parameters to a cached original version and add your type info.

local newRectOriginal = display.newRect display.newRect = function( ... ) local r = newRectOriginal( ... ) r.type = "rect" return r end local r = display.newRect( 50, 50, 100, 100 )

how to change this code to remove _G.fOrig?

plugs = { display = {"Circle", "Container","EmbossedText","Emitter", "Group", "Image", "ImageRect", "Line", "Mesh", "Polygon", "Rect", "RoundedRect", "Snapshot", "Sprite", "Text"}, physics = {"Joint", "ParticleSystem"}, widget = {"Button", "PickerWheel", "ProgressView", "ScrollView", "SegmentedControl", "Slider", "Spinner", "Stepper", "Switch", "TabBar", "TableView"}, composer = {"Scene"}, graphics = {"ImageSheet", "Mask", "Outline", "Texture" }, media = {"EventSound", "Recording" }, } \_G.fOrig = {} for key, value in pairs(plugs) do if loadstring("return "..key)() then fOrig[key] = {} for i=1, #value do fOrig[key][i] = loadstring("return "..key..".new"..value[i])() loadstring( key..".new"..value[i]..[[= function( ... ) local f = fOrig.]]..key..'['..i..[[]( ... ) f.\_\_lib = "]]..key..[[" f.\_\_type = "]]..value[i]..[[" return f end]])() end end end plugs = nil