- Home /
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!
i'm not sure i understand what you're trying to achieve, but changing the contents of an enum during runtime is impossible.
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?
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.
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.
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.
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!
...I'm actually struggling to see how a Dictionary could be used here.
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! :)
Your answer
Follow this Question
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