this or self equivalent?

Does Corona/Lua have an equivalent to this or self?

For example I want to make an object that moves itself. I created a function to create these objects.

Here’s what I have:

function Bug() 
 local b = display.newCircle(0, 0, 15)
 b.direction = math.random(math.pi\*2)
 b.deviation = math.random()
 b.speed = math.random(1, 3)
 b:setFillColor(255, 60, 0)
 return b
end

Here’s where I’d like to go:

function Bug() 
 local b = display.newCircle(0, 0, 15)
 b.direction = math.random(math.pi\*2)
 b.deviation = math.random()
 b.speed = math.random(1, 3)
 b.move = function() 
 this.x = this.x + this.speed
 this.y = this.y + this.speed
 end
 b:setFillColor(255, 60, 0)
 return b
end

[import]uid: 98652 topic_id: 19075 reply_id: 319075[/import]

you can do it like that:
[lua]function Bug()
local b = display.newCircle(0, 0, 15)
b.direction = math.random(math.pi*2)
b.deviation = math.random()
b.speed = math.random(1, 3)
b.move = function()
b.x = b.x + b.speed
b.y = b.y + b.speed
end
b:setFillColor(255, 60, 0)
return b
end[/lua] [import]uid: 16142 topic_id: 19075 reply_id: 73578[/import]

Thanks for the reply, that makes a lot of sense. [import]uid: 98652 topic_id: 19075 reply_id: 73621[/import]

Lua has the self command.

[code]

function example:new()
self.x = 100
self.angle = 100
end

[/code] [import]uid: 84637 topic_id: 19075 reply_id: 74766[/import]

I’m still a little confused as to the use of : in place the . is there any good explanation of this somewhere?

function example:new()
 self.x = 100
 self.angle = 100
end

Can you give me an example usage for this example? I would expect it to have a return self at the end?

I have been using the example above with good success so far. [import]uid: 98652 topic_id: 19075 reply_id: 74771[/import]

Say you had the following function.

You can declare sub methods from inside the function using self.

all adhering to the newBall class. (OOP (object oriented programming)

function newBall()  
 function self:destroy()  
 self:removeSelf()  
 end  
  
 function self:hide()  
 self.isVisible = false  
 end  
  
 return self  
end  
  
local ball = newBall()  
  
--Destroy it  
ball:destroy()  
  
--Hide it  
ball:hide()  

[import]uid: 84637 topic_id: 19075 reply_id: 74772[/import]

I guess that you would need “return self” at the end. I guess this is handled implicitly.

Looks like the : is used to call a nested function.

[import]uid: 98652 topic_id: 19075 reply_id: 74788[/import]

Correct, just updated my post [import]uid: 84637 topic_id: 19075 reply_id: 74791[/import]

Functions can have “self” passed to them.
local function something.myfunction(self, param1, param2, param3, etc)
Then you have self which is the object of type something passed to your function.

Lua provides a short cut:

local function something:myfunction(param1, param2, param3, etc)

In this format, “self” is implicitly provided.

This is probably one of the more frustrating features of Lua for people coming from other languages because this passing of “self” is hidden some of the time. In Javascript “this” is always passed and you don’t have an alternate syntax to explicitly pass it.

So If I write a function using the : method and you call it with the . method, parameters will be shifted by one and this causes a lot of headaches. [import]uid: 19626 topic_id: 19075 reply_id: 74818[/import]

Hey, Rob, this is an awesome explanation. Thank you! I never understood the difference between : method and . method – I’ve been simply (and blindly) using whichever the syntax that worked. (I know I should do more homework, but sometimes it’s hard to do it especially when it’s possible to get by without knowing what I should know…)

Naomi [import]uid: 67217 topic_id: 19075 reply_id: 76724[/import]