Capricorn 76
This Lua tutorial demonstrates the Object Oriented Class mechanism included with the ScriptENGINE. Use the IMisc:classDefine() and IMisc:classCreateInstance() functions to create and manage classes/objects. Lua is a very powerful scripting language. Lua has many advanced features, allowing us to do interesting things like incorporate OO design in our scripts.

The ScriptENGINE extends the basic Lua functionality to give developers all the power of Object Oriented Design using classes including:


--------------------------------------
-- Here's an example of how to use the IMisc class interface functions to create your own classes.
--------------------------------------
-- NB: It's assumed the reader has some understanding of OO design.

--------------------------------------
-- CMotorcycle.e76script
--------------------------------------

-- All new classes must be defined with the following line.
-- They can be defined in their own script files, or all together in one script.
CMotorcycle = IMisc:classDefine(nil); -- The 'nil' indicates that this class has no base class

--------------------------
-- Constructor is automatically called from IMisc:classCreateInstance()
-- when you create new instances of this class.
--------------------------
function CMotorcycle:__construct(make, color, engineCapacity)
    print('[CMotorcycle:__construct]');

    -- Init the motorcycle state
    self:init(make, color, engineCapacity);
end

--------------------------
-- Destructor is automatically called by the Lua garbage collector
-- when all references to the class instance are set to nil.
--------------------------
function CMotorcycle:__destruct()
    print('[CMotorcycle:__destruct]');
end

--------------------------
-- Initialise a motorcycle instance
-- We have made a utility function to initialise the state of the motorcycle.
-- This can be called manually to reset the state of the object.
--------------------------
function CMotorcycle:init(make, color, engineCapacity)

    print('[CMotorcycle:init]', make, color, engineCapacity);

    ------------------
    -- Check input params
    ------------------

    ------------------
    -- Intialise the motorcycle PROPERTIES
    ------------------
    self.make           = make;
    self.color          = color;
    self.engineCapacity = engineCapacity;
end

--------------------------
-- Other METHODS / BEHAVIOURS
-- These are lua functions that can be called on each motorcycle instance.
--------------------------
function CMotorcycle:getMake()
    -- 'self' is a lua keyword referring to this instance of the motorcycle class.
    -- (just like 'this' in C/C++).
    return self.make;
end

function CMotorcycle:getColor()
    return self.color;
end

function CMotorcycle:hasBigEngine()
    -- If the engine capacity is greater than 250cc
    -- then this motorcycle has a big engine
    return (self.engineCapacity > 250);
end


--------------------------------------
-- main.e76script
-- Startup state
--------------------------------------
function OnStateRun()
    -- Open the console to display our output
    IApp:showConsoleWindow();

    -- Create a new instance of the CMotorcycle class, with the given parameters.
    -- The params are passed directly to the __construct() function.
    local m = IMisc:classCreateInstance(CMotorcycle, 'Kawasaki', 'green', 600);

    if (m:hasBigEngine()) then
        print('This ' .. m:getMake() .. ' motorcycle has a big engine!');
    else
        print('This ' .. m:getMake() .. ' motorcycle has a small engine.');
    end

    -- Call destructor.
    m:__destruct();

    IKey:getch();
end

--------------------------------------
-- This would be our expected output....
--------------------------------------
-- This Kawasaki motorcycle has a big engine!

Copyright © 2006-23 Sep 2009 Capricorn 76 Pty. Ltd. (created on Wed Sep 23 16:49:11 2009)