- Home /
I want to compare the value of two enums with one another but struggle in how to accomplish it. Might need a bit help in understanding why.
Hi =)
I have a shop, which generates its buyable items based on which kind of category of wares it belongs to. For examle: One shop only sells every item from the "craftmaterial" category, while another only sells "weapons". Up until now, I used strings for this and it worked. But I would rather use enums, since I'm sure that someday, I will misspell "craftingmaterial" and wonder for hours why it doesn't show up in my shop.
So, on the side of my items, I wrote this (it's an excerpt):
using UnityEngine;
[CreateAssetMenu]
public class Item : ScriptableObject
{
public enum Warengruppe { Craftingmaterial, Weapons };
public Warengruppe warengruppe;
}
The shop-script looks like this (again, an excerpt)
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ShopSlotGrid : MonoBehaviour
{
public enum Warengruppe { Craftingmaterial, Weapons };
public Warengruppe warengruppe;
void Start () {
itemsInGame = GameObject.FindGameObjectWithTag ("ItemsInGame").GetComponent<ItemsInGame> ();
foreach (Item i in itemsInGame.items)
{
if (i.warengruppe == warengruppe) // TODO Hier zu enum ändern (change this to enum)?
{
shopSlots.Add(Instantiate(shopslotPrefab, shopslotGrid));
dieseShopAuslage.Add(i);
}
}
}
Right now, I get this error:
Operator
==' cannot be applied to operands of type
Item.Warengruppe' and `ShopSlotGrid.Warengruppe'
First, I'm rather sure that using two seemingly equal enums like this is probably a bad idea? Second: Why can I not use the "=="-operator in this case? I've used this operator in other places of my game successfully and the only difference I can see is that this is the only place where I'm using a "for each" loop :/
If it helps: I use this script on every shop in my game, so I would like to choose which item category belongs to each of them by hand in the inspector and not inside the code.
Would love to hear your input :)
Answer by Bunny83 · Apr 27, 2018 at 10:25 PM
You defined two seperate enum types. One that is a nested type under your Item class and one that is a nested type under your "ShopSlotGrid". Those are two completely seperate types. Just because you named them the same doesn't make it the same type. You may want to define that enum type outside of your class and let both classes use the same enum type. The cleanest solution would be to create a "Warengruppe.cs" file and just put your enum definition in there. Of course remove the nested types and just use that one.
ps:
As a german myself i would strongly recommend to rename that enum to something like ItemGroup ^^ Especially since the members of that enum are english.
If you don't want to create a seperate file for the enum you can declare it in one of the two class files. You can even keep it a nested type if you like. Though nesting should be used carefully. When you create a nested type that type should be strongly bound to the outerclass. In this case i would recommend to declare it inside the Item class and name it just "Group". You can use the enum type every where by using Item.Group
public class Item : ScriptableObject
{
public enum Group { Craftingmaterial, Weapons };
// [ ... ]
public class ShopSlotGrid : $$anonymous$$onoBehaviour
{
public Item.Group group;
// [ ... ]
Aaaaaah, thank you! I always wondered why I should declare enums outside of a class (tutorials like to not mention that part of enums, sadly). I've done what you said and now, it works! And I think, I understand enums a bit better than before, thank you!
Oh, and... yeah, my code is a bit of a bi-lingual mess ;) I rework the parts that work into something a bit more consistent on Sundays and I think I will put "rename stuff" on the list, too ;)
Answer by Buckslice · Apr 27, 2018 at 10:26 PM
You should just have one version of the enum, you cant compare different enums together really, and there is no point in having two enums here because they are exactly the same. Just use Item.Warengruppe since the categories seem to designate what type of items they are. In line 9 of shop code just say this instead
public Item.Warengruppe warengruppe;
Hope this helps
Thank you for your input =)! I wasn't really aware that I had two different enums since they sounded similar. Oh well, beginner mistakes ;)
Your answer
Follow this Question
Related Questions
Using Enum With Different Scrits 2 Answers
How to set a value for each level without using a switch statement 2 Answers
Enum with multiple fields 1 Answer
Enum with multiple fields 2 Answers