✅ Best way to use and compare enums? (Unity)
Hi, currently working with enums in the ubiquitous poker-suit card game type setup that a lot of people have going on.
Two main scripts are involved, the CardClass script and the GameManager script - and the main enum I want to focus on for now is 'Suits'.
My main question is how best to compare it in the case of the player selecting different suits as their chosen suit. So if a player selects spades and then hearts, Clubs and Diamonds now treat the player differently in the game logic.
Do I simply just have a line like
using CardClass
in the GameManager which grants me access to the same enum, and create a new variable in the manager to keep track of the player's chosen Suits enum?29 Replies
Do let me know if there's a better or more efficient way to handle this
You want to check if a selected Enum is like another Enum? @CrossTheLine
Hmm, yeah, if I've got an enum in the Cardclass that's based off of poker suits, then do I just import it over via
`using CardClass
and compare it there?If you want to use it in another class, it will give you this error:
So in that case, you would indeed tell it to add the using.
Now it recognizes it.
A lot of decisions you make in programming are valid. It depends on your reasoning behind them why you made them.
I see, that's fair enough then
But if you are new, do not worry about it too much. Make the code work and learn from the issues you will run into eventually.
If you want to compare a chosen suit, you can just use the "==" comparison.
They can't use
using CardClass;
, obviouslyOh, why not Ero?
You can't have a using directive for a class
Just try it?
unity doesn't seem to bar me from using cardclass, but it tells me i should 'using static CardClass' instead
Oooh my bad, I didn't know you were using Unity.
I thought you were making it as a console app or something.
In that case, should I be using
using static CardClass
as the option instead?No
Wrong message
But still no
just use the using directive on the namespace you created containing your cards enum
like above
Sorry, I'm a bit confused by that. Do you mean in the gameManager where I'm wanting to check the enum?
In which case my intellisense tells me to use
using static
instead which Ero says is wrongreferring to this example here
Oh, so something like
Using CardClass.Suits;
then?
that way i dont need to drag the whole class inUnity often doesn't have namespaces. These suggestions are pointless
You don't need to be
using
anything
Have you even tried, at all, that you can use these types in other files?Well, I'm simply confused and asking about it, I guess. Because I've defined the
public enum Suits { heart, spade, diamond, club};
in the CardClass, I don't know if the best approach to this in the Game Manager is to quite literally, just copy-paste the exact same enum over? I'm not sure how one would refer to the enum without specifying what it is somehowif it is inside of the global namespace then you can just call CardClass.Suits.heart, provided that the suits enum is inside of that class
Ah yep, thank you, now I understand.
I did
public CardClass.Suits playerSuit;
so now I can properly initialize the value.yes correct
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.