- Home /
Cycling through enum with key press
I'm trying to create a weapon script for the player. I decided dump the different weapons/objects into and enum rather than using a SwitchWeapon function. However, now I'm kind of stuck. I would like to add the feature of using either the mouse scroll or the "q" and "e" keys to cycle between the weapons. I know exactly how I would do it had I created a weapon function, however I'm kind of at a loss for how to do this with an enum and I really don't feel like starting the script over using a weapon function. I suppose I could use a series of if else statements to check which WeaponType from the list is selected, but it seems like there should be a more intuitive way to do this.
Also, I'm not terribly familiar with using enum (obviously). Does the enum have integers corresponding to each element in the list? because then I suppose I could just increase/decrease that integer by one.
Here's some of the code I have:
The enum:
enum WeaponType {primaryFire, concreteBarrier, stationaryTurret, playerShield}; var weaponType : WeaponType;
//this is sent to a switch(weaponType) case if(Input.GetKeyDown("f")){ weaponType = WeaponType.primaryFire; } if(Input.GetKeyDown("1")){ weaponType = WeaponType.concreteBarrier; }
if(Input.GetKeyDown("2")){ weaponType = WeaponType.stationaryTurret; } if(Input.GetKeyDown("3")){ weaponType = WeaponType.playerShield; } if(Input.GetKeyDown("e")){ //what code needs to go here to switch to the next weapon/object in the enum list? } if(Input.GetKeyDown("q")){ //what code needs to go here to switch to the previous weapon/object in the enum list? }
Sorry this is so long. Thanks for any help in advance.
How come a "weapontype" is defined as "primaryFire" isnt'a weapon type supposed to be $$anonymous$$elee/Anti-Tank/Ranged/Energy-Based/Ballistic something like that ? Sounds confusing to say primary fire is a weapon type, I would call that a weapon firing mode ins$$anonymous$$d tbh
Answer by Eric5h5 · Jan 24, 2011 at 01:59 AM
Enums are essentially ints with fancy names, so to some extent you can treat them like ints. First you would need
private var numberOfWeaponTypes = System.Enum.GetValues(WeaponType).Length;
Then you can use
else if(Input.GetKeyDown(KeyCode.E)){
weaponType += 1;
if (weaponType == numberOfWeaponTypes) weaponType = 0;
}
else if(Input.GetKeyDown(KeyCode.Q)){
weaponType -= 1;
if (weaponType == -1) weaponType = numberOfWeaponTypes-1;
}
(It's a bit more efficient to use "else if" for them all aside from the first, since it skips over the ones that can't be used instead of evaluating them all. Also, using KeyCodes instead of strings is faster and gives you potential errors at compile-time instead of runtime.)
Thank you! This is perfect. Works like a charm. :) Yeah, it was that numberOfWeaponTypes variable I just couldn't figure out. Also, thanks for the tips. I always wondered if using $$anonymous$$eyCodes has any particular advantage. I'll be changing all my strings to $$anonymous$$eyCodes. As for the if statements, that was just lazy program$$anonymous$$g on my part. I was going to go back and change it eventually. :) Thanks again!
I should also mention that you can assign specific values in enums...for example, you could do "`enum WeaponType {primaryFire = 3, concreteBarrier = 8, stationaryTurret = 12, playerShield = 20};`", but that wouldn't be a good idea in this particular instance obviously.
hey I knew that but didn't even think of using enum like that! Thanks man :D You helped me out big time here just for answering somebody else's question
Hi,
I know this is an old topic, but does anyone know how to write this in c#?
Thanks!
I couldn't figure out the bottom part but the first line I believe is written like
private int numberOfWeaponTypes = System.Enum.GetValues(typeof(WeaponType)).Length;
int numWeapons = System.Enum.GetValues(typeof(WeaponType).Length;
// Cast weapon to int to do math on it
int wt = (int)weaponType;
wt++;
if( wt == numWeapons) wt = 0;
weaponType = (WeaponType) wt;
Your answer
Follow this Question
Related Questions
Operator '==' cannot be used! 2 Answers
Switch case using enum javascript 2 Answers
Weapon pick up and switching script 2 Answers
Why doesn't this weapon switching script work 1 Answer
best way to weapon swapping 0 Answers