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

[vJASS] Access created item in hooked function

Status
Not open for further replies.
Hi everyone

I am trying to get access to items that are created by "CreateItemLoc()" and "CreateItem()" functions

Lets say I have this:
JASS:
hook CreateItemLoc CreateItemLocEx
and
JASS:
function CreateItemLocEx takes integer itemId, location loc returns item
call BJDebugMsg(GetItemName(bj_lastCreatedItem)) // failed to get last created item from hooked function
    return bj_lastCreatedItem
endfunction
How can access that bj_lastCreatedItem within CreateItemLocEx()?

I also discovered that when the function below is placed inside CreateItemLocEx(), no items is created at all:
JASS:
call TimerStart(t, 0.00, false, function DoNothing)
This applies to any attached function. The bottom line is, timers won't work at all :(

Thanks for your time
 
Level 18
Joined
Nov 21, 2012
Messages
835
please check for other natives/BJ functions that create item, Im not sure if thats all ;)

JASS:
globals
    timer               t
    rect                  r
    unit                  tempUnit=null
endglobals

hook CreateItemLoc CreateItemLocEx                                  //1
hook UnitAddItemByIdSwapped UnitAddItemByIdSwappedEx //2
hook CreateItem CreateItemEx                                            //3
hook UnitAddItemById UnitAddItemByIdEx                            //4
hook UnitAddItemToSlotById UnitAddItemToSlotByIdEx         //5
//-------------------------------------------------------------------
//-------------------------------------------------------------------
//these 2 for demo only
function IsItemIndexed takes item i returns boolean
    return GetItemUserData(i)>0
endfunction
function IndexItem takes item i returns nothing
    call SetItemUserData(i, 1)
endfunction



//-------------------------------------------------------------------
function CreateItem1 takes nothing returns item
    call BJDebugMsg(GetItemName(bj_lastCreatedItem))
    return bj_lastCreatedItem
endfunction
//-------------------1-------------------------------------------------------------
function CreateItemLocEx takes integer itemId, location loc returns nothing
    call TimerStart(t, 0.00, false, function CreateItem1)  
endfunction
//-------------------2-------------------------------------------------------------
function UnitAddItemByIdSwappedEx takes integer itemId, unit whichHero returns nothing
    call TimerStart(t, 0.00, false, function CreateItem1)
endfunction
//-------------------3-------------------------------------------------------------
function EnumItm takes nothing returns nothing
    local item i = GetEnumItem()
    if not IsItemIndexed(i) then
        call IndexItem(i)
        call BJDebugMsg("indexing item: " + GetItemName(i))
    endif
    set i=null
endfunction
function SearchInRect takes nothing returns nothing
    call EnumItemsInRect(r, null, function EnumItm)
endfunction
function CreateItemEx takes integer itemid, real x, real y returns nothing
    call MoveRectTo(r, x, y)  
    call TimerStart(t, 0.00, false, function SearchInRect)
endfunction
//-------------------4-------------------------------------------------------------
function SearchOnUnitThenInRect takes nothing returns nothing
    local integer x=0
    loop
        if not IsItemIndexed(UnitItemInSlot(tempUnit, x)) then
            call IndexItem(UnitItemInSlot(tempUnit, x))
            call BJDebugMsg("indexing item: " + GetItemName(UnitItemInSlot(tempUnit, x)) + " in slot " + I2S(x))
            return
        endif
        set x=x+1
        exitwhen x==bj_MAX_INVENTORY
    endloop
    //if non-indexed item not found on "tempUnit" unit (inventory may be full) then look at the ground
    call EnumItemsInRect(r, null, function EnumItm)
endfunction

function UnitAddItemByIdEx takes unit whichUnit, integer itemId returns nothing
    call MoveRectTo(r, GetUnitX(whichUnit), GetUnitY(whichUnit))  
    set tempUnit=whichUnit
    call TimerStart(t, 0.00, false, function SearchOnUnitThenInRect)
endfunction

//-------------------5-------------------------------------------------------------
function UnitAddItemToSlotByIdEx takes unit whichUnit, integer itemId, integer itemSlot returns nothing
    call UnitAddItemByIdEx(whichUnit, itemId)
endfunction

//===========================================================================
function InitTrig_func takes nothing returns nothing

    set t=CreateTimer()
    set r=Rect(0.00, 0.00, 256.00, 256.00)
endfunction

edit
forgot: you may want to exclude dead and hiden items
 

Attachments

  • NightSkyAurora.w3x
    15.6 KB · Views: 49
When I create two items in a row using for example "CreateItemLoc()", CreateItem1() will only capture the second created item. I think that the timer waits for too long before displaying the last created item. Before the name of the first item is displayed, a second item can be created that overrides "bj_lastCreatedItem" from the first created item
 
Level 18
Joined
Nov 21, 2012
Messages
835
I have no problem to detect when (and what) items are created for a hero. The game provides events that can detect when a unit acquires an item.
what if inventory is full? you have to check on the groud every time to be sure you will not miss any item created

@AGD
JASS:
hook DoNothing Nothing

//becomes

function h__DoNothing takes nothing returns nothing
    //hook: Nothing
//so here we have access only to argument(s) passed to original native/BJs, right?
//and to get what original native returns - we have to use 0sec timer

    call sc___prototype1_evaluate(1)
call DoNothing()
endfunction
 
Last edited:
Status
Not open for further replies.
Top