Gamemodes, Lua and Oriented Object Programming

I’ve learned a bit about Oriented Object Programming a while ago, but strangely in all the languages i use i’m still using linear programming, and that is, even in Gmod10.

If the language use Objects and classes, i can reasonably use the existing ones, but for example am unable to find reasons why i should create new ones. I do write a lot of what i call “helper functions” wich acomplish common tasks and, in general, avoid me to rewrite the same piece of code twice.

Would a lua guru or two care to show me one or two brief examples of code where on one side they used linear programming and on the other they used OOP and why they thing it’s better.

I tend to have a very practical aproach to learning languages and usually can’t learn something without being confronted to a specific problem, in the case of creating new classes, i never found a practical use to it.

Codes are approximate and may not be correct as I wrote them here without reference.
Linear
[lua]–…
if cider.item.stored[name] then
local item = cider.item.stored[name]
if item.spawnable then
cider.item.make(name,pos)
end
end
–…
function cider.item.make(name,pos)
if cider.item.stored[name] then
local item = cider.item.stored[name]
local ent = ents.Create(“cider_item”)
ent:SetModel(item.model)
ent:SetPos(pos)
ent:Spawn()
ent._Item = name
return ent
end
end
[/lua]
OOP
[lua]
–…
local item = GM.Items[name]
if not item and item.spawnable then return false end
local ent = item:Make(pos)
–…
function META:Make(pos)
local ent = ents.Create(“mshine_item”)
ent:SetModel(self.model)
ent:SetPos(pos)
ent:Spawn()
ent._Item = self.uniqueID
return ent
end[/lua]
I highly prefer OOP, which is why I have dumped using cider which is entirely linear and started making my own, which is as OO as I can make it.

I’m trying to understand it… so it’ some kind of item creation code but … aren’t sents already some kind of object anyway?

and what is “META” ?

on a sidenote i see on the first example that you make your functions as elements of a custom table… i’m gonna look into making this, it looks like an easier way to avoid collision with other functions than making convoluted names.

Meta here stands for a metatable. The most commonly used metatable is the “player” metatable which allows you to write functions which you would call as if they were pre-existing functions in the player library. For example:

[lua]local meta = FindMetaTable(“Player”)

function meta:RickRoll()
if SERVER then
self:SendLua(“LocalPlayer():RickRoll()”)
else
local htm = vgui.Create(“HTML”)
htm:SetSize(ScrW() , ScrH())
htm:OpenURL(“http://www.bringvictory.com”)
htm:StartAnimate(100)
end
end

[/lua]

I wrote that up quickly, so it’s probably wrong.

yeah, but in this case it’s a metatable applied to all the items tables. :3:

Ouchhh -_- i feel stupid… i guess i need to seriously read the lua handbook…

No shame in reading it, it’s got a wealth of information.
I bought a hard copy of it. :3:
[Link]