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

[Trigger] Custom Value, but more than one!

Status
Not open for further replies.
Level 6
Joined
Apr 29, 2008
Messages
94
Actualy im talking about the "Item- Set custom value" trigger, but cant i do it somthing like "Item- Set custom value (1/2/3...)" via JASS or somthing?

I mean, i need to find a way to add multiple values to a specific item...
 
Level 40
Joined
Dec 14, 2005
Messages
10,532
Whee, vJass.

JASS:
struct ItemCustomValue
    integer cv1 = 0 //may as well assign a default value
    //type name [= value]
    //etc
endstruct

//to apply it to an item

call SetItemUserData(someItem,ItemCustomValue.create())

//to use it

local ItemCustomValue dat = GetItemUserData(someItem)
//use dat.cv1 and any other values. For example:
call BJDebugMsg("The value of dat.cv1 is: " + I2S(dat.cv1))

//to destroy it, when you're done with the item to prevent an index leak

call dat.destroy()
 
Level 40
Joined
Dec 14, 2005
Messages
10,532
Note that you can use any native type except code - you aren't limited to integers. You can even have arrays, but that has some side effects.

You can have a maximum of 8190 instances, but that should be fine for you (you can increase them but it makes the map script longer and the code a bit slower).
 
Level 6
Joined
Apr 29, 2008
Messages
94
give every unit a custom value when they enter the map (so first units value will be 1, second's 2 etc). then make real variables Value1 and Value2 etc (huge arrays). then set them to the unit's values like this:

set Value2[custom value of [unit]] to [whatever value u want]

A very smart way, how have'nt i thought of this? -.-..

Thanks!
 
Last edited:
Level 40
Joined
Dec 14, 2005
Messages
10,532
give every unit a custom value when they enter the map (so first units value will be 1, second's 2 etc). then make real variables Value1 and Value2 etc (huge arrays). then set them to the unit's values like this:

set Value2[custom value of [unit]] to [whatever value u want]
Needs a good indexing algorithm, which is possible but already implemented in structs (and such it is much easier to just use them).
 
Level 40
Joined
Dec 14, 2005
Messages
10,532
i didnt understand a word of that oO
To index units by custom value, you need a good algorithm (efficient, thorough, relatively low-cost, and accurate), and seeing as the struct example I provided has such an algorithm built into it (in vJass) there really isn't any reason to do it another way if you have vJass available (which everyone should).
 
Level 4
Joined
Mar 14, 2009
Messages
98
Personally, I would go for PurplePoot's solution if you know JASS as it offers a much more efficient method of getting indexes. Doom_Sheep's method would just run out of indexes after 8192 units have entered your map. Well, of course you could filter stuff so it'd be 8192 units matching a certain condition. Still, the point stands that if your map plays for a long enough, the system will stop working at some point. If you have a really, really quick map, it shouldn't really matter though.

Oh, and an indexing algorithm is something that gives you an unused index. The simplest example of one would be what doom_sheep suggested, which is incrementing a global index every time you get an index. It doesn't allow recycling of unused indexes though, so you'd have a limited amount of indexes to be used.

More efficient indexing algorithms allow recycling of indexes, so you're only limited by the number of simultaneous indexes used. vJASS has a built-in indexing algorithm for structs, and that's what PurplePoot used. You get an index by using .allocate() and free an index by using .destroy(). To use vJASS, I believe you need to use JNGP (JassNewGenPack).

Again, if you're making a really, really quick map, it shouldn't really matter as you're probably not going to hit 8192 units anyway.

P.S. I'm not exactly sure about the 8192 limit. It could be 8191 or 8190 or something close to that.
 
Level 16
Joined
Oct 12, 2008
Messages
1,570
Going for Doom_Sheep's way is the exact same as PurplePoots way,, Only Purplepoot uses the (as i like to call it) illusion of vJass.

Only Doom_Sheep's way is probably less accurate/efficient because you have to do it yourself, which might become less efficient.
(vJass did it for you with the simple action of local STRUCTNAME variablename = STRUCTNAME.allocate())
 
If you have NewGen + Latest patch you could do this..

JASS:
library UserData initializer InitTrig

    globals
        private hashtable table
    endglobals
    
    struct userdata
        method store takes unit u, integer data returns nothing
            call SaveInteger(table, GetHandleId(u), this, data)
        endmethod
        method get takes unit u returns integer
            return LoadInteger(table, GetHandleId(u), this)
        endmethod
    endstruct
    
    private function InitTrig takes nothing returns nothing
        set table = InitHashtable()
    endfunction
    
endlibrary

JASS:
    private function test takes nothing returns nothing
        local userdata u = userdata.create()
        local unit x = CreateUnit(Player(0), 'hfoo', 0, 0, 0)
        call u.sett(x, 50)
        call BJDebugMsg(I2S(u.get(x)))
    endfunction
 
Level 17
Joined
Jun 17, 2007
Messages
1,433
You should really post an example that actually works... ;)

JASS:
    private function test takes nothing returns nothing
        local userdata u = userdata.create()
        local unit x = CreateUnit(Player(0), 'hfoo', 0, 0, 0)
        call u.store(x, 50)
        call BJDebugMsg(I2S(u.get(x)))
    endfunction
 
Level 6
Joined
Apr 29, 2008
Messages
94
Wow.. you all went on the "hardcore" ways with JASS and vJASS but I only needed a way to add few low-weighted Custom Values to some items..

Anyways, very useful explenations and tips but will keep using doom_sheep's GUI way, yup I'm naab. :hohum:

Thanks guys! :thumbs_up:


+REP
 
Status
Not open for further replies.
Top