- Home /
Question by
joemost29 · Jul 07, 2015 at 05:45 PM ·
javascriptinventoryenum
Javascript Enum Question
Hey Guys, Making a simple Javascript inventory class and just have a quick question
{
var ItemName : String;
var ItemId : Int;
var ItemIcon : Texture; //the 2D image of it for inventory use basically
var ItemType : itemtype; //referencing the enum created below
}
//enum(s)
//basically an enum makes an array of the possible values an item could have
enum itemtype = {weapon, food, potion, misc, material, tool}
I was thinking about adding a conditional set of values in the class to it, like lets say are only values if the enum itemtype is set to 'weapon'
Would it be as easy as saying if (ItemType === weapon) { var ItemMaterail : String}
Any advice would be amazing!
Comment
Answer by YoungDeveloper · Jul 07, 2015 at 05:48 PM
enum ITEM_TYPE = {weapon, food, potion, misc, material, tool}
private var itemType : ITEM_TYPE;
if(itemType == ITEM_TYPE.weapon){
}
if you need a lot of checks id suggest using switch case tree.
switch(itemType){
case ITEM_TYPE.weapon:
//do something with weapon
break;
case ITEM_TYPE.food:
//do something with food
break;
}
is this a switch case tree example (https://unity3d.com/learn/tutorials/modules/beginner/scripting/switch)
Ahh okay, so I was definetly off coding wise..
yes it is, its much faster than lots of if else checking
Your answer
