[0.8.9][NK] Classes

johnycilohoklajohnycilohokla REGISTERED Posts: 283 Developer
edited December 2015 in Modding Tutorials
First I will explain the general concept of Class.

Classes are templates, used to group functions and store class/static variables.
Once the Class is defined it can then be used to create multiple instances.
Instances can store variables (local to the instance) and execute the functions defined in the Class.

Classes in Lua

While Lua does not have a class system by default, it makes it easy to implement one.

Lua Classes are based on tables and metatables, tables act as a storage for variables, while metatable are used to define the functions (assign class to the table) and override operators.

TUG has an advanced class system, that allows creating Subclasses and applying Mixins to Classes.

Classes in TUG

To define a class in TUG you will need to Subclass EternusEngine.Class, this Class provides a basic Subclass system, InstanceOf function and the functions required for Mixins.

You will also have to name your Class, the name is passed into the Subclass function as first parameter and has to be unique.

Example:
MyClass = EternusEngine.Class.Subclass("MyClass")

Subclasses

To define a subclass you will need to call the Subclass function on the Class you want to create a Subclass of and give it a unique name as the first parameter.

Example:
MySubclass = MyClass.Subclass("MySubclass")

Global variables

Example:
MyClass.MyVariable = 123

Constructor & Local variables

Example:
function MyClass:Constructor( args )
self.m_myVariable = 123
end

Functions

Example:
function MyClass:MyFunction( args )
-- code
-- self can be used to store/access local variables
return false
end
My Blog
jc.png
  yt.png  t.png  

Comments

  • RawrRawr REGISTERED, Tester Posts: 511 Seed
    Oh hey, quick query on the functions, can TUG's lua class's return all lua data types?(including tables and metatables?) and that "return false" part of the functions example is just arbitrary right? or do all TUG class functions need to at least return false?
    Programmer, designer, artist.
  • johnycilohoklajohnycilohokla REGISTERED Posts: 283 Developer
    Yeah any type, table, metatable, userdata(native object), string, number.
    There is no need for return value.
    You can also return multiple values, it's really flexible.

    Example:
    function MyFunction()
    return 1, "test", false
    end

    local num, str, bool = MyFunction()
    My Blog
    jc.png
      yt.png  t.png  
  • RawrRawr REGISTERED, Tester Posts: 511 Seed
    nice. :+1:
    Programmer, designer, artist.
Sign In or Register to comment.