What I want to do is have a variable (in a javascript) that can be changed from the UI. But I want the variable only to have certain things the user can choose from.
For example:
variable slotType should have three choices: right hand, left hand and body.
What I want to know is what is the variable type for such a thing and how to declare it?
That’s an enum:
enum SlotType {RightHand, LeftHand, Body}
var slotType = SlotType.Body;
–Eric
thank you very much
I never knew something like that existed. I will keep it in mind for later.
I tried it, it seems only one element can be selected at the same time. Is there something to select multiple elements ?
And can we assign them a value ? (so, writing SlotType.Body could give us a string, for example)
In C# you can mark enums with a “Flags” attribute and give them exponential values, then you can combine/read them using bitwise operations:
[Flags]
public enum Countries
{
Canada = 1,
UnitedStates = 2,
England = 4,
Japan = 8,
Australia = 16
}
Countries possibleCountries = Countries.Canada | Countries.Japan | Countries.UnitedStates; //(makes a value of 11, that value is only possible to get with that combination of countries
if (possibleCountries Countries.Canada == Countries.Canada)
{
//Canada is possible
}
if (possibleCountries Countries.Japan == Countries.Japan)
{
//Japan is also possible
}
if (possibleCountries Countries.England != Countries.England)
{
//England not possible
}
Mind you that’s C# (and off the top of my head). Now JavaScript doesn’t actually have support for enums so you’ll want to do some experimentation I suppose to see if this is possible in UnityScript.
EDIT: AkilaeTribe, in C# you can assign your own integers (or byte), but it’s typically not the case to make your code dependant on the underyling values. If you want to make enums that can have arbitrary underlying values (such as strings, floats, etc.) then you may want to consider creating a class with predefined readonly static instances with a private constructor (again, C#, off the top of my head):
public class MyOptions
{
public static readonly MyOptions Height = new MyOptions("Height", 100.0f);
public static readonly MyOptions Width = new MyOptions("Width", 20.0f);
public static readonly MyOptions IsLocked = new MyOptions("IsLocked", 1.0f);
private string m_Name;
public string Name { get { return this.m_Name; } }
private float m_Value;
public float Value { get { return this.m_Value; } }
private MyOptions(string name, float value)
{
this.m_Name = name;
this.m_Value = value;
}
}
MyOptions theOptionValue = MyOptions.IsLocked;
if (theOptionValue == MyOptions.IsLocked)
{
if (theOptionValue.Value == 1.0f)
{
Debug.Log("Option Name: " + theOptionValue.Name);
//do stuff
}
}
It’s not any different in Unityscript.
–Eric
Maybe he is confusing it with Actionscript which does not support enums.
Neither JavaScript, ActionScript (at least ActionScript 2.0, not sure about 3.0) nor ECMAScript (upon which all of these languages are based upon) have enumeration support. There are various ways you can hack one together (it’s still not a proper enum) but you lose type-safety (but if you’re using UnityScript, I don’t think you’re concerned with that). Thus if you do use this UnityScript enum, use with caution as that language support doesn’t exist in any of the traditional ECMAScript implementations to use for comparison.
JScript.NET does, however, and as I’ve noted before, Unityscript and JScript.NET are nearly identical.
–Eric
Good stuff. I suppose it’s fair to say then that it’s more accurate to associate UnityScript with JScript.NET than JavaScript?
(btw, ActionScript 3.0 doesn’t support enums either apparently)
Yep. I think all the JScript.NET code I’ve seen on MSDN would compile without changes in Unity.
–Eric