Class in Lua

I googled for lua reference and found Dota2 from valve is using source2 engine which has lua as their scripting language.

Their example includes

vengefulspirit_nether_swap_lua = class({})
 
--------------------------------------------------------------------------------
 
function vengefulspirit_nether_swap_lua:GetAOERadius()
	return self:GetSpecialValueFor( "radius" )
end
 
--------------------------------------------------------------------------------

https://developer.valvesoftware.com/wiki/Dota_2_Workshop_Tools/Lua_Abilities_and_Modifiers

Here vengefulspirit_nether_swap_lua is the file name and they are assigning it to class({}).
I have found the Class.lua file form a repo and guess they are using the same.

Can some one explain how are they implementing class structure using class.lua

class.lua (9.7 KB)

At a quick glance, they offer inheritance through polymorphism using meta tables.

If you create a Class and assign functions to it, you can pass that Class in to the constructor when you create a new one to use the first as “base”.

By default it seems to provide to string functions.

2 Likes

Inheritance

local base = class({})

function base:hit()
  print("hit base")
end

local super = class({}, {}, base)

function super:hot()
  print("super hot")
end

Usage

local super_instance = super()

super_instance:hit()
-- hit base
super_instance:hot()
-- super hot
4 Likes

Thanks for the reply. I have a question regarding the implementation

consider i am creating

gun.lua

local class = require("class")
-------------------------------------------------
local gun = class({})

self.totalAmmo =100
-------------------------------------------------
function gun:GetTotalAmmo()
   return self.totalAmmo
end 
-------------------------------------------------

Then in pistol.lua

local class = require("class")
local gun = require("gun")
-------------------------------------------------
local pistol = class(gun)
-------------------------------------------------
function pistol:PrintTotalAmmo()
 print(pistol:GetTotalAmmo())
end
-------------------------------------------------

Do it makes sense?
Will this work in defold or am i missing something here?
can i call these inside defold .script files?

PS: I am new and learning lua/Defold :neutral_face:

Self is actually only available on the instance, so you can’t set ammo like that.

Saw now also that the inheritance is third parameter and not first, so I edited my initial example.

local gun = class({
	constructor = function (self, ammo)
		self.ammo = ammo
	end
})

function gun:fire()
	self.ammo = self.ammo - 1	
end

-- notice that base class is third param.
local pistol = class({}, {}, gun)

function pistol:printAmmo()
	print(self.ammo)
end

function pistol:printCapacity()
	print(self.capacity)	
end

Usage

local instance = pistol(100)

instance:printAmmo()
-- 100

-- unless we set capacity from outside it will be nil
instance.capacity = 10

instance:printCapacity()
-- 10

Only the instance based values are shown if we print the instance. Functions and Constants associated with the actual class are kept in a metatable and only available when indexed.

pprint(instance)
-- {
--   ammo = 100
--   capacity = 10
-- }
3 Likes

thanks a lot