0

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..

2
  • 6
    Sounds like you need an array of Player class instances, where Player would have members Weapon and Team. Commented Oct 1, 2013 at 20:45
  • 1
    Why don't you use two properties in a Player class, one CurrentWeapon and one Team? Commented Oct 1, 2013 at 20:45

3 Answers 3

4

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;}
}
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.