- Home /
Enum style variable
How can I create a variable that looks like an enum in the inspector? Do I have to declare it as a class? IE
public class myEnumvar {
var item1: int = 1;
var item2: int = 2;
}
public var myEditableEnum: myEnumvar;
Or is there a more straightforward way of doing it?
for anyone wanting this functionality the above code works btw. I just wondered if there was a syntax for a more intuitive way of declaring it similar to: public enum var myvar { var1:int, var2:int } or if I was already doing it correctly and to leave it as-is
Answer by sneftel · May 20, 2011 at 10:12 PM
What an enum is, is a set of non-editable named values. You can then make variables of that enum type, and those variables will be settable to any of the named values (with a useful dropdown box).
It sounds like you don't actually mean "enum". Do you just want to organize a set of variables into its own scope? If so, the code you're using above is the most straightforward way of doing it. You shouldn't call it "enum", though, as that's an unrelated concept.
Actually I didn't. :) "a variable that looks like an enum in the inspector?" I thought scope mean public, private or static. (I'm self taught) Not entirely familiar with the correct ter$$anonymous$$ology. Just wanted to make sure I was doing it right, which I take it I am. Thanks. :)
I also wanted to set some of them with labels ins$$anonymous$$d of numbers so they could be selected from a dropdown list. Right now I just use an enum for the list of labels. Then a separate editable group of variables with "Label" entries and IDs that match the enum dropdown which seems clunky.
"Scope" is one of those things that programmers use to mean way too many different things. :-) The more unambiguous term for private/public/static is "protection level", but I've definitely seen people call it "scope".
If you want to use an enum-style dropdown box to select a set of values, you have a couple of options. The first is, as you say, use an enum for the dropdown, then translate from that to what you actually want.
The other way is to move to CustomEditor. These can be time-consu$$anonymous$$g to write and keep up to date, but they're the only way to have full control over the inspector.
Custom editors (or often more practical/easier: custom inspectors) are definitely worth the effort if you're working on a complicated script.
Answer by sneftel · May 20, 2011 at 09:42 PM
No that just declares enumerator constants that can't be edited. I need something that is visible in the editor and can be edited during runtime. So it looks like an enumerator but is read/write ins$$anonymous$$d of read only. Just for legibility for other $$anonymous$$m members who don't read code very well.
So I can use something like if( player.health > player.maxhealth ). It also lets me organize my variables into sub-groups. $$anonymous$$y main routine has over 100 variables, if they were one long list it would be incomprehensible.
Together this makes variables human readable and organized and makes my code read like pseudocode.
Your answer
Follow this Question
Related Questions
Is it possible to create a script dinamic like animation>size controling the Elements variables? 1 Answer
How to Typecast JS Variables as C# Classes? 0 Answers
Different way to access class variables? 1 Answer
Finding the length of an enum in a different class 1 Answer
Creating A Class Variable That Works With Any Class 0 Answers