• 🏆 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!

String to Ability Id Integer

Status
Not open for further replies.
Level 4
Joined
Aug 5, 2007
Messages
49
So I'm trying to create some jass that takes a sold item and adds an ability to the hero.

The thing is, I want the ability added to be 1 of 5 different ones that all do the same thing (just with different hotkeys) depending on the order of sold item.

So say I have 4 warstomp abils for each hotkey (Q,W,E,R) then I sell the "War Stomp" item to the hero, it then adds warstomp Q to the hero. But if I buy it again (for the sake of argument) it adds Warstomp W to the hero.

It gets which ability to add by taking the current slot number, and the base ability as a string.

so if I sell the item I get "WSS" and if it's the first slot I get "1" so together they make "WSS1"

I then need to take that "WSS1" and convert it to the ability id 'WSS1' I can't seem to find the proper method to convert it.

I tried using S21("WSS1") and AbilityId("WSS1") but neither worked.

I have no idea how to pass the trigger 'WSS1' from "WSS1" -.- it looks so easy but I just can't even.
 
Last edited:
Level 13
Joined
Jul 15, 2007
Messages
763
I know you can pull unit-types from integers made from strings, but i'm not sure about abilities. This thread here discussed related topics it in the past: Ability id to integer?

But to my (inferior GUI) mind why not just do something like this?

Event
Hero buys ability

Actions

Set ability[1] = Ability Q,
Set ability[2] = Ability W
Set ability[3] = Ability E
Set ability[4] = Ability R

IF slot[1] is occupied, then check slot[2], else add ability [1]
IF slot[2] is occupied, then check slot[3], else add ability [2]
IF slot[3] is occupied, then check slot[4], else add ability [3]
IF slot[4] is occupied, then cancel purchase, else add ability [4]

Obviously simplified. You'd need to use a boolean to confirm if a slot has been learnt and add all other functions you want.

And setting 4 variables versus being able to automatically identify 3 from 1 isn't that much effort.
 
Level 7
Joined
Jan 23, 2011
Messages
350
Grab a Hashtable
SaveInteger(HASH_VAR, StringHash("WSS1"), 0, WARSTOMP_Q)
SaveInteger(HASH_VAR, StringHash("WSS2"), 0, WARSTOMP_W)
SaveInteger(HASH_VAR, StringHash("WSS3"), 0, WARSTOMP_E)
SaveInteger(HASH_VAR, StringHash("WSS4"), 0, WARSTOMP_R)

If you know how the rawcodes works, you could do something like

JASS:
set i = firstRawcode
set n = 1

loop
    SaveInteger(HASH_VAR, StringHash("WSS"+I2S(n)), 0, i)
    set i = i+1
    set n = n+1
    exitwhen n > 4
endloop

You have to know some shit that i don't know
 
Level 4
Joined
Aug 5, 2007
Messages
49
I ended up finding this while browsing for answers which seems to work as intended.

vJASS:
// Credits to AceHart for this.
library Char2IdLib

    // No need to touch this. Credits to AceHart for this.
    function Char2Id takes string c returns integer
        local integer i = 0
        local string abc = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
        local string t

        loop
            set t = SubString(abc,i,i + 1)
            exitwhen t == null or t == c
            set i = i + 1
        endloop
        if i < 10 then
            return i + 48
        elseif i < 36 then
            return i + 55
        endif
        return i + 61
    endfunction

    // No need to touch this. Credits to AceHart for this.
    function String2Id takes string s returns integer
        return ((Char2Id(SubString(s,0,1)) * 256 + Char2Id(SubString(s,1,2))) * 256 + Char2Id(SubString(s,2,3))) * 256 + Char2Id(SubString(s,3,4))
    endfunction
endlibrary


Now I'm having problems saving and loading strings via hashtables. I can seem to save and load it fine within the same script, but second iterations or outside scripts can't load that hashtable data. It's like it clears the data after each activation.

My code saves the string in a hash, then to test displays that saved data lower in the trigger, which works, but when loading that data at the top of the trigger, or in another trigger, it does not.

The idea in Inc Abil is to check the unit's saved ability slots to see if it's buying an ability it already has, and if so, increase the level of that ability, if not, add the ability to the unit.

I've used several call displays to make sure all the other functions are working, for some reason it loads the one at the end of "addAbility" just fine, but won't load the field at the top of "IncAbil"

vJASS:
function addAbility takes integer pl, item si, unit buy returns nothing
    local integer TI
    local string TS
    set TS = GetAbil(udg_CSMod[pl],si)
    set TI = String2Id(TS)
    call UnitAddAbility(buy, TI)
    call SaveStr(udg_HeroHash,udg_CSMod[pl],GetHandleId(buy),TS)
    call DisplayTextToForce( GetPlayersAll(), LoadStr(udg_HeroHash,udg_CSMod[pl],GetHandleId(buy)))
    set udg_CSMod[pl] = udg_CSMod[pl] + 1
endfunction

function IncAbil takes unit u, integer pl, item si returns nothing
    local integer in = GetItemCharges(si)
    local string an
    local string ts
    local integer iA = 1
    local integer iB = 1
    local boolean tr = false
    loop
        exitwhen iA == 8
        set iB = 1
            loop
                exitwhen iB == 8
                set an = GetAbil(iA,si)
                set ts = LoadStr(udg_HeroHash, iB, GetHandleId(u))
            if(ts == an) then
                call IncUnitAbilityLevel(u,String2Id(an))
                set tr = true
                call DisplayTextToForce( GetPlayersAll(), ts +"||"+an )
            endif
            set iB = iB+1
        endloop
        set iA = iA+1
    endloop
    if(tr == false) then
    call addAbility(pl,si,u)
    endif
endfunction
 
Status
Not open for further replies.
Top