OOP help

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.
User avatar
Zilarrezko
Party member
Posts: 345
Joined: Mon Dec 10, 2012 5:50 am
Location: Oregon

Re: OOP help

Post by Zilarrezko »

I fixed the problem by not using the self thing, but I wanted to know why it didn't work...

Some starting stuff...

Code: Select all

tile = {
}
map = {
}

function tile.new(x, y, id, w, h)
	local self = {}
	self.x, self.y = x, y
	self.w, self.h = w or 32, h or 32
	self.id = id
	self.moused = false
	self.stoodUpon = false
	return setmetatable(self, tile)
end

Code: Select all

function mapStartClick(x, y, button)
	for k, v in ipairs(map) do
		if button == "l" and v.moused then
			if v.id == 1 then
				v.id = 2
			else
				v.id = 1
			end
		end
	end
end
^^^ this is the one that worked

vvv this one did not

Code: Select all

function map:startClick(x, y, button)
	if button == "l" and self.moused then
		if self.id == 1 then
			self.id = 2
		else
			self.id = 1
		end
	end
end
why did ^^^^^^ this one not work?

just to clearify, the "startClick" portion is like the function name, and the thing on the left side of the colon is the name of the table that I'm trying to access. Is it because I'm trying to access a value in the table "map" when they're all tables? I can't think of any other reason.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: OOP help

Post by Robin »

In the last function self refers to map. But you'd want to use one of the tiles, and map is a table of tiles (see: the for loop in mapStartClick). You could do:

Code: Select all

function tile:startClick()
   if self.moused then
      if self.id == 1 then
         self.id = 2
      else
         self.id = 1
      end
   end
end

function mapStartClick(x, y, button)
    if button == 'l' then
        for i, v in ipairs(map) do
            v:startClick()
        end
     end
end
HTH. :)
Help us help you: attach a .love.
User avatar
Zilarrezko
Party member
Posts: 345
Joined: Mon Dec 10, 2012 5:50 am
Location: Oregon

Re: OOP help

Post by Zilarrezko »

So then why don't I need to make a...

Code: Select all

function tile:draw()
      --yadayadayadayada
end


function mapDraw()
     for k, v in ipairs(map) do
           v:draw()
     end
end
but in my code I only need tile:draw and don't need mapDraw at all?
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: OOP help

Post by Robin »

Huh? I don't understand.
Help us help you: attach a .love.
User avatar
Zilarrezko
Party member
Posts: 345
Joined: Mon Dec 10, 2012 5:50 am
Location: Oregon

Re: OOP help

Post by Zilarrezko »

I don't know why I need the function mapStartClick for tile:startClick to work. I have a tile:draw function, but It works fine without having to for loop into the map table.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: OOP help

Post by Robin »

I don't know what your code looks like. How do you call tile:draw() and does its definition contain a loop over map?
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: OOP help

Post by Roland_Yonaba »

As The Great Robin pointed out, it is hard to answer to that without knowing your actual implementation. At least, post your code.
But on the basis of OOP design principles, tile.draw, IMHO, should contain the logic to draw only one single tile. Otherwise, it will look confusing.
Then, if you dispose of a higher order class, which represents a collection of tiles (i.e a Map class for instance, Map.draw would normally loop over each single tile and call tile.draw). Or, you will have to write a utility function to draw all tiles, similar to your mapDraw function.
User avatar
ejmr
Party member
Posts: 302
Joined: Fri Jun 01, 2012 7:45 am
Location: South Carolina, U.S.A.
Contact:

Re: OOP help

Post by ejmr »

Zilarrezko wrote:I don't know why I need the function mapStartClick for tile:startClick to work. I have a tile:draw function, but It works fine without having to for loop into the map table.
Since you’re writing code for tiles and maps I would suggest looking at Karai17’s STI library. It is a well-written version of what it sounds like you’re trying to create, so reading through the code may help illuminate some object-oriented concepts with regards to tiles and such. Seeing his approach may answer some of your questions.
ejmr :: Programming and Game-Dev Blog, GitHub
南無妙法蓮華經
User avatar
Zilarrezko
Party member
Posts: 345
Joined: Mon Dec 10, 2012 5:50 am
Location: Oregon

Re: OOP help

Post by Zilarrezko »

ejmr wrote:Since you’re writing code for tiles and maps I would suggest looking at Karai17’s STI library. It is a well-written version of what it sounds like you’re trying to create, so reading through the code may help illuminate some object-oriented concepts with regards to tiles and such. Seeing his approach may answer some of your questions.
uh.... nah I'm pretty much more confused now than before I opened that.

Can never look at someone else's code without getting a headache.

And no, it's not what I'm going for(to be blunt, sorry). Its façade would look that way, but I plan to not use tiled, or any map source, or almost any source of the matter (except maybe to do shaders... but that's a WHOLE nether forum post for me, just like this one).
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 32 guests