0
\$\begingroup\$

I'm currently working on a 2D RPG, where the player character moves on a tilemap but is not constrained to the individual tiles. Every tile in a room is referred to by x and y coordinates to an Array. I'm considering using a Location class to store a float x and float y for the player character, as well as, perhaps, a Room. What are the advantages and disadvantages of this approach versus keeping the float x and float y values of the Player class (thus not creating a Location class)?

Collision detection with walls, objects, and other players will need to be calculated.

\$\endgroup\$
2
\$\begingroup\$

This depends on the language. Since you didn't mention language, I am for the moment going to assume either C++ or C# or D or Rust or some similar low-level language (not Java!), because game development.

There are zero advantages to keeping x and y as members of Player rather than abstracting out to a Point/Location type.

There are many many advantages to abstracting tightly-coupled primitive values to a separate type. Abstracting in this way is a fundamental part of writing good, generic, maintainable code. Such abstraction better fits the SOLID principles with a practical result of reducing cut-n-paste code and hence reducing bugs and maintenance costs and allows the creation of reusable algorithms that operate on common data types (e.g., utilities for calculating the distance between points, or rotating points around an origin, or so on).

Quite simply, every single game engine/framework has some kind of Point/Location type. Not having one is just silly. Going along with this there should be similar classes for things like AxisAlignedBox or other geometric primitives that you game frequently uses. Duplicating the data members and algorithms every time you need such a primitive is bad bad coding.

If you are in C#, prefer a struct over a class, as structs are value types and classes are reference/boxed types. As to why that matters, read on.

When using a language like Python or Java or JavaScript or so on then there is a set of disadvantages with having a separate Location/Point class, though note that the advantages massively outweigh the disadvantages. User-defined types in such languages are always reference types. That is a problem.

The most frequently-cited but rather least important is that the reference types are always boxed (allocated in the free-store/heap), accessing them incurs a memory indirection, and creating instances increases GC pressure. Basically, they hurt performance. That's not even close to the worst problem, even in games.

The most serious problem is that you can "leak" references easily from your public interfaces and get very bewildering bugs. Code to illustrate:

// C++ / C# / any other "good" programming language
player.position.x = 1;
local_pos = player.position; // COPY position to a local
local_pos.x += 3;
print(player.position.x); // prints: 1

// Python / Java / JavaScript / any other "bad" language
player.position.x = 1;
local_pos = player.position; // ALIAS position to a local
local_pos.x += 3;
print(player.position.x); // prints: 4 !!!! oops!

In languages with these enforced reference semantics you are required to write more complicated code to ensure that you are avoiding mutating any references. The simple version involves lots of clones but that can result in pretty horrible performance, even for simple 2D games. Production-grade solutions are complicated. Best to just completely shun languages that lack user-defined value types, in my not-so-humble opinion.

\$\endgroup\$
  • \$\begingroup\$ Thanks for the answer. Why do you consider C# a low-level language though, in comparison to Java? I understand C++ and co. but doesn't C# run on .NET? (For the record, I am coding in Java.) \$\endgroup\$ – sneelhorses Jul 26 '16 at 2:54
  • \$\begingroup\$ .NET is considerably more powerful and allows much more low-level access than the JVM does. C# has features like struct and unsafe (among others) while Java has no equivalents. Note that Android's Dalvik runtime may have extra features here of which I am unaware, but a quick Google search isn't turning anything up. \$\endgroup\$ – Sean Middleditch Jul 26 '16 at 3:29

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.