liveBook · Manning

Short

As explained in High Performance Unity Game Development

A fixed-width integer data type for representing relatively small whole-number values.

A short is a 2-byte signed integer with a range of −32,768 to 32,767. Use it for bounded whole-number data, such as enemy hit points, when reducing memory use or network payload size matters. section 2.6.2

Choose a smaller integer type only after checking its limits and performance:

  • Ask how large the value can get; a short can hold 10,000 hit points, including negative values. section 2.6.2
  • A short uses half the space of an int, but smaller types may be slower; test on the minimum target device. If compact storage is unnecessary, prefer int. section 2.6.2 section 2.7
Listing 2.22 Our enemy data class, with short hit point array
public class EnemyData
{
    public Vector2[] Position;
    public Vector2[] Direction;
    public float[] Velocity;
    public bool[] Alive;
    public short[] HitPoints;
}

This enemy data structure uses a short array for hit points. section 2.6.2

How High Performance Unity Game Development covers it

How deep it goes
Defines a 2-byte signed range, then warns that smaller types may run slower and should be tested on target devices.
Examples
Uses enemy hit points and a binary-saved enemy count to illustrate compact storage.
What you can do after
Apply it by checking maximum values, choosing a fitting range, and testing performance on the minimum target device.
How it connects
Compares short with int, ushort, and byte for storage size and performance.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of High Performance Unity Game Development. The section links point to the parts of the book it draws on.