- Home /
Neater way to test an Enum in a different script?
I have a script terrainInfoJS.js that stores various bits of information about each type of terrain in my game. I have another script unitInfoJS.js that stores info related to the armies/units that populate the map. The terrainInfoJS.js has a series of enums for defining various aspects of the terrain e.g:
enum TerrainType {
unassigned = 0,
grass = 1,
hills = 2,
mountain = 3}
//initialising the enum
var terrainType = TerrainType.unassigned;
The unitInfoJS.js script now needs to test this enumeration so that the correct actions are performed depending on the terrain that the unit occupies. I can't get this to work in a succinct way with lettered variables, only with numbered. For example, this works:
//if terrain is hills
if(terrainInfoJS.terrainType == 2){ }
..but this does not work:
//if terrain is hills
if(terrainInfoJS.terrainType == hills){ }
...and neither does this:
//if terrain is hills
if(terrainInfoJS.terrainType == terrainType.hills){ }
...but this seems to work:
//if terrain is hills
if(terrainInfoJS.terrainType == terrainInfoJS.terrainType.hills){ }
However, only the first way is elegant, but that's not very readable out of context due to the arbitrary numbers from the enum. The second, and third don't work, and the last one seems pretty clunky. Is there a way to set up an Enum so that it can be tested from another script in a readable way with minimal extra code?
Answer by Dave-Carlile · Jun 22, 2013 at 07:47 PM
I believe you want to compare to TerrainType.hills
, not terrainType.hills
. TerrainType
(upper case initial t) refers to the enum, terrainType
(lower case initial t) referes to your variable. Case matters.
Your answer
Follow this Question
Related Questions
C# GetComponent Issue 2 Answers
Accessing Enumeration from another script issue 1 Answer
Simple question about OnMouseButtonAsUp 1 Answer
Type of a script 1 Answer