Hi there!
I'm running into a problem - which I've come across multiple times before - but still am unsure how to 'solve'.
I was working on a basic economy game where you can buy up to 10 flats, and charge rent for the number of floors, there are 5 floors. I also implemented a mechanic where renters would occasionally, randomly leave an apartment/floor and you'd have to click a button to rent it out again to get rent.
There are also repairs involved, which start adding up when you purchase the building. Repairing costs money. If you don't repair, the renters start paying less money every x seconds until a minimum rent is reached. Finally, you can collect the rent. If you click on a building you get a menu with all the buttons and rent height, repair height and total rent height displayed, with a close button to close it for that building.
There are also visual signs for each building to warn you when can collect rent, repairs are overdue etc.
So you have building 1, 2, 3 etc. up until 10 (this image is just to give you an idea, didn't want to put the whole artwork here)
Problem: The code works perfectly for one building. But the challenge lies in efficiently reproducing the code for all the 10 buildings which all do the same thing. Earlier I thought to myself; why not copy the code and buttons for each building? But that turned out to be a hellish amount of mindkilling work, duplicating each button, global variable, and replacing everything in the code.
I figured it could be done more efficiently. So I started using instance variables for buttons. That reduced the amount of work largely, but I still found myself replacing and setting code for each specific building and its floors. My god.. and all kinds of quirky undesireable stuff is currently happening. In other words - it's not working and I was about to throw in the towel.
I'm not so sure what I am doing anymore, where to look for in the code to fix it, and I'm confident there's a way more efficient way of coding this. Here are some examples of how it looks now:
It would be awesome if someone could point me in the right way of how to handle this. Currently I've got about a 165 global variables, well, let's just say I firmly believe it can be done more efficiently, just don't know how.
I thank you much in advance!