Page 5 of 5

Re: OOP help

Posted: Wed Feb 19, 2014 8:50 am
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.

Re: OOP help

Posted: Wed Feb 19, 2014 10:23 am
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. :)

Re: OOP help

Posted: Wed Feb 19, 2014 3:56 pm
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?

Re: OOP help

Posted: Thu Feb 20, 2014 8:43 am
by Robin
Huh? I don't understand.

Re: OOP help

Posted: Thu Feb 20, 2014 2:55 pm
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.

Re: OOP help

Posted: Thu Feb 20, 2014 3:34 pm
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?

Re: OOP help

Posted: Thu Feb 20, 2014 6:51 pm
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.

Re: OOP help

Posted: Thu Feb 20, 2014 7:09 pm
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.

Re: OOP help

Posted: Thu Feb 20, 2014 10:23 pm
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).