✅ 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
CrossTheLine
CrossTheLine2y ago
Do let me know if there's a better or more efficient way to handle this
Shinyshark
Shinyshark2y ago
You want to check if a selected Enum is like another Enum? @CrossTheLine
CrossTheLine
CrossTheLine2y ago
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?
Shinyshark
Shinyshark2y ago
If you want to use it in another class, it will give you this error:
Shinyshark
Shinyshark2y ago
So in that case, you would indeed tell it to add the using.
Shinyshark
Shinyshark2y ago
Shinyshark
Shinyshark2y ago
Now it recognizes it.
Shinyshark
Shinyshark2y ago
A lot of decisions you make in programming are valid. It depends on your reasoning behind them why you made them.
CrossTheLine
CrossTheLine2y ago
I see, that's fair enough then
Shinyshark
Shinyshark2y ago
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.
ero
ero2y ago
They can't use using CardClass;, obviously
CrossTheLine
CrossTheLine2y ago
Oh, why not Ero?
ero
ero2y ago
You can't have a using directive for a class
Shinyshark
Shinyshark2y ago
ero
ero2y ago
Just try it?
CrossTheLine
CrossTheLine2y ago
unity doesn't seem to bar me from using cardclass, but it tells me i should 'using static CardClass' instead
Shinyshark
Shinyshark2y ago
Oooh my bad, I didn't know you were using Unity. I thought you were making it as a console app or something.
CrossTheLine
CrossTheLine2y ago
In that case, should I be using using static CardClass as the option instead?
ero
ero2y ago
No Wrong message But still no
Anchy
Anchy2y ago
just use the using directive on the namespace you created containing your cards enum like above
CrossTheLine
CrossTheLine2y ago
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 wrong
Anchy
Anchy2y ago
referring to this example here
CrossTheLine
CrossTheLine2y ago
Oh, so something like Using CardClass.Suits; then? that way i dont need to drag the whole class in
ero
ero2y ago
Unity 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?
CrossTheLine
CrossTheLine2y ago
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 somehow
Anchy
Anchy2y ago
if 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
CrossTheLine
CrossTheLine2y ago
Ah yep, thank you, now I understand. I did public CardClass.Suits playerSuit; so now I can properly initialize the value.
Anchy
Anchy2y ago
yes correct
Accord
Accord2y ago
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.
Want results from more Discord servers?
Add your server
More Posts