C#: How to best track in-game "states" of things (i.e. using bitwise operators)?

Started by
4 comments, last by Shpongle 9 years ago

Right now I'm making a 2D space shooter. I have a class, Ship, which holds all the data and behavior for spaceships. Each Ship object can contain multiple instances of another class, ShipPart. Right now I'm using ShipPart mainly for different animations that will trigger depending on different types of status variables in the Ship object.

For example, right now I'm using ShipPart instances to display thruster animations. When the Ship is moving, the ShipPart for thrusters is set to Active and the animation displays.

Ideally what I want to be able to do is set a variety of on/off statuses for the Ship class that can correspond to any given ShipPart instance. I was thinking that using basic bits to do this could work. I could use a simple 16 bit or 32 bit variable in the Ship class, giving me 16 or 32 possible "states" for any Ship object. Then doing an appropriate bitwise comparison to a corresponding variable in each ShipPart instance, could set their "Active" boolean variable accordingly.

The only issue I'm not 100% sure about the best way to do this. Any help? Or if anyone has a better way of going about this, I'm happy to hear it.

Advertisement
You could for example use an enum, set the values to the various bits and annotate it with [Flags].

It's a C++-ism, and not that common, but it nevertheless works:



[Flags]
enum RenderType
{
  None = 0x0,
  DataUri = 0x1,
  GZip = 0x2,
  ContentPage = 0x4,
  ViewPage = 0x8,
  HomePage = 0x10 // Next two values could be 0x20, 0x40
}
You can use it like a normal type, but will have to cast to uint for storing/restoring.

RenderType renderFlags = RenderType.GZip | RenderType.HomePage;

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

You could for example use an enum, set the values to the various bits and annotate it with [Flags].

It's a C++-ism, and not that common, but it nevertheless works:


[Flags]
enum RenderType
{
  None = 0x0,
  DataUri = 0x1,
  GZip = 0x2,
  ContentPage = 0x4,
  ViewPage = 0x8,
  HomePage = 0x10 // Next two values could be 0x20, 0x40
}
You can use it like a normal type, but will have to cast to uint for storing/restoring.

RenderType renderFlags = RenderType.GZip | RenderType.HomePage;

I wouldn't say it's not common, I use flags like this all the time and see plenty of libraries that do as well.

Also, if you're going to be storing it as a uint, I'd change the enum's underlying type:


[Flags]
enum RenderType : uint
{
  None = 0x0,
  DataUri = 0x1,
  GZip = 0x2,
  ContentPage = 0x4,
  ViewPage = 0x8,
  HomePage = 0x10 // Next two values could be 0x20, 0x40
}

As a side note, if you use flags then I highly recommend using the HasFlag() function:


var renderType = RenderType.GZip | RenderType.HomePage;
// ...
if(renderType.HasFlag(RenderType.GZip)
{
    // Do something here
}


Ideally what I want to be able to do is set a variety of on/off statuses for the Ship class that can correspond to any given ShipPart instance. I was thinking that using basic bits to do this could work. I could use a simple 16 bit or 32 bit variable in the Ship class, giving me 16 or 32 possible "states" for any Ship object.

From your description it sounds like you already have this functioning as you like. Why do you want to move to a bitmask to control different ship parts? That seems like a more rigid architecture...

I guess my question is: what's wrong with your current architecture? What problems are you facing with it?


Ideally what I want to be able to do is set a variety of on/off statuses for the Ship class that can correspond to any given ShipPart instance. I was thinking that using basic bits to do this could work. I could use a simple 16 bit or 32 bit variable in the Ship class, giving me 16 or 32 possible "states" for any Ship object.

From your description it sounds like you already have this functioning as you like. Why do you want to move to a bitmask to control different ship parts? That seems like a more rigid architecture...

I guess my question is: what's wrong with your current architecture? What problems are you facing with it?

Right now I'm still trying to design the architecture for this. The only thing I have implemented is controlling the thruster animations and that's hard-coded.

@Endurion & Programmer16: Thank you for the advice about using enums/flags! I've started reading up on it and it seems like something that will be the solution I'm looking for.

This topic is closed to new replies.

Advertisement