0
\$\begingroup\$

I have this question since a while now. In a TRPG game like Fire Emblem or Advanced Wars (or even in something simpler like a chess game) with tiles and units, you need to store where each unit is in order to calculate the movement of the units, if there is already another unit within the path or just to display the unit correctly in the world. How do you think is the best way to store all this information?

I can think of a couple ways to do this but I'm sure there is a better way.

The easier way is to store a reference to the unit in the cell. I think this is not optimal cause sometime I'm forced to either go through all cells or have an additional array with the cells that contain an unit. Both of this solutions sound... primitive.

If you store the cell reference in the unit you still need an array with all cells in your map. Also, usually you need to know if a cell contains an unit, so again you have to go through all units to check if a cell contains an unit or not. It seams cheaper this way (since there is less units than cells) but still primitive.

Having the reference in both entities sounds like the cheaper way to access the info you need, but maintaining it is complicated and it looks like weird. I've read on some places that this is the way to do it, but I don't really think it's a good practice.

Maybe the best way is to have another class to do this work by having the reference in both directions and creating the methods you need to retrieve or update both references at the same time. But I'm not sure how...

What do you guys think??

Thanks you!!

\$\endgroup\$
4
  • \$\begingroup\$ "I've read on some places that this is the way to do it, but I don't really think it's a good practice" - then it sounds like you've discovered that different developers have different opinions about this, so hearsay from one online stranger or another might not be what you want to follow for your own game. 😉 For what it's worth, I'd agree that it's valuable to store both a tile coordinate with your unit, and your unit's presence in the tile grid. If you'd like help designing methods to use and keep these two pieces of data in sync, try editing your question to ask about that specifically. \$\endgroup\$ Commented Oct 5, 2021 at 20:24
  • \$\begingroup\$ Hi! I'm not looking for help to keep the tile and unit in sync, I already did it and it works. But while I was doing it, I just field weird because I had to shield myself a lot to avoid bugs where these two entities would get out of sync. So I though maybe I was doing something wrong or that I was missing some other better way to do it. Thank you!! \$\endgroup\$ Commented Oct 6, 2021 at 8:29
  • \$\begingroup\$ Ah. Your question said "But I'm not sure how..." so it sounded like that was a part you needed help with. Can you clarify then: do you still need help with something, or is this post an opinion poll about which implementations are preferred by whichever users happen to click this post? If the latter, I don't think you'll get any answer more authoritative than the ones you've already found and formed opinions of your own about. \$\endgroup\$ Commented Oct 6, 2021 at 11:50
  • \$\begingroup\$ I wasn't looking for an actual implementation as per se, but rather to know if I what I was doing is (conceptually) correct or if I was completely wrong and I should do it another way. From your comments I understand that what I'm doing is fine and it shouldn't be a bad practice if I ensure that I can keep these two references in sync. To clarify, about the "But I'm not sure how..." I meant if there is another way to do it (store the references outside the tile/unit themselves, e.g.). Sorry if I didn't make myself clear. \$\endgroup\$ Commented Oct 6, 2021 at 12:51

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.