Classes

Forest Village introduces concept of Classes for lua. Is scripted in class.lua, file is inside "scripts/system folder" (you need to extract scripts.pak, see Package to know how to do it)

It is very similar for classes from any other programming language. Use this syntax to define custom class:

A = Class( "A" ) --// Define A class with class-name "A" function A: init(arg1, arg2) --// This is constructor function. --// It has been called at object's creation. --// It can has one or several arguments. end function A: fooBar(arg) --// Regular function of class. end

Than you can create instance of this class and use it functions:

local instance = A( 1, 2 ) instance:fooBar( 3 )

Note that in both cases (define and call function) a semicolon character are used (instead of dot in some lua examples). The use of consistent notation is highly important to proper passing arguments. The difference is that with semicolon form instance will be passed to function along with other arguments. It is passed as hidden argument «self». You can access it in function body to call other functions or pass futher:

function A: func(arg1, arg2) globalFunction( self, arg2) self :fooBar(arg1) end

Moreover, class instances can behaves like normal lua table. Create and read any custom fields if you need it for some reason:

self .customField = 44 self .customData = { name = "data" , value = 44 } print ( self .customData.value)

Inheritance
Classes supports inheritance. Check this out:

A = Class( "A" ) function A: function_a print ( "function_a" ) end B = Class( "B", A) --// new class B that inherits A function B: function_b self :function_a -- call inerited function end

You can get base class of variable or class by «base» field. It is often useful if you override some function in inherited class but still want to call base version of function:

function A: fooBar(arg) print ( "A" ) end function B: fooBar(arg) B.base.fooBar( self, arg) --// call original A version of fooBar* print ( "B" ) --// new logic end

* Note that in this case you are forced to use dot character and explicit pass self as first argument. This is one of the rare exceptions when semicolon syntax is imposible.

__INDEX__ __NEWSECTIONLINK__