• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Using 1.x style of programming in pre-1.x servers

Codex NG

Recurrent Flamer
Joined
Jul 24, 2015
Messages
2,994
Solutions
12
Reaction score
1,657
Although incomplete at the moment, i thought this would be a nice library to allow people developing
pre-1.x servers an opportunity to see that they too can write out their scripts in 1.x style of programming

Inspiration from this thread
https://otland.net/threads/about-metatables.240349/
Using @Non Sequitur, metatable example

I haven't quite placed all the metatables together.. eventually i will get around to it :p

All these functions are from an 8.6 server.
http://pastebin.com/MABQZWR6
 
You are using the same constructor and table for Player, Npc, Monster and Creature.
Code:
-- inherit all the metamethods of Creature
Player, Npc, Monster = Creature, Creature, Creature

No, this doesn't copy the table Creature to Player, Npc and Monster.

That could be a solution:

Code:
function newClass(constructor, parent)
    local methods = { }
    if parent then
        methods.__parent = parent
    end
    local MT = {__index = function(a, b) if methods[b] then return methods[b] elseif methods.__parent and methods.__parent[b] then return methods.__parent[b] end end}

    local call = function(self, ...)
        local obj = constructor(...)
        if not obj then
            return nil
        end

        return setmetatable(obj, MT)
    end

    return setmetatable(methods, {__call = call})
end

Creature = newClass(function(id)
    return {id = id}
end)

Player = newClass(function(id, guild)
    return {id = id, guild = guild}
end, Creature)

function Creature:getId()
    return self.id
end

function Player:getGuild()
    return self.guild
end


local player = Player(100, 5)
local creature = Creature(20)

print(player:getGuild(), player:getId())
print(creature:getGuild()) --  attempt to call method 'getGuild' (a nil value)

Like this you can define Player and "inherit" all methods from Creature.
 
Last edited:
Back
Top