Inheriting code base and values

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Chewynouget
Prole
Posts: 4
Joined: Wed Apr 16, 2014 3:16 pm

Inheriting code base and values

Post by Chewynouget »

Hi there, I have a class with this set of values and functions:

Code: Select all

require("class")
entity = class:new()

function entity:new()
	self.x = 100
	self.y = 100
	self.width = 32
	self.height = 32
	self.info = "entity"
	self.alive = true
	self.color = {r = 255, g = 0, b = 0}
	return self
end
function entity:load()

end
function entity:update()
	if self.alive then

	end
end
function entity:draw()
	if self.alive then
		love.graphics.setColor(self.color.r, self.color.g, self.color.b)
	end
end
function entity:destroy()
	self.alive = false
end
And I want to be able to use those same functions and values simply for other classes, like so:

Code: Select all

require("entity")
player = entity:new()

function player:load()

end
function player:draw()
	love.graphics.rectangle("fill", self.x, self.y, self.width, self.height)
end
function player:update()
	self.x = self.x + 5
end
I come from a flash and As3 background, which if any of you know you may understand more or less what I'm trying to do.
So could anyone help me with this? All help is appreciated.
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: Inheriting code base and values

Post by Roland_Yonaba »

I saw this was posted a long while ago, and was never responded. I'll take a crack at that.
This fairly doable in Lua, but in your case, it roughly depends on the code inside the file class.lua.

Lua has no built-in OOP, it just provides mechanisms to model your own vision of OOP. As a result, there are different flavours of pseudo-oop, and most of the people tend to use libraries, or just write their own. So, did you basically use some available class librairy ? Or did you wrote your own ? In both cases, can you let us see that code ?

Depending on how classes are implemented, classes deriving from a superclass should inherit its attributes and methods. So if you have a classA where some method methodA is defined, your OOP scheme you use will normally let you use classB.methodA, assuming classB derives from classA. Normally.

In case you also have an instance from classA, namely instanceA, instanceA.methodA should be ready to use, aswell.
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 207 guests