getting properties from objects

How can I get properties from an object in Codea? And, additionally, how can I define my own for it?
Let’s say I have two ellipses and I want to reuse the position of the first, on the second one.

ellipse(0,0,100)
ellipse(?,?,200)

In Gideros Mobile, for example, I would do something like:

<pre lang="lua>local ellipse1 = ellipse( 0, 0, 100 ) local ellipse2 = ellipse( ellipse1:getX(), ellipse1:getY(), 200 ) ``` PS: I know I could make variables hold the position and assign them to both ellipses, but I would like to read them as properties directly from the object itself. I want to be able to <b>speak</b> directly to each object! But as far as I can see, the ellipse function doesn't return anything to be assigned to a variable and hold any props. Would I have to make a Class and make then instances of it?

Hello @se24vad. The short answer to your question is yes.

The ellipse function does not create an ‘object’.

You could use a Lua table to hold the data:

myCircle = {x = 10, y = 20, radius = 50}

Or you could use a Lua table that has been especially adapted to behave like an object in an OOP language, using Codea’s class() function:


Circle = class()

function Circle:init(x, y, radius)
    self.x = x
    self.y = y
    self.radius = radius
end

function Circle:draw()
    ellipse(self.x, self.y, self.radius)
end

function Circle:copy()
    return Circle(self.x, self.y, self.radius)
end

myCircle = Circle(10, 20, 50)
myOtherCircle = myCircle:copy()

function draw()
    background(0)
    myCircle:draw()
    myOtherCircle:draw()
end

Very good! Now everything makes sense to me. Just forgot to think outside the box :smiley: