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!

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.