- Home /
Error with Structs and Enums
Im making a inventory system for my game and i am trying to check if an item type is equal to the same type. I have a struct with enums:
[System.Serializable]
public class Item {
#region vars
public string itemName, itemDesc;
public int itemID;
public Sprite itemIcon;
public GameObject itemModel;
public int value, amount;
public ItemTypes itemType;
#endregion
#region ItemTypes
public struct ItemTypes
{
public enum Consumable { Health, SpeedPotion, Food, JumpPotion, SkillPotion,}
public enum Weapon { Automatic, Sniper, Pistol, Shotgun, }
public enum Armor { Head, Chest, Pants, Shoes, }
public enum Craftable { Stick, Cloths, Rope, Rock}
}
#endregion
then I check the item type:
if (inventory.items [slotNumber].itemType == Item.ItemTypes.Consumable) //Error here
{
inventory.items[slotNumber].amount--;
if(inventory.items[slotNumber].amount == 0)
{
inventory.items[slotNumber] = new Item();
itemAmount.enabled = false;
inventory.ItemOptions.GetComponent<ItemUse> ().usedItem = false;
}
inventory.ItemOptions.GetComponent<ItemUse> ().usedItem = false;
}
else
{
Debug.Log("Cannot use Item");
}
I want to take the easy way but it's not letting me. I dont want to do "Item.ItemTypes.Consumable.Food", "Item.ItemTypes.Consumable.Health", etc. just to check if an item is a consumable. Thanks for the help.
Error: Assets/InventoryScripts/HotBarItem.cs(28,60): error CS0119: Expression denotes a type', where a
variable', value' or
method group' was expected
Answer by Landern · Jan 21, 2015 at 03:22 AM
That isn't how enums work. You defined four enum types in a struct, you're checking the enum type Consumable which is an enumeration of integers(the default type for an enum numerical representation) represented in code by Names such as Health, SpeedPotion, Food, JumpPotion, SkillPotion. In order to check itemType, it would have to be comparable to a type ItemTypes that derives from ValueType in c#. But structs can't be inherited whatsoever, but do inherit from type ValueType. There isn't a reason to have a struct with enums in it, you're not defining anything special at all, enums derive from Enum and are beyond optimized. You have complicated something that shouldn't be complicated.
Some additional points:
structs aren's serialized / serializable by Unity.
Even if you turn your ItemTypes struct into a class to make it serializable, the ItenTypes class / struct is actually empty. It doesn't have any information stored in it. All you have there are enum declarations which are types and not fields. So an instance of ItenTypes contains 0 information.
You might want to create derived classes from your Item class for each category. Or, if an item can actually be in two categories, implement a strategy pattern (just like Unity's component system) to describe what behaviour an item should have
Another way is to use only one enum called ItemType and define all possible types in there. You could arrage them like a bitmask.
Example:
public enum ItemType
{
Consumable = 0x00010000,
Health = 0x00010001,
SpeedPotion = 0x00010002,
Food = 0x00010003,
JumpPotion = 0x00010004,
SkillPotion = 0x00010005,
Weapon = 0x00020000,
Automatic = 0x00020001,
Sniper = 0x00020002,
Pistol = 0x00020003,
Shotgun = 0x00020004,
Armor = 0x00030000,
Head = 0x00030001,
Chest = 0x00030002,
Pants = 0x00030003,
Shoes = 0x00030004,
Craftable = 0x00040000,
Stick = 0x00040001,
Cloths = 0x00040002,
Rope = 0x00040003,
Rock = 0x00040004
}
Here you can use a bit test to see which category an item is in:
ItemType type = ItemType.Health;
if(type & ItemType.Consumable != 0)
// item is a Consumable
if(type & ItemType.Weapon != 0)
// item is a Weapon
Bunny, the last part of your answer:
ItemType type = ItemType.Health;
if(type & ItemType.Consumable != 0)
// item is a Consumable
if(type & ItemType.Weapon != 0)
// item is a Weapon
Edit: Never$$anonymous$$d But if i try to do: if(type & ItemType.Health != 0) It will allow All Consumables
Answer by sk8terboy4 · Jan 21, 2015 at 04:19 AM
Thanks Landern, I knew I was doing something wrong with the enums and stuff just didn't know exactly. Thanks Bunny, I'm going to take a look at Strategy patterns and bits.
Your answer
Follow this Question
Related Questions
Enum instead of numbers in arrays 1 Answer
enum comparing 1 Answer
Returning item names in an inventory array 3 Answers
Cannot Implicitly convert to bool problem? 1 Answer
Enum Type Inventory? 1 Answer