288 posts
Posted 15 April 2015 - 04:17 PM
I have been wondering this for a long time, but how do you define a function that uses a semi-colon instead of a full stop?
2427 posts
Location
UK
Posted 15 April 2015 - 04:27 PM
I believe this is correct
--#this
function t.func(self,parms)
--code
end
--#is the same as this
function t:func(parms)
--code
end
--#if you want this to be something loadable in an api
function func(self, parms)
--code
end
you only want to do this if the function is in a table and needs to have access to something that is in its parent table, the parent table will be passed to it as the first parameter, if the function is declared as "t:func" then the table is named "self"
Edited on 15 April 2015 - 02:30 PM
3057 posts
Location
United States of America
Posted 15 April 2015 - 06:09 PM
A semicolon is this ( ; ), if you are talking about OOP you'd use a colon ( : ).
288 posts
Posted 15 April 2015 - 06:44 PM
Oops! Silly mistake! Yeah i mean a :
3057 posts
Location
United States of America
Posted 15 April 2015 - 07:31 PM
In that case, there's really no reason to do so unless you are using OOP. For example, what Lupas posted *will* work, but it has no additional functionality.
OOP looks something like this:
local animals = {
function eat( self, food )
self.foodBar = self.foodBar + food
end,
function say( self )
print( self.name )
end,
}
local function monkey()
return setmeatable( {name="monkey", food=10}, {__index = animals} )
end
local monkeyone = monkey()
monkeyone:eat( 1 )
monkeyone:say()
7083 posts
Location
Tasmania (AU)
Posted 15 April 2015 - 11:50 PM
It may help to mention that "self" refers to the table the function is in. So when calling, t:func(1,2,3) is simply shorthand for t.func(t,1,2,3).
288 posts
Posted 02 May 2015 - 03:54 PM
Thanks for that, I'm learning OOP so I can make cleaner code.