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

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
thedarkloon
Prole
Posts: 7
Joined: Sat Feb 05, 2022 12:46 am

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

Post 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.
Attachments
tile.lua
(5.39 KiB) Downloaded 75 times
entity.lua
(1.42 KiB) Downloaded 72 times
buildings.lua
(3.14 KiB) Downloaded 80 times
Last edited by thedarkloon on Mon Feb 07, 2022 8:21 pm, edited 2 times in total.
User avatar
dusoft
Party member
Posts: 635
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

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

Post 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?
thedarkloon
Prole
Posts: 7
Joined: Sat Feb 05, 2022 12:46 am

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

Post 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:
thedarkloon
Prole
Posts: 7
Joined: Sat Feb 05, 2022 12:46 am

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

Post 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.
Post Reply

Who is online

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