- Home /
enum comparing
Hi guys, i was just trying to wrap my head around enums and was trying to use them for an equipment system and inventory system but seem to be getting this error no matter how i approach it.
Assets/Slot.cs(48,95): error CS0019: Operator ==' cannot be applied to operands of type
DragHandler.Slot' and `Slot.mySlot'
on script Slot
public enum mySlot{
invSlot,
oneHand,
TwoHand,
offHand,
head,
shoulder,
chest,
legs,
belt,
boots
};
public mySlot itemType = mySlot.invSlot;
public void OnDrop (PointerEventData eventData)
{
if(item && DragHandler.itemBeingDragged.transform.GetComponent<DragHandler>().itemType == itemType)
{
print(transform.name);
//this one here is causing the error,
}
what i want to do is check both enums against each other to see if either one = each other without having to check each state otherwise it would be very messy is this possible without having to check each state?
i would of thought it would be something like i had done checking only the parameters i needed and everything else would return it back to its original slot in the inventory if it did not meet the state requirements
DragHandler Script
public enum Slot{ invSlot, oneHand, TwoHand, offHand, head, shoulder, chest, legs, belt, boots };
public Slot itemType = Slot.invSlot;
these are not the entire scripts as they would be very long to post but the required info i would believe that is needed to help sold this issue.
the 2 enums i want to check against eachother without having to check every single state. more or less
if it is a 1 Handed weapon, is it a sword dagger mace, etc, in most RPG games a mage cant wield a sword or a shield. but can store them in their inventory. what i want to do is compare the items enum against the players inventory and equipment slot enums. can this player equip this particular item etc. the way i want to do it is through enum comparing without having to go through every single possible combination rather than checkign the possible combinations only like if it is a offhand Sword you can only be put in the offhand weapon slot of a warrior or the inventory slot. but other rules will apply to other classes. that may break that condition.
Answer by maccabbe · Jul 25, 2015 at 05:40 AM
You are declaring two different enums Slot and mySlot and the compiler cannot compare the two directly. This is because it cannot assume that two enums are describing the same thing. For instance suppose I had one enum for the Smith family, James Smith & Mary Smith, and another enum for the Miller family, James Miller & Mary Miller.
enum Smith {James, Mary};
enum Miller{James, Mary};
Then the following isn't defined to the compiler
Smith james1=Smith.James;
Miller james2=Miller.James;
Debug.Log(james1==james2);
After all, in your case you would expect true to print but in this case you would expect false to print.
In your case you are defining two enums with the same enumerators so it seems like you just want to use one enum. Then you can just compare them directly, i.e.
enum Smith {James, Mary};
Smith james=Smith.James;
Smith mary=Smith.Mary;
Smith notMary=Smith.James;
Debug.Log(james==mary);
Debug.Log(james==notMary);
So you should remove Slot or mySlot and only use the enum you keep.
ahh thanks had a feeling i was doing something so stupidly wrong, ok so i fixed that part up and decided to have everything working off one script so it is much easier to manage.
would it be possible to only show specific enums in the inspector based on what is selected.
or is their an easier way to allow more options for a single enum. the issue im having is i can get items to move around my UI from my inventory to my Character slots, but i cant put items in my Inventory because the parameters do not meet.
i had tried adding a second script and comparing with a foreach on the componenets of the script, that kinda worked but when it returns false it fails. so i tried doing it with an int as well so when the int is >0 it returns it is an allowed slot to place the item, that did kinda work, but it is very sloppy, and i have no reason to add the script twice on every single game object/icon their has to be a more efficient and cleaner way than this hacky way.
the way i have it setup is like this.
I'm not sure why you are checking parameters while moving items in your inventory. Usually any item can be moved to any slot in the inventory so since any Item you are trying to move is an Item then it doesn't matter what itemtype it is.
Yes that is working fine for now moving items around the inventory. But that doesn't restrict a sword from being placed on a chest alot in the equipment slot.
The issue I was having their was the 2 weapon alots have more than 1 parameter thus making an enum almost useless in this case. So I decided to do the weapon slots with bools to check if a 1 handed main hand, off hand or 2 handed weapon can be placed in any of the slots. Althoufh its the approach I didn't want to take. It works for now. managed to get all the stats working correctly for each slot. Only thing left to do is compare the weapon alots against the 1h items and 2 handed items. As the 2H item goes into the main hand alot it still allows an offhand item to be equiped. that's fine for now as it could be a talent or skill I may wish to add, but I still need to come up with a way to move items from the offhand to my inventory if I'm weilding a 2 handed item.
So far its moving well, but plan on rebuilding it eventually when I get this one up and running.
Your answer
Follow this Question
Related Questions
Change Enum Contents 1 Answer
Inventory AddItem help 1 Answer
Scriptableobject List and Instantiating objects from it 3 Answers
Inventory Drop Function Problem 1 Answer
What is a good component to use for a GUI inventory display? 1 Answer