OOP Understanding

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
Drakk
Prole
Posts: 2
Joined: Mon Feb 04, 2013 1:12 pm

OOP Understanding

Post by Drakk »

Hey everyone,
I'm new to love, and lua in general. I love it so far. :ultrahappy:
I read alot of topics on here about OOP, and this isn't a love specific question but it seems it's a common topic, and I have an issue.

I'm using Bart's class lib. Here ( removed license for the sake of code length )

Code: Select all

__HAS_SECS_COMPATIBLE_CLASSES__ = true

local class_mt = {}

function class_mt:__index(key)
    return self.__baseclass[key]
end

class = setmetatable({ __baseclass = {} }, class_mt)

function class:new(...)
    local c = {}
    c.__baseclass = self
    setmetatable(c, getmetatable(self))
    if c.init then
        c:init(...)
    end
    return c
end
I also created this small sound manager thing, to see if I'm using this correctly.

Code: Select all

--[[

@File: sound.lua
@Purpose:
--------------------------------------------------------

]]--

ZLove.Sound = {};
ZLove.Sound.internalObjects = {}; -- container full of sound objects
ZLove.Sound.Object = class:new();

function ZLove.Sound.Object:init( Source, sType, bPrecache )
	
	bPrecache = bPrecache or false;
	
	self.loveSource = {};
	self.bPrecache = bPrecache;
	self.bPlayed = false;
	
	if( type( Source ) == "table" ) then 
	
		-- if we precache, we load all the sounds in the table
		if( bPrecache ) then
			for _, v in ipairs( Source ) do
				table.insert( self.loveSource, { v, love.audio.newSource( v, sType ) } );
			end
		else
			local iRndIndex = math.random( 1, #Source );
			table.insert( self.loveSource, { Source[ iRndIndex ], love.audio.newSource( Source[ iRndIndex ], sType ) } );
		end
	else
		table.insert( self.loveSource, { Source, love.audio.newSource( Source, sType ) } );
	end
	
	table.insert( ZLove.Sound.internalObjects, self );
	
end

function ZLove.Sound.Object:play()
	self.loveSource[1][2]:play();
end

function ZLove.Sound:update( dt )
	
	-- now we can loop through self.internalObjects
	-- and control every sound object we've created
	
end
I use it like so:

Code: Select all

local nSound = ZLove.Sound.Object:new( "sound.ogg", "static", false );
nSound:play();
I keep all the created objects inside ZLove.Sound.internalObjects, this is so I can loop through the created objects and manage them. Such as deleting them when they're stopped and so on.. I assume i'm using this class lib correctly? Anything part of ZLove.Sound.Object will be 'inherited' by each object I create. Which to my understanding is a good thing, so i don't have to re-write code for each sound I make?

Sorry if I wasn't to clear. This all works, but I'm not sure if i'm using OO abilities correctly.

Thanks!
User avatar
sanjiv
Citizen
Posts: 88
Joined: Mon Feb 27, 2012 5:11 am

Re: OOP Understanding

Post by sanjiv »

I can't help, but this is exactly the kind of stuff I need to approach next. So now that I've posted here, it'll be easy for me to return to.

Related question: How does one go about choosing an OOP library? Is the following list kept up to date? https://www.love2d.org/wiki/Category:Libraries
scutheotaku
Party member
Posts: 235
Joined: Sat Dec 15, 2012 6:54 am

Re: OOP Understanding

Post by scutheotaku »

I'm not familiar with SECS in particular, but yes it appears that you are using OO correctly :)

As a side note, "table.insert" can be slow. So instead of this (for example):

Code: Select all

table.insert( self.loveSource, { v, love.audio.newSource( v, sType ) } );


...you may want to use something like this instead:

Code: Select all

self.loveSource[#self.loveSource + 1] = { v, love.audio.newSource( v, sType ) }

--table.insert(myTable, value)
--does the same thing as
--myTable[#myTable+1] = value
[not a plug>>>]It may be helpful for you to look at my LOVE project framework with OO: https://www.love2d.org/forums/viewtopic ... 38&p=73277 It is based on the 30log library, but you may find some of the functions and methods (particularly the ones in Entity.lua) useful to you :)
sanjiv wrote: Related question: How does one go about choosing an OOP library? Is the following list kept up to date? https://www.love2d.org/wiki/Category:Libraries
I suggest just that you just look over them and to go with the one that has the features you need and best fits your style. It's also worth looking at other users' comments, as a few of the OO libraries have inheritance problems (though that may not be a big deal for your project).

MiddleClass ( https://www.love2d.org/wiki/MiddleClass ) and SECS ( https://www.love2d.org/wiki/Simple_Educ ... ass_System ) seem to be the most popular choices. I'm partial to 30log ( https://www.love2d.org/wiki/30log ) as it's really simple and easy to use, yet is very powerful (to see how easy it is, see "quick tour" here: https://github.com/Yonaba/30log/blob/master/README.md ).

Also, there's always the option of writing your own OO code. This should be a good start (assuming you know the fundamentals of Lua, that is): http://www.lua.org/pil/16.html

PS: If either of you need an explanation of OOP in general, here's a really good one: http://teknadesigns.com/what-is-object- ... ogramming/
Drakk
Prole
Posts: 2
Joined: Mon Feb 04, 2013 1:12 pm

Re: OOP Understanding

Post by Drakk »

scutheotaku, great post. Thank you, helped alot. :)
scutheotaku
Party member
Posts: 235
Joined: Sat Dec 15, 2012 6:54 am

Re: OOP Understanding

Post by scutheotaku »

Drakk wrote:scutheotaku, great post. Thank you, helped alot. :)
Great! I'm glad I could help :)
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 7 guests