Giving an item back to the unit after drop

luorax

Invasion in Duskwood
Reaction score
67
How can I immediately give the item back to its owner if it drops it? Because this crashes my WC3:

JASS:
library InventorySystem requires Table,ItemList,SimError,Status,Champion,AII
        
    private struct Data extends array
        boolean sold
        boolean dropped
        
        private method AII_onDestroy takes nothing returns nothing
            set this.sold=false
            set this.dropped=false
        endmethod
        
        implement AII
    endstruct
    
    //The Inventory struct..
    struct Inventory
        unit Owner
        integer ShopId
        Champion Champ

        public static HandleTable table
        
        //creates a new Inventory for the unit
        static method create takes unit whichUnit, Champion c returns thistype
            local thistype this = 0
            if not thistype.table.exists(whichUnit) then
                set this = thistype.allocate()
                set this.Owner = whichUnit
                set this.ShopId = GetPlayerId(GetOwningPlayer(this.Owner))
                set this.Champ = c

                call TriggerRegisterUnitEvent(thistype.Pickup, this.Owner, EVENT_UNIT_PICKUP_ITEM)
                call TriggerRegisterUnitEvent(thistype.Drop, this.Owner, EVENT_UNIT_DROP_ITEM)
                call TriggerRegisterUnitEvent(thistype.Sell, this.Owner, EVENT_UNIT_PAWN_ITEM)
                call TriggerRegisterUnitEvent(thistype.LevelUp, this.Owner, EVENT_UNIT_HERO_LEVEL)
                
                set thistype.table[this.Owner] = this
                return this
            endif
            return 0
        endmethod
        
        //will be runned whenever an item is picked up
        private static method pickupItem takes nothing returns boolean
            if Data[GetManipulatedItem()].dropped then
                set Data[GetManipulatedItem()].dropped=false
            else
                call thistype.equipItem(GetTriggerUnit(),GetManipulatedItem())
            endif
            return false
        endmethod
        
        //Will be runned whenever an item is dropped
        private static method dropItem takes nothing returns boolean
            if not Data[GetManipulatedItem()].sold then
                set Data[GetManipulatedItem()].dropped=true
                call UnitAddItem(GetTriggerUnit(),GetManipulatedItem())
                call SimError(GetOwningPlayer(GetTriggerUnit()), "You can't drop items!")
            endif
            return false
        endmethod
        
        //Will be runned whenever an item is sold
        private static method sellItem takes nothing returns boolean
            set Data[GetSoldItem()].sold=true
            call thistype.unequipItem(GetTriggerUnit(),GetSoldItem())
            return false
        endmethod
        
        private static method levelUpAct takes nothing returns boolean
            local unit u=GetTriggerUnit()
            local Champion c=Champion.getChampionById(GetUnitTypeId(u))
            local real life=GetUnitState(u,UNIT_STATE_LIFE)
            local real mana=GetUnitState(u,UNIT_STATE_MANA)

            call Status<u>.modHealthBonus(c.healthPerLevel)
            call Status<u>.modManaBonus(c.manaPerLevel)
            call Status<u>.modHealthRegenBonus(c.healthRegenPerLevel/5)
            call Status<u>.modManaRegenBonus(c.manaRegenPerLevel/5)
            call SetHeroStr(u, GetHeroStr(u,false)+c.resistancePerLevel, true)
            
            call SetPlayerTechResearched(GetOwningPlayer(u),&#039;UPGA&#039;,GetHeroLevel(u)-1)
            call SetPlayerTechResearched(GetOwningPlayer(u),&#039;UPGD&#039;,(GetHeroLevel(u)-1)*c.damagePerLevel)
            
            set DamageStats<u>.AttackSpeedMultiplier=DamageStats<u>.AttackSpeedMultiplier+c.attackSpeedPerLevel
            
            call SetUnitState(u,UNIT_STATE_LIFE,life+c.healthPerLevel)
            call SetUnitState(u,UNIT_STATE_MANA,mana+c.manaPerLevel)
            
            set u = null
            return false
        endmethod

        private static trigger Pickup = CreateTrigger()
        private static trigger Drop = CreateTrigger()
        private static trigger LevelUp = CreateTrigger()
        private static trigger Sell = CreateTrigger()
        
        //initialization of the Inventory struct
        private static method onInit takes nothing returns nothing
            set thistype.table=HandleTable.create()
            call TriggerAddCondition(thistype.Pickup, Condition(function thistype.pickupItem))
            call TriggerAddCondition(thistype.Drop, Condition(function thistype.dropItem))
            call TriggerAddCondition(thistype.Sell, Condition(function thistype.sellItem))
            call TriggerAddCondition(thistype.LevelUp, Condition(function thistype.levelUpAct))
        endmethod
        
    endstruct
    
endlibrary
</u></u></u></u></u></u>


Any idea why?
 

luorax

Invasion in Duskwood
Reaction score
67
I was not sure. Maybe some GUI users have an idea how to do it (because my problem is simple, you don't have to know JASS to understand it).
I can easily write it in JASS if they have any idea how to give the item back.

EDIT: I got it. This is the result for those who're curious:

JASS:
library InventorySystem requires Table,ItemList,SimError,Status,Champion,AII
        
    private struct Data extends array
        boolean sold
        boolean dropped
        unit owner
        
        private method AII_onDestroy takes nothing returns nothing
            set this.sold=false
            set this.dropped=false
        endmethod
        
        implement AII
    endstruct
    
    //The Inventory struct..
    struct Inventory
        unit Owner
        integer ShopId
        Champion Champ

        public static HandleTable table
        
        //creates a new Inventory for the unit
        static method create takes unit whichUnit, Champion c returns thistype
            local thistype this = 0
            if not thistype.table.exists(whichUnit) then
                set this = thistype.allocate()
                set this.Owner = whichUnit
                set this.ShopId = GetPlayerId(GetOwningPlayer(this.Owner))
                set this.Champ = c

                call TriggerRegisterUnitEvent(thistype.Pickup, this.Owner, EVENT_UNIT_PICKUP_ITEM)
                call TriggerRegisterUnitEvent(thistype.Drop, this.Owner, EVENT_UNIT_DROP_ITEM)
                call TriggerRegisterUnitEvent(thistype.Sell, this.Owner, EVENT_UNIT_PAWN_ITEM)
                call TriggerRegisterUnitEvent(thistype.LevelUp, this.Owner, EVENT_UNIT_HERO_LEVEL)
                
                set thistype.table[this.Owner] = this
                return this
            endif
            return 0
        endmethod
        
        //will be runned whenever an item is picked up
        private static method pickupItem takes nothing returns boolean
            if Data[GetManipulatedItem()].dropped then
                set Data[GetManipulatedItem()].dropped=false
            else
                call thistype.equipItem(GetTriggerUnit(),GetManipulatedItem())
            endif
            return false
        endmethod
        
        private static method dropItemCallback takes nothing returns nothing
            local Data d=GetTimerData(GetExpiredTimer())
            call UnitAddItem(d.owner,d.item)
            call ReleaseTimer(GetExpiredTimer())
        endmethod
        
        //Will be runned whenever an item is dropped
        private static method dropItem takes nothing returns boolean
            local timer t
            local item i
            if not Data[GetManipulatedItem()].sold then
                set t=NewTimer()
                set i=GetManipulatedItem()
                set Data<i>.dropped=true
                set Data<i>.owner=GetTriggerUnit()
                call SetTimerData(t,Data<i>)
                call TimerStart(t,.01,false,function thistype.dropItemCallback)
                set t=null
                set i=null
                call SimError(GetOwningPlayer(GetTriggerUnit()), &quot;You can&#039;t drop items!&quot;)
            endif
            return false
        endmethod
        
        //Will be runned whenever an item is sold
        private static method sellItem takes nothing returns boolean
            set Data[GetSoldItem()].sold=true
            call thistype.unequipItem(GetTriggerUnit(),GetSoldItem())
            return false
        endmethod
        
        private static method levelUpAct takes nothing returns boolean
            local unit u=GetTriggerUnit()
            local Champion c=Champion.getChampionById(GetUnitTypeId(u))
            local real life=GetUnitState(u,UNIT_STATE_LIFE)
            local real mana=GetUnitState(u,UNIT_STATE_MANA)

            call Status<u>.modHealthBonus(c.healthPerLevel)
            call Status<u>.modManaBonus(c.manaPerLevel)
            call Status<u>.modHealthRegenBonus(c.healthRegenPerLevel/5)
            call Status<u>.modManaRegenBonus(c.manaRegenPerLevel/5)
            call SetHeroStr(u, GetHeroStr(u,false)+c.resistancePerLevel, true)
            
            call SetPlayerTechResearched(GetOwningPlayer(u),&#039;UPGA&#039;,GetHeroLevel(u)-1)
            call SetPlayerTechResearched(GetOwningPlayer(u),&#039;UPGD&#039;,(GetHeroLevel(u)-1)*c.damagePerLevel)
            
            set DamageStats<u>.AttackSpeedMultiplier=DamageStats<u>.AttackSpeedMultiplier+c.attackSpeedPerLevel
            
            call SetUnitState(u,UNIT_STATE_LIFE,life+c.healthPerLevel)
            call SetUnitState(u,UNIT_STATE_MANA,mana+c.manaPerLevel)
            
            set u = null
            return false
        endmethod

        private static trigger Pickup = CreateTrigger()
        private static trigger Drop = CreateTrigger()
        private static trigger LevelUp = CreateTrigger()
        private static trigger Sell = CreateTrigger()
        
        //initialization of the Inventory struct
        private static method onInit takes nothing returns nothing
            set thistype.table=HandleTable.create()
            call TriggerAddCondition(thistype.Pickup, Condition(function thistype.pickupItem))
            call TriggerAddCondition(thistype.Drop, Condition(function thistype.dropItem))
            call TriggerAddCondition(thistype.Sell, Condition(function thistype.sellItem))
            call TriggerAddCondition(thistype.LevelUp, Condition(function thistype.levelUpAct))
        endmethod
        
    endstruct
    
endlibrary
</u></u></u></u></u></u></i></i></i>


However better ideas are welcomed.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top