Skip to content
This repository has been archived by the owner on Jan 2, 2021. It is now read-only.

Properties

Brad Sharp edited this page Nov 26, 2016 · 2 revisions

Properties are required to remain whatever type they are defined as, tables, userdatas and variable types should be defined using accessors.

local class = require'class'

local planet = class {
    Name = "";
    Magnitude = 0;
}

Accessors are useful when you wish to store a table, userdata or value of unknown type. The get function is called when the user wants to retrieve the value of the variable. The set function is called when the user attempts to set the value of the variable. Both are given the object as the first parameters, while set is also given the value the user wishes to set your variable to.

local class = require'class'

local planet = class {
    Radius = 0;
    SurfaceArea = {
        get = function (this)
            return 4 * math.pi * this.Radius * this.Radius
        end,
        set = function (this, value)
            this.Radius = math.sqrt(value / (4 * math.pi))
        end
    }
}
Clone this wiki locally