• 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.

How to map unit type ids to integer

Status
Not open for further replies.
Level 15
Joined
Aug 7, 2013
Messages
1,338
Hi,

I'd like to be able to map unit type ids (e.g. 'H001') to integer values. To simplify things I've made an integer array that maps integers to the unit type ids, but I'd also like to be able to go in the reverse, and I don't think it's possible with an array, as the indices would be too large.

Would it be as simple as just having a function that does it?

JASS:
function foo takes integer a returns integer
  if a == 'H001':
    return 0
.
.
.
endfunction

Or is there a better way to do this?
 
Level 15
Joined
Aug 7, 2013
Messages
1,338
Can you please explain your question better.

If you put all unit type ids in an array then you can use that array.

Unless you mean something like this.

unitTypeArray[ 'H001'] = 0

In the case above you can't use 'H001' as an index. You would then need to use a hashtable.

I have this array so far, which I (arbitrarily) fill.

JASS:
globals
integer array indexToId
endglobals

function init takes nothing returns nothing
set indexToId[0] = 'H001'
set indexToId[1] = 'H049'
set indexToId[2] = 'H01E'
...
endfunction

Now I would like to be able to go in the reverse, i.e. given the unit type in the form of 'XXXX', return the index which maps to it in the array
JASS:
 indexToId
.

Ideally I would like to be able to the same as above, but the unit type values are too high. So basically I just need an inverse of the function/array above.

JASS:
function someFunc takes integer returns integer
if integer == 'H001'
  return 0
elseif integer == 'H049'
  return 1
else //integer == 'H01E'
  return 2
endif
endfunction

This works, but if my
JASS:
 indexToId
array is large (like size 200), I would need to go through 200 if statements, where as going in the other direction is constant time, since it's set in the array.

So is there a clever way to accomplish this (without controlling the unit type ids of custom units)?
 
Level 15
Joined
Aug 7, 2013
Messages
1,338
Alright where is the syntax and documentation for said hash table?

I've read two entire manuals (the one provided by Jeff Pang, and the vJass manual) and the only mention of hash table was that it might be the underlying data structure for how arrays are actually implemented.
 
Level 15
Joined
Aug 7, 2013
Messages
1,338
JASS:
globals
    hashtable ht = InitHashtable()
endglobals

function load takes integer id returns integer
    return LoadInteger(ht, id, 0)
endfunction

function init takes nothing returns nothing
    call SaveInteger(ht, 'hfoo', 0, 1)
endfunction

This maps 1 to hfoo unit type (footman).

Ah this is it, but I want to map the 'hfoo' unit type to 1, not the other way around.

JASS:
LoadInteger takes hashtable table, integer parentKey, integer childKey returns integer

SaveInteger takes hashtable table, integer parentKey, integer childKey, integer value returns nothing

So assuming I made the hashtable ht, if I do:

JASS:
SaveInteger(ht, 'hfoo', 0, 0)
...
Load('hfoo') == 0 //this will return true?
 
Status
Not open for further replies.
Top