0
\$\begingroup\$

I've been stuck doing this in a very inefficient way and really want to know if there's some design pattern that solves this because it seems like a pretty common problem to have.

I have a system where I have a "world" class then that class has a list of town objects then the town has a list of buildings and buildings have a family. It's simple to for example, know what buildings a town has since the towns have all buildings in a list but what I always end up needing is a way for the building to know what town it is in for example. This can be done by giving buildings a town variable to keep that saved but I have to keep making sure that gets set properly for every stage in the chain which makes a mess of the code. It also messed up saving cause it saved a building in the town's building list but when saving the buildings data, had to save the town's data again since the building has that variable, creating a loop.

I've also tried making static methods that can be reached anywhere that say, a building can use and it will search through all towns until it finds where the building belongs, but that seems very inefficient to me and I've heard the same from others.

Lastly I've tried giving all of these things an ID but that still makes them have to search what "parent" has their ID.

Is there some way of doing this a more correct way? I have a hard time finding good search terms for it so I finally decided to just ask for guidance.

\$\endgroup\$
  • 1
    \$\begingroup\$ Can you give an example of the kind of operation you're doing where a building needs to know its town? Can we gather the needed info from that operation context? For example, if we're doing an iterative loop over all buildings in a town, we could save a "current" town as part of that iteration, and pass it to the building when we call its update method. \$\endgroup\$ – DMGregory Apr 9 at 18:52
  • \$\begingroup\$ @DMGregory Actually what you suggested about passing it in the update method would solve my problem pretty well, I'll go with that and see if it leads to any issues. An example of what I could end up wanting to do is for a character (that is within a family, that is within a building, which is inside a town, inside a country, within a world) to figure out what kind of buildings are in the same town as them for example. \$\endgroup\$ – Taceo Apr 9 at 19:02
  • \$\begingroup\$ Your savegame problem seems to be a design problem with your saving system. Perhaps it needs some way to mark variables as "transient" so they don't get saved. \$\endgroup\$ – Philipp Apr 9 at 19:04
  • \$\begingroup\$ @Philipp I know how to make it not save those variables as to avoid the loop but that would mean that whenever I load a game, I would need to run a method to set those variables manually which I could do, but it just feels wrong and inefficient to me and I would love to know if there's a smarter way. \$\endgroup\$ – Taceo Apr 9 at 19:07
  • \$\begingroup\$ What do you mean by "but I have to keep making sure that gets set properly for every stage in the chain which makes a mess of the code"? I'd assume buildings will be assigned to only one city for the life of the application. \$\endgroup\$ – andrew zuo Apr 9 at 20:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.