Page 1 of 1

(SOLVED) Objects of the same class are getting changed to the same object

Posted: Sun Feb 06, 2022 7:06 pm
by thedarkloon
Hi there!
So, I have function call to construct a building on a selected tile. Each tile has an entity variable, and an entity can be initialized as a building (as a subclass of entity). However, when I build a building on a selected tile, all previous buildings turn to the building type I choose.

Say we have two houses (first picture), then place a church (second picture). The houses get overwritten!
https://love2d.org/imgmirrur/3AkfGD5.html

It won't let you overwrite houses if you try to build in a spot that already has a building, so I guess that's good. In addition, tiles that have been previously selected - but not built on - are not affected.

I have to assume I'm doing something wrong with middleclass: https://github.com/kikito/middleclass.

Re: (Middle Class) Objects of the same class are getting changed to the same thing

Posted: Sun Feb 06, 2022 7:20 pm
by dusoft

Code: Select all

if tileIndex.sprite ~= nil then
				love.graphics.draw(tileSetSheet, tileIndex.sprite, x, y)
				if tileIndex.entity ~= nil then
					love.graphics.draw(tileIndex.entity.sprite, x, y)
				end
			else
				love.graphics.draw(tileSetSheet, TileQuads['E'], x, y)
			end
What's exactly you are doing here? First you draw a sprite (default tile, e.g. grass?), then you draw an entity over, if it exists?

Re: (Middle Class) Objects of the same class are getting changed to the same thing

Posted: Sun Feb 06, 2022 7:23 pm
by thedarkloon
Yes. On second though it'd probably be better to not nest the entity draw. Thanks for pointing that out!
You'll find a lot of this code is... not great. Hence the buildings all changing. :rofl:

Re: Objects of the same class are getting changed to the same object

Posted: Mon Feb 07, 2022 8:20 pm
by thedarkloon
Solved it! For some reason, building being a subclass of entity made it to where a new building call affected all buildings. By separating building and making it its own class and putting it in the same file as its data, the buildings are now their own objects.