Sloopernine
Sloopernine

Reputation: 1

Many Enums in one Array C#

I have been looking around how to make one array with many different enums.

What I am trying to do is have enums with for example

public enum playerTeam
{
    Red,
    Blue
};

and another with

public enum currentWeapon
{
    Knife,
    Gun,
    Rifle,
    Shotgun,
    SniperRifle,
    RocketLauncher,
    Grenade,
    Molotov,
    FlameThrower,
    ProximityMine,
    RemoteMine
};

and then assign them to a array called something like

Players[]

Then being able to loop trough the array and set values of each enum. I have used the enums without array before. To set/get data of the player. But now I am about to expand my project to multiplayer. And I cant figure out how to add enums to one array. As that would make code a bit easier to handle.

This was really hard to explain, hope you guys understand..

Upvotes: 0

Views: 137

Answers (3)

user34537
user34537

Reputation:

This is a bit ridiculous because team is definably not a weapon (wtf is it currentWeapon. and not Weapon) but if you're asking how to use an enum and bit flags you could write

[Flags]
public enum Weapon
{
    ...
    WeaponMask = 0xFF
    IsBlueTeam = 0x0100
}

Which allows you to do

switch(player[i] & WeaponMask) { case SomeWeapon: ... }
isBlueTeam = (player[i] & IsBlueTeam) != 0 //assuming its either blue or read

Upvotes: 0

user932887
user932887

Reputation:

That's a weird way to look at things, considering what C# allows you to do. At best, to achieve exactly what you want, you could use something that maps keys to values (players to weapons/teams), like the System.Collections.Generic.Dictionary.

But there's really no need to do that. The Player class should contain that info in two fields:

class Player
{
   ...
   private Team currentTeam;
   private Weapon currentWeapon;
   ...
}

Judging by how you named your enums, and by your idea, I'm thinking you should also follow a learning resource for C# and OOP, from beginning to end.

Upvotes: 0

Michael
Michael

Reputation: 1833

I'd suggest you create a class Player which has members Weapon and Team. Then use player instances to perform operations.

class Player
{
    Weapon CurrentWeapon {get; set;}
    Team Team {get; set;}
}

Upvotes: 4

Related Questions