That seems a little harsh, but I agree that the colon isn't necessary here because you are expecting other modules to use your global variable to call the functions, rather than passing a reference different objects around. So you really don't need a reference to your object implicitly passed to each function call.
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
- Code: Select all
-- Completely equivalent:
function object:func(a, b, c, ...)
-- do stuff
end
object.func = function(self, a, b, c, ...)
-- do stuff
end
-- Also completely equivalent:
object:func(x, y, z)
object.func(object, x, y, z)
What we are talking about is the difference shown by this example, using the default API:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
- Code: Select all
-- Yes, "minetest" is an object. However, since we always use the same object to make these calls,
-- we don't need to pass a reference to it around:
local playerlist = minecraft.get_connected_players()
local status = minecraft.get_server_status()
local inv = minecraft.get_inventory({ type = "player", name = "myname"})
-- However, "inv" here points to some inventory (InvRef) object, and when we call
-- a method on that object the method HAS to know WHICH inventory it is dealing with!
local empty = inv:is_empty()
So what you are talking about is probably the first case where you simply call "mymodule.myfunction(...)".
On the other hand, if you want a function that is ONLY callable within the same file (NOTE: not the entire module, but the same actual *.lua FILE!), you want:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
- Code: Select all
local my_func = function(a, b, c)
-- do stuff
end
my_func(x, y, z)
Which keeps it from conflicting with any other module that might use a function, variable, or object called "my_func".