HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Storing Problem

07-19-2009, 08:23 PM#1
Flame_Phoenix
Hi guys, I am making a test map for a new system I have in my head. The idea was given to me by someone else that remaked Blade.dk health and mana system, now I am trying to complete it.

Basically my problem is: The code works perfectly the first time I call Adjust, when I create the structure. However the structure "aTarget" doesn't seem to be saved inside of table (dunno why) and so when I call the Adjust function for the second time, all is messed up.

Can someone tell me what I am doing wrong please? rep++ awarded and the code is fairly simple and quite easy to read IMO.
This is still under construction by the way, it only supports a few levels now, soon it will support +/- 498 damage max, which is fair for almost any sane map.

Btw, it requires Table.

Collapse JASS:
library AttackMod initializer Init requires Table
//===========================================================================
//=============================SETUP START===================================
//===========================================================================
    globals
        private constant integer ATTACK_AID = 'A000'   //rawcode of the ability that will give bonus attack
        private constant integer MAX_DELTA = 10    //the maximum bonus the unit can have. MUST BE PAIR number.
    endglobals
//===========================================================================
//=============================SETUP END=====================================
//===========================================================================
    globals
        private group unitsWithBonus
        private HandleTable activeTable //your private Table's global variable
    endglobals
    
    private struct aTarget
        unit target
        integer level
        
        static method create takes unit who, integer aidLevel returns aTarget
            local aTarget this = aTarget.allocate()
            
            //setting members
            set .target = who
            set .level = aidLevel + MAX_DELTA + 1
            
            //put the struct in the Table, we just use the caster's
            //handle adress as the key which tells us where in the 
            //Table the struct is stored
            set activeTable[.target] = this 
            
            //add unit to group 
            call GroupAddUnit(unitsWithBonus, .target)
            
            //increase the attack of the unit!
            call UnitAddAbility(.target, ATTACK_AID)
            call SetUnitAbilityLevel(.target, ATTACK_AID, .level)
            
            call BJDebugMsg("createLevel= "+I2S(.level))
            
            return this
        endmethod
        
        method setDelta takes integer newDelta returns boolean
            local integer change = .level + newDelta
            
            call BJDebugMsg("level = "+I2S(.level))
            call BJDebugMsg("newDelta= "+I2S(newDelta))
            call BJDebugMsg("endlevel = "+I2S(change))
            call BJDebugMsg(GetUnitName(.target))
            
            call UnitRemoveAbility(.target, ATTACK_AID)
            
            if (change > (MAX_DELTA*2) + 1) then
                call BJDebugMsg("It is not possible to modify attribute, change MAX_DELTA")
            else
                set .level = .level + newDelta
            
                call UnitAddAbility(.target, ATTACK_AID)
                call SetUnitAbilityLevel(.target, ATTACK_AID, .level)
                
                return true
            endif
            
            return false
        endmethod
        
        method onDestroy takes nothing returns nothing
            //since the spell is not active anymore, we clean the Table      
            call activeTable.flush(.target) 
            call BJDebugMsg("kill!")
            //the units are not anymore in the active units group.
            call GroupRemoveUnit(unitsWithBonus, .target)
            
            //remove the ability from the unit
            call UnitRemoveAbility(.target, ATTACK_AID)
        endmethod
    endstruct
//===========================================================================
    public function Adjust takes unit who, integer delta returns boolean

        //if the user is requesting a level to high or to low, we refuse it
        if IAbsBJ(delta) > MAX_DELTA then
            call BJDebugMsg("The delta is too high or too low. Please fix MAX_DELTA value")
        else
            
            //if the unit is in our group, we simply change the level of the ability.
            //if not, we create a place for it and to the rest.
            if IsUnitInGroup(who, unitsWithBonus) then
                call aTarget(activeTable[GetTriggerUnit()]).setDelta(delta)
            else
                call BJDebugMsg(GetUnitName(who))
                call aTarget.create(who, delta)
            endif
            
            return true
        endif
        
        return false
    endfunction
//===========================================================================
    private function DeathConditions takes nothing returns boolean
        if IsUnitInGroup(GetTriggerUnit(), unitsWithBonus) then 
            call aTarget(activeTable[GetTriggerUnit()]).destroy()
        endif
        
        return false
    endfunction
//===========================================================================
    private function Init takes nothing returns nothing
        //when a unit dies, we don't need to keep track of it anymore
        local trigger onDeathTrg = CreateTrigger(  )
        call TriggerRegisterAnyUnitEventBJ(onDeathTrg, EVENT_PLAYER_UNIT_DEATH )
        call TriggerAddCondition(onDeathTrg, Condition(function DeathConditions))
    
        //setting our globals
        set unitsWithBonus = CreateGroup()
        set activeTable = HandleTable.create()
    endfunction
endlibrary

Here is test map!

I give rep++ to people who help!
Attached Files
File type: w3xAttackMod.w3x (23.1 KB)
07-19-2009, 08:29 PM#2
Blubb-Tec
Collapse JASS:
            if IsUnitInGroup(who, unitsWithBonus) then
                call aTarget(activeTable[GetTriggerUnit()]).setDelta(delta)
            else
                call BJDebugMsg(GetUnitName(who))
                call aTarget.create(who, delta)
            endif

how about changing that GetTriggerUnit() to who?
07-19-2009, 08:30 PM#3
Flame_Phoenix
Ahh damn, a silly mistake. I can't believe I posted this here now ....
I guess physic exercise DOES make your brain melt xD
rep++, you deserve =P
07-19-2009, 08:31 PM#4
Blubb-Tec
lol, i guess you reply rather fast ~