Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

what is the point of using table on functions? like this

Asked by 4 years ago
local Hi = {}

function Hi.Destroy()
----
end

or also

local Hi = {}

function Hi:ChangeValueToInfinite()
----
end

1 answer

Log in to vote
1
Answered by 4 years ago

doing:

local car = {
    start = function()
        print("starting car")
    end,


    move = function()
        print("moving at 20 MPH")
    end
}

is equivalent to doing:

local car = {}

function car.start()
    print("starting car")
end

function car.move()
    print("moving at 20 MPH")
end

so basically doing that is another way of defining a method of an object (or as you might call it a table in Lua)..

you can do object.X = Y;where X is a property name, and Y is the value of the property, but if you really think about it, there isn't the same syntax for methods without using function object.methodName().. which would only limit you to do:

object = {methodName = f()} or

object.methodName = f()

0
You forgot metamethods Ziffixture 6913 — 4y
0
@Fearhren this kinda tells all that.. Metamethods are declared just like other method, with underscores prefixing User#23252 26 — 4y
0
Not at all Ziffixture 6913 — 4y
0
@Fearhren you can define a metamethod like this: function MetaTable.__MetaMethod() User#23252 26 — 4y
Ad

Answer this question