• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

[JASS] Simplify multiple "or" conditions

Status
Not open for further replies.
Level 11
Joined
Sep 14, 2009
Messages
284
Is there anyway to simplify this JASS line
JASS:
    return GetUnitTypeId(GetTriggerUnit()) == udg_ZoneEnemies[1] or GetUnitTypeId(GetTriggerUnit()) == udg_ZoneEnemies[2] or GetUnitTypeId(GetTriggerUnit()) == udg_ZoneEnemies[3] or GetUnitTypeId(GetTriggerUnit()) == udg_ZoneEnemies[4] or GetUnitTypeId(GetTriggerUnit()) == udg_ZoneEnemies[5]

To something like this:
JASS:
    return GetUnitTypeId(GetTriggerUnit()) == udg_ZoneEnemies[1, 5]
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,287
Is there anyway to simplify this JASS line
IcemanBo's approach is probably the highest performance, most scalable and easiest to maintain way. Although hastables are complex calls they are still a lot faster than multiple comparisons as they involve less name resolution (their performance is worse but still comparable to that of an array lookup). The nature of hashtables means that if you are comparing 5 or 5,000 unit types it should still take around the same time to execute.

That said his return statement throws a syntax error as he forgot the hashtable parameter of LoadBoolean.

At some stage returning non-existant values from hashtables was prone to produce a fatal error in WC3. As such I would recommend using "HaveSavedBoolean" to check if it is the appropriate unit type since you only declare values from unit types you want to return true.

JASS:
return HaveSavedBoolean(hash, GetUnitTypeId(GetTriggerUnit()), 0)
Since only the types which should return true are stored in the hashtable, it will only return true for them. Unlike LoadBoolean which could try to fetch a non-existant boolean, HaveSavedBoolean explicitly defines the return value for such a case and does not attempt any access. You would still want to use LoadBoolean in the case that you explicitly store false or true values at certain indices but only as long as you store something there in the first place.
 
Status
Not open for further replies.
Top