🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

How to manage the placement of units in cells and rules? (Code structuring)(C#)

Started by
3 comments, last by Alberth 4 years, 2 months ago

Hello everyone,

This is a rather simple problem and so I am hoping for some simple answers!

The game I'm currently developing has a cell grid that you can place units in with some rules governing whether or not you can place units into the cells. I have an array of Units, whereby the indices represent the cells. If a cell is occupied, the value corresponding to the cell index in the array should have the unit assigned or otherwise be null. An example of a placement rule is that units can't be placed into a cell that is already occupied.

Keeping in mind that the array should be kept up to date with regards to which cells are occupied, WHERE should I manage the positioning of the units into the cells when moved, the assignment of the units to the cell array and the placement rules?

My original thought was to have it all managed in a grid service, where I would just say, GridService.PlaceUnit(Unit, Cell). The grid service would check if it could place the unit in the cell, and if it could, it would position it there and then set the cell as occupied by the unit. However, the problem I see with this is that the grid service would be manipulating the position data of my unit externally which is apparently a no-no.

I have problems like this all the time, I don't know how to divide my responsibilities between classes.

Could someone please give me some guidance on how to solve these issues? Perhaps some principles? I'm desperate!

Cheers.

Advertisement

bazz_boyy said:
the problem I see with this is that the grid service would be manipulating the position data of my unit externally which is apparently a no-no.

Change the function signature to return the center of the cell on success, something like

bool GridService.PlaceUnit(Unit, Cell, out Vector2)

should do what you want. You can get the return value and on success just set the position of your unity anywhere else.

This is less of an issue, it is our daily job to find not the best but a solution we're happy with. You need some experience and knowledge of the language you are using (using C# I bet you work in Unity) and it'll go on

@shaarigan Yeah I suppose that's a pretty simple solution. It definitely solves the problem I was having.

Thank you!

You should see “placement” not as a single function but as a sequence of “find a good spot for unit” followed by “put unit at found spot”. The former doesn't change anything, it just computes a position by inspecting the cells etc. The latter does the actual placement using the position computed by the former. (That is, first decide that the jump will give a safe result, before you make the jump.)

In the placement function I typically double-check that all the rules allow that provided position to verify correctness of the decision procedure. Obviously that should never fail, although in practice it is surprising how often you end up with such “impossible” results.

This topic is closed to new replies.

Advertisement