I've had a look through the sdk doc and also a few examples but it's hard to find anything that explains what the simple class / instances do (but also specifically passing functions, arguments to and from).
As far as I know, the : is an instance and the . is a class (or table)?... but what is self, and what is super exactly?
So I haven't worked with classes/extends/super yet, but self and colon are about instances of tables and what we are passing in as "self."
functions are just code that can be anywhere, so you can give a table entry named "func_whatever" some code to execute and then call it like my_table.func_whatever().
But if we wanna operate ont he table values, it's good to have a reference to the table itself, so that's passed in as the first parameter and usually called "self".
The colon operator is shorthand for this standard and hides the self parameter.
obj = {
name = "unnamed",
x = 0,
y = 0
}
obj.func_standalone = function()
print("don't need an instance to call this. just call obj.func_standalone()")
end
obj.func_method = function(self)
self.x = 100
self.y = 100
print("set object '" .. self.name .. "' to position (100,100)")
end
function obj:func_another_method()
self.x = 50
self.y = 10000
print("set object '" .. self.name .. "' to position (50,10000)")
end
function test()
obj.func_standalone()
local obj_instance = table.deepcopy(obj)
obj_instance.name = "Henry"
-- calls func_method on the original obj table, so modifies obj to have 100,100 in x and y
obj:func_method()
-- calls func_method but passes in our instance, so operates on that instead
obj.func_method(obj_instance)
-- same as the call above - we're calling the func_method code and passing in our obj_instance as self
obj_instance.func_method(obj_instance)
-- same as the call above
obj_instance:func_method()
-- calls the second function on our instance
obj_instance:func_another_method()
--[[ output:
don't need an instance to call this. just call obj.func_standalone()
set object 'unnamed' to position (100,100)
set object 'Henry' to position (100,100)
set object 'Henry' to position (100,100)
set object 'Henry' to position (100,100)
set object 'Henry' to position (50,10000)
]]--
end```
omgosh thank you for taking the time to help me out in this. Helps me digest this in a way I can understand it, and hopefully anyone else that's a beginner and stumbling on this.
Besides what's been said, I'd like to point out that both operators : and . are native Lua operators, are nothing specific or custom of Playdate SDK. You may want to read some Lua docs, e.g.: