HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

CScache Attached Variables Problem

07-25-2007, 05:43 AM#1
waaaks
ive been studying attached variables in CS_Cache, but still didnt get my problem solved, so ill ask it directly to the master

Collapse JASS:
local unit u = GetTriggerUnit()
call AttachObject( GetTriggerUnit(), "caster", gg_trg_AWD )
does this mean GetTriggerUnit() is attached to trigger AWD?
or trigger AWD is attached to GetTriggerUnit()?

Collapse JASS:
local unit e = GetAttachedUnit(GetTriggerUnit(), "caster")
does this mean that i can use GetTriggerUnit from the actions and get it anywhere?

why cant i use this?
Collapse JASS:
local unit u = GetTriggerUnit()
call AttachObject(u, "caster", gg_trg_AWD)

can anyone post a trigger that uses attached variables correctly?

i need an answer for this to make a spell that blocks damage dealt to the buffed unit

for Tables
can anyone post a trigger that a table is used, i need a short trigger that clarifies the uses of tables?
they said tables are better than attached variable, so i think i will need it...
07-25-2007, 07:24 AM#2
Pyrogasm
Quote:
Originally Posted by waaaks
Collapse JASS:
local unit u = GetTriggerUnit()
call AttachObject( GetTriggerUnit(), "caster", gg_trg_AWD )
does this mean GetTriggerUnit() is attached to trigger AWD?
or trigger AWD is attached to GetTriggerUnit()?
The trigger is attached to the unit.

Quote:
Originally Posted by waaaks
Collapse JASS:
local unit e = GetAttachedUnit(GetTriggerUnit(), "caster")
does this mean that i can use GetTriggerUnit from the actions and get it anywhere?
What exactly are you asking? Just because you attach GetTriggerUnit() to something doesn't mean that it becomes GetTriggerUnit() in any function. If I had to guess an answer to your question (being how I don't entirely understand what you're asking), I'd say "yes".

Quote:
Originally Posted by waaaks
why cant i use this?
Collapse JASS:
local unit u = GetTriggerUnit()
call AttachObject(u, "caster", gg_trg_AWD)
You can do that, but it's nearly pointless as it attaches the trigger to the unit instead of the other way around.

Quote:
Originally Posted by waaaks
can anyone post a trigger that uses attached variables correctly?
Here is a script by Blade.dk that creates, however inefficiently, mobile lightning effects using gamecache and a timer:
Collapse JASS:
function AddLightningFromUnitToUnitTimed_Child takes nothing returns nothing
    local timer t = GetExpiredTimer()
    local unit from = GetAttachedUnit(t, "from")
    local unit to = GetAttachedUnit(t, "to")
    local lightning bolt = GetAttachedLightning(t, "bolt")
    local real z1 = GetUnitFlyHeight(from)
    local real z2 = GetUnitFlyHeight(to)
    if z1 == 0 then
        set z1 = 60
    endif
    if z2 == 0 then
        set z2 = 60
    endif
    call MoveLightningEx(bolt, GetAttachedBoolean(t, "checkVisibility"), GetUnitX(from), GetUnitY(from), z1, GetUnitX(to), GetUnitY(to), z2)
    call AttachReal(t, "timeelapsed", GetAttachedReal(t, "timeelapsed")+0.01)
    if ((GetAttachedReal(t, "timeelapsed") > GetAttachedReal(t, "duration")) or (GetUnitState(from, UNIT_STATE_LIFE) <= 0) or (GetUnitState(to, UNIT_STATE_LIFE) <= 0)) then
        call CleanAttachedVars(t)
        call DestroyTimer(t)
        call DestroyLightning(bolt)
    endif
    set t = null
    set from = null
    set to = null
    set bolt = null
endfunction

function AddLightningFromUnitToUnitTimed takes string codeName, boolean checkVisibility, unit from, unit to, real duration returns lightning
    local timer t = CreateTimer()
    local lightning bolt
    local real z1 = GetUnitFlyHeight(from)
    local real z2 = GetUnitFlyHeight(to)
    if z1 == 0 then
        set z1 = 60
    endif
    if z2 == 0 then
        set z2 = 60
    endif
    set bolt = AddLightningEx(codeName, checkVisibility, GetUnitX(from), GetUnitY(from), z1, GetUnitX(to), GetUnitY(to), z2)
    call AttachReal(t, "duration", duration)
    call AttachObject(t, "from", from)
    call AttachObject(t, "to", to)
    call AttachBoolean(t, "checkVisibility", checkVisibility)
    call AttachObject(t, "bolt", bolt)
    call TimerStart(t, 0.01, true, function AddLightningFromUnitToUnitTimed_Child)
    return bolt
endfunction

Quote:
Originally Posted by waaaks
i need an answer for this to make a spell that blocks damage dealt to the buffed unit
Your simplest solution is to create an ability based off of a mountain giant's "Hardened Skin" ability. Then, make it reduce 1.00 damage, set the "Percent" field to true, and set the minimum damage to be reduced to cap to 0.00.

Then, simply add the ability to a unit when needed and then run a periodic check for all units that have that ability but not the buff. If a unit matches this condition, remove the ability from it.

Quote:
Originally Posted by waaaks
for Tables
can anyone post a trigger that a table is used, i need a short trigger that clarifies the uses of tables?
they said tables are better than attached variable, so i think i will need it...
Well, tables are rather simple. All it does is remove the I2S(CS_H2I(h)) call from the setting/getting of attached variables. When you call GetAttachmentTable(...), you're getting the string that is the domain (first level of categorization) of the handle index in the GameCache and simply using that each time you attach/get something instead of getting it over and over again. Take, for example, part of my Custom Sleep Functions:
Collapse JASS:
//==========================================================\\
//            Pyrogasm's Custom Sleep Functions             \\
//                                                          \\
//                       Version 1.00                       \\
//==========================================================\\

//##########################################################\\
//                    Start Configuration                   \\
//##########################################################\\

constant function CSF_SleepAbilityId takes nothing returns integer
    return 'A000' //Rawcode of the "__CSF Dummy Sleep" spell
endfunction

constant function CSF_DummyUnitId takes nothing returns integer
    return 'e000' //Rawcode of your dummy unit
endfunction

constant function CSF_SleepStunTime takes nothing returns real
    return 2.00   //This is the "Stun Duration" field of the sleep ability
                  //It determines how long a slept unit is invulnerable for
endfunction

constant function CSF_SleepBuffId takes nothing returns integer
    return 'BUsl' //Rawcode of the "Sleep" buff that your map uses
                  //This should be the same on all spells based on Sleep
endfunction

constant function CSF_SleepPauseBuffId takes nothing returns integer
    return 'BUsp' //Rawcode of the "Sleep (Pause)" buff that your map uses
                  //This should be the same on all spells based on Sleep
endfunction

constant function CSF_SleepStunBuffId takes nothing returns integer
    return 'BUst' //Rawcode of the "Sleep (Stun)" buff that your map uses
                  //This should be the same on all spells based on Sleep
endfunction

//function CSF_InvulnerableBuff takes unit whichUnit returns boolean
//    if GetUnitAbilityLevel(whichUnit, 'BHds') > 0 then //Divine Shield
//        return true
//    elseif GetUnitAbilityLevel(whichUnit, 'Bvul') > 0 then
//        return true     //Repeat this and the above line for each different
//    endif               //Invulnerability buff in your map; change 'Bvul'
//    return false        //To the rawcode of the buff
//endfunction
//##########################################################\\
//                     End Configuration                    \\
//##########################################################\\

function CSF_EndSleep takes nothing returns nothing
    local timer T = GetExpiredTimer()
    local string TTable = GetAttachmentTable(T)
    local unit whichUnit = GetTableUnit(TTable, "CSF whichUnit")
    local string UTable = GetAttachmentTable(whichUnit)
    local integer ActiveSleeps = GetTableInt(UTable, "CSF Active Sleeps")

    set ActiveSleeps = ActiveSleeps-1
    if ActiveSleeps == 0 then
        call UnitRemoveAbility(whichUnit, CSF_SleepBuffId())
        if GetTableBoolean(TTable, "CSF Short Duration") then
//            call SetUnitInvulnerable(whichUnit, false)
            call UnitRemoveAbility(whichUnit, CSF_SleepPauseBuffId())
            call UnitRemoveAbility(whichUnit, CSF_SleepStunBuffId())
            call SetTableBoolean(TTable, "CSF Short Duration", false)
        endif
    endif
    call SetTableInt(UTable, "CSF Active Sleeps", ActiveSleeps)

    call PauseTimer(T)
    call CleanAttachedVars(T)
    call DestroyTimer(T)
    set T = null
    set whichUnit = null
endfunction

function SleepUnit takes unit whichUnit, real Duration, boolean OverRide returns nothing
    local unit Dummy
    local timer T
    local string Table

    if (not(OverRide) and GetUnitAbilityLevel(whichUnit, CSF_SleepBuffId()) == 0) or OverRide then
        set Dummy = CreateUnit(GetOwningPlayer(whichUnit), CSF_DummyUnitId(), GetUnitX(whichUnit), GetUnitY(whichUnit), 0.00)
        call UnitAddAbility(Dummy, CSF_SleepAbilityId())
        call IssueTargetOrder(Dummy, "sleep", whichUnit)
        call UnitApplyTimedLife(Dummy, 'BTLF', 2.00)

        set T = CreateTimer()
        set Table = GetAttachmentTable(T)
        call SetTableObject(Table, "CSF whichUnit", whichUnit)
        if Duration < CSF_SleepStunTime() then
            call SetTableBoolean(Table, "CSF Short Duration", true)
        endif
        call TimerStart(T, Duration, false, function CSF_EndSleep)

        set Table = GetAttachmentTable(whichUnit)
        call SetTableInt(Table, "CSF Active Sleeps", GetTableInt(Table, "CSF Active Sleeps")+1)

        set Dummy = null
        set T = null
    endif
endfunction
Note that you still have to clean attached variables with the CleanAttachedVars(...) function even when using tables.
07-25-2007, 01:33 PM#3
Vexorian
hmmm am I a victim of dejavu?
07-25-2007, 08:30 PM#4
Pyrogasm
Yup; I've seen this before somewhere too...
07-26-2007, 04:38 AM#5
waaaks
yeah i posted this thread in vexorians spells and systems, but after 3 days, theres no one who replies, so i posted it here

anyways, thanks pyro +rep