Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by RealSoftGames · Jul 25, 2015 at 04:27 AM · inventoryenuminventory system

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image RealSoftGames · Jul 25, 2015 at 12:41 PM 0
Share

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.

alt text

inventorysystem.png (251.8 kB)
avatar image maccabbe · Jul 25, 2015 at 04:27 PM 0
Share

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.

avatar image RealSoftGames · Jul 25, 2015 at 05:22 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges