Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 importsjc · Aug 26, 2013 at 11:36 AM · inspectorinventoryenuminventory system

Change Enum Contents

Hi,

I am building an inventory system(using javascript), and i am attempting to implement an item type and sub-type system. Let me explain:

an item type for example could be wood.

and its sub types could be pine, spruce, and maple

the types and sub types are contained like this:

 enum itemType{wood, books, candles}
 enum itemSubType{pine, spruce, maple}

the problem i run into is changing the contents of itemSubType when the itemType changes.

Is there any way to fix this? i have done some research and it seems i might need a custom inspector, so please do not put that in your answer.

Thanks for any help!

Comment
Add comment · Show 2
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 Jamora · Aug 26, 2013 at 11:48 AM 0
Share

i'm not sure i understand what you're trying to achieve, but changing the contents of an enum during runtime is impossible.

avatar image perchik · Aug 26, 2013 at 05:48 PM 0
Share

Do you know what item and subtypes can exist in the game? Or is this something you want to do in the editor, and have the game run with the right item?

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Hoeloe · Aug 26, 2013 at 12:01 PM

You can't change the contents of an enum during runtime, in the same way you can't change the name of a variable during runtime. Enums are just tools to help the programmer. Essentially, they are just integers, but with names to give the programmer hints as to what they are (which is why they are called "enumerations"). In this case, pine=0, spruce=1 and maple=2. What you might be better off doing is simply using an integer directly, so instead of storing spruce, you store 1, and interpret that as the required information. Because this is a more general solution, it means you can use the information differently depending on other values (such as the item type). As well as this, you could make a collection of different enums, for example:

 enum woodType { pine, spruce, maple }
 enum bookTYpe { hardback, paperback }

Because these are essentially just integers, you can cast them to integers, so writing (int)(woodType.spruce) will give you the value 1. Because of this, you could assign to the subtype integer using any of these different enums, and as long as you parse the information correctly once it has been set, it will work and still be readable.

Comment
Add comment · Show 16 · 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 Fattie · Aug 26, 2013 at 12:30 PM 0
Share

Right, you can't change enum at runtime.

It could be, you should be using a simple Dictionary, or other List-like technique. Check out many answers here on Dictionary examples.

avatar image Hoeloe · Aug 26, 2013 at 12:32 PM 2
Share

What he should really be doing is using a struct or class for each object type (if a class, then extending a base class for general items), allowing much more detailed information to be given, unique for each item.

avatar image Fattie · Aug 26, 2013 at 12:59 PM 1
Share

for sure, but it possibly sounds like a job for a Dictionary which is - come to thin of it - exactly like using an "enum" but you can change it an run time!

avatar image Hoeloe · Aug 26, 2013 at 01:29 PM 0
Share

...I'm actually struggling to see how a Dictionary could be used here.

avatar image Fattie · Aug 26, 2013 at 02:16 PM 0
Share

Actually .. yeah I sort of see what you mean :-) I agree that it's totally unclear just what the OP wants specifically - so I may be totally offtrack. However, with a dictionary, of course you could just map "various words" (Dog, Cat, Tiger, Lion .. etc etc) to numbers, indexes, string-int. In this way he could easily add new words (Elephant, $$anonymous$$ouse, etc) at runtime. because it's a dictionary you can instantly get from the string say "Dog" to some code number integer like, 417.

it's clear that the OP is just getting going, I agree completely that he should learn about structs and classes as you mention (but I guess that won't specifically help - as such - if his problem is he has a "list of concepts" (dog, etc) expressed as STRINGS, BUT he wants to add to that at run time.

So to be honest I don't know what's best for the OP :) But he/she should definitely learn about structs, classes, and then Dictionary. I actually suspect an sql database is the answer here! :)

Show more comments

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

18 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

Related Questions

enum comparing 1 Answer

How to start an inventory system? 2 Answers

Referencing a class defined within a ScriptableObject 0 Answers

Exposing Enums in Custom Inspector 1 Answer

Accessible collection of Type 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