• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[Wurst] Hashtable Alternative

Status
Not open for further replies.
Level 4
Joined
Dec 24, 2010
Messages
67
There's no Table anymore. Getting the data from a unit by its HandleID was quite convenient.

Can you please give me an example code of the best alternative in Wurst? I read about Generics on the manual, but I'm not sure how to implement it.
I don't mind using hashtables, but there's gotta be a better way.
 
While you certainly CAN use Table in wurst, it is advised against. HashMap is a good alternative, that's widely used. Here's a script that uses HashMap to connect the unit's handle id to a class instance. When an unit is indexed, it will create UnitData instance, when it is deindexed, it will be removed.
JASS:
package TableExample
import HashMap
import UnitIndexer

public function unit.getData() returns UnitData
    return UnitData.of(this)

public class UnitData
    static constant map = new HashMap<unit, thistype>

    protected unit u
    protected real realVal
    protected int intVal

    function getRealVal() returns real
        return realVal

    function getIntVal() returns int
        return intVal

    function setRealVal(real val)
        realVal = val

    function setIntval(int val)
        intVal = val

    construct(unit u)
        this.u = u
        map.put(u, this)

    static function of(unit u) returns thistype
        return map.get(u)

    ondestroy
        map.remove(u)

init
    onUnitIndex(() -> new UnitData(getIndexedUnit()))
    onUnitDeindex(() -> destroy UnitData.of(getDeindexedUnit()))

This allows you to also use unit.getData() which is really handy. This makes things like GetTriggerUnit().getData().getRealVal() possible.
 
Last edited:
Level 4
Joined
Dec 24, 2010
Messages
67
I see so that's how to use that frickin HashMap! Was so frustrated about that.
Thanks again, I'll study this.
 
It is confusing because in vJass you had to explicitly write code that gets an integer index out of everything (else you had to use the table/hashtable directly).

In case of units, it's the unit id. In case of strings, it's the string hash, in case of int, it is simply int. In case of reals, it is ints multiplied by a number large enough to make it an int, and so on.

Wurst converts everything to an ID if possible and necessary, meaning that even HashMap<unit, someClass> is actually in reality HashMap<int, int>, but you never see those ints because things are converted automatically by the wurst's generics system.

In this way, HashMap<unit, UnitData> in reality maps <unit handle id, class instance id>, but you don't have to deal with that crap, you can just abstractize it as HashMap<unit, unitData> and focus on making your code work instead.
 
Level 4
Joined
Dec 24, 2010
Messages
67
Wurst converts everything to an ID
Yeah no kidding, it actually made me think twice about using Wurst. I was like "what the hell, this is too complicated!" an hour ago. But I guess I'll get used to it overtime.
 
Yeah no kidding, it actually made me think twice about using Wurst. I was like "what the hell, this is too complicated!" an hour ago. But I guess I'll get used to it overtime.

Ironically, it is actually less complicated, because you do not have to think about IDs at all. A lot of people who come from vJass believe they need freedom to do some low level stuff in jass (like directly manipulating IDs or allocating class ids by themselves) but this is simply not true, it is a remnant of vJass mentality where people optimize things they don't need to optimize, and try to do low level stuff themselves, when someone has already done it better many times.
 
Level 4
Joined
Dec 24, 2010
Messages
67
Oh I never thought of it that way. But then again can't deny that I'm one of those people.
I just have to adapt I guess. But you know it's kinda understandable, we got raised to that kind of thinking, naturally we will believe that it is the right thing. It's kinda one-sided at first but we'll outgrow it eventually.
 
Oh I never thought of it that way. But then again can't deny that I'm one of those people.
I just have to adapt I guess. But you know it's kinda understandable, we got raised to that kind of thinking, naturally we will believe that it is the right thing. It's kinda one-sided at first but we'll outgrow it eventually.

Well, precisely. The more you learn the more you see flaws in bad programming languages, that is just how it is. Even Wurst has a ton of flaws, but they are nothing compared to jass and vjass.
 
Status
Not open for further replies.
Top