WeaverOOP - A budding OOP library

Showcase your libraries, tools and other projects that help your fellow love users.

If expanded upon, might you use this?

Yes
0
No votes
Maybe
3
25%
No
9
75%
 
Total votes: 12

User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: WeaverOOP - A budding OOP library

Post by Robin »

LuaWeaver wrote:But how do the metatables work with this? That's what I need to know.
This: the __index metamethod.

The basis is simple:

Code: Select all

SomeClass = {}
function SomeClass:foo()
    print(self.name)
end

SomeInstance = {name = 'blah'}
setmetatable(SomeInstance, {__index = SomeClass})
SomeInstance:foo() -- prints blah
Pretty much every Lua OOP worth its salt boils down to this.
Help us help you: attach a .love.
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: WeaverOOP - A budding OOP library

Post by Roland_Yonaba »

LuaWeaver wrote:But how do the metatables work with this? That's what I need to know.
I'll strongly recommend PIL, especially chapter 13. You will find useful explanations there.
LuaWeaver
Party member
Posts: 183
Joined: Wed Mar 02, 2011 11:15 pm
Location: Ohio, USA

Re: WeaverOOP - A budding OOP library

Post by LuaWeaver »

I've been working on it, and this is what I have so far.

main.lua

Code: Select all

require "WeaverOOP"

enity=class:new()
enity.name="Enity"
enity.maxHp=100
enity.hp=100
enity.mana=1000
enity.damage=10
enity.defence=1
enity.accuracy=75

function enity.speak(enity2)
	print("Hi! I'm "..self.name.."! I have "..tostring(self.hp).." hp out of "..tostring(self.maxHp).." left! I also have "..tostring(self.mana).." left!")
end

function enity.damage(enity2, target)
	for i=1, self.accuracy do
		if math.random(1, 100)==15 then
			target.hp=self.damage-target.defence
		end
	end
end

character=object.new(enity)

character:speak()

character:damage(character)

character:speak()
WeaverOOP.lua

Code: Select all

class={}
class.__index=class
object={}
object.__index=class

function class:new()
	local class2={}
	class2.__index=class
	return class2
end

function object.new(class2)
	local object2=class2
	object2.__index=class2
	return object
end
And I get this error message:

Code: Select all

main.lua:26 attempt to call method 'speak' (a nil value)

What did I do wrong D:?
"your actions cause me to infer your ego is the size of three houses" -finley
LuaWeaver
Party member
Posts: 183
Joined: Wed Mar 02, 2011 11:15 pm
Location: Ohio, USA

Re: WeaverOOP - A budding OOP library

Post by LuaWeaver »

Never mind, solved on a different forum.
"your actions cause me to infer your ego is the size of three houses" -finley
Post Reply

Who is online

Users browsing this forum: No registered users and 68 guests