Explanation on this issue?

Reaction score
91
I have a script that is supposed to issue usage of a specific item at some point of the game. Because there are a lot of items that I use based on instant, unit or point cast, I used a simplified method and just call the 3 natives instead of checking whether the item should be casted on units/points/instantaneously or not.

JASS:

// This code is executed in a loop so I use an O(n) search in an array for item types. "crap" is an item variable, don't get confused... ^^
// ITEM[a] is my integer array containing all item types.
                        set crap = GetItemOfTypeFromUnitBJ(this.cast, ITEM[a])
                        if not UnitUseItemPoint(this.cast, crap, this.tx, this.ty) then
                            if not UnitUseItem(this.cast, crap) then
                            // should work?
                                if not UnitUseItemTarget(this.cast, crap, this.targ) then
                                    call BJDebugMsg("FAIL ?!?!?!?")
                                endif
                            endif
                        endif

Say this is a part of my script that issues that item casting. Everything works perfectly except for unit targeting... This event is supposed to trigger when a spell is cast and if the specific item targets units, it should target a unit. The problem is that the item was never used on the target even though I had no other mistakes in my code. After some toying around I rearranged the order of casting and found out that this time it works flawlessly.

JASS:

// This code is executed in a loop so I use an O(n) search in an array for item types. "crap" is an item variable, don't get confused... ^^
                        set crap = GetItemOfTypeFromUnitBJ(this.cast, ITEM[a])
                        if not UnitUseItemPoint(this.cast, crap, this.tx, this.ty) then
                            if not UnitUseItemTarget(this.cast, crap, this.targ) then
                                if not UnitUseItem(this.cast, crap) then
                                    call BJDebugMsg("FAIL ?!?!?!?")
                                endif
                            endif
                        endif


So, can anybody explain to me why the first method does not work for unit targeting while the second works for everything?
Even though in the first method FAIL is never displayed, it does not cast the item!
 

Tom Jones

N/A
Reaction score
437
The obvious question is then how far the execution goes in the first code? Does UnitUseItem(...) return a strange boolean, just as IsUnitType(...)? Try comparing to false instead of using not, and try adding a debug message inside each if statement to see how far the code goes.
 
Reaction score
91
In both situations FAIL never gets displayed (when it's a unit-target item) so it is returning TRUE... but there is no issued item casting.

JASS:
//
                        set crap = GetItemOfTypeFromUnitBJ(this.cast, ITEM[a])
                        //if not UnitUseItemPoint(this.cast, crap, this.tx, this.ty) then
                            //if not UnitUseItemTarget(this.cast, crap, this.targ) then
                                //if not UnitUseItem(this.cast, crap) then
                        if UnitUseItemPoint(this.cast, crap, this.tx, this.ty) == false then
                            if UnitUseItem(this.cast, crap) == false then
                                if UnitUseItemTarget(this.cast, crap, this.targ) == false then
                                    call BJDebugMsg("FAIL ?!?!?!?")
                                endif
                            endif
                        endif


Totally same situation as with the not example... :nuts: Swapping UnitUseItem and UnitUseItemTarget works just fine.
 

Rainther

I guess I should write something of value here...
Reaction score
61
Confirm with debug messages which of the "if"s that makes it not reach "FAIL" and mentioned above. If you already have done it, you at least haven't said it.
 
Reaction score
91
JASS:

if UnitUseItem(this.cast, crap) == false then


After this part, UnitUseItemTarget does not get executed since UnitUseItem returns true and stops further actions... This is quite odd since UnitUseItem does not require any target and should return false, however even though it gives the unexpected result, it does not cast the item.
I also noticed something odd with those natives for point targeting items (only point since I use Channel with that option):

JASS:

                        set crap = GetItemOfTypeFromUnitBJ(this.cast, ITEM[a])
                        if not UnitUseItemPoint(this.cast, crap, this.tx, this.ty) then
                            if not UnitUseItem(this.cast, crap) then
                            // should work?
                                if not UnitUseItemTarget(this.cast, crap, this.targ) then
                                    call BJDebugMsg("FAIL ?!?!?!?")
                                endif
                            endif
                        endif

FAIL gets displayed, although it shouldn't since UnitUseItemPoint is supposed to return true, right?
 

Tom Jones

N/A
Reaction score
437
I think it has something to do with range. If the target is out of range of the unit with the item it'll return false. This would make perfect sense because:

UnitUseItemPoint(...) - Has range.
UnitUseItemTarget(...) - Has range.
UnitUseItem(...) - Target is the unit itself, so no range.

I have an idea that UniUseItem(...) always will return true. I'll do some testing to see whether or not this is true.

*Edit*

Here's how I tested:
JASS:
scope ItemTest initializer Init

globals
    private constant integer WHICH_ID   = 1     //Range 0-3.
    private constant real ITEM_RANGE    = 500
    private constant integer TARGET_ID  = 'hfoo'
    
    private unit U   
    private string array S
    private integer array ITEM_IDS  
    private unit array TARGET_UNITS
    private real array TARGET_POINTS
endglobals

private function onTest takes nothing returns nothing
    local item theitem = UnitAddItemById(U,ITEM_IDS[WHICH_ID])
    local integer i = 0
    local string s
    local boolean array b 
        
    loop
        exitwhen i == 2
        set b[i*3] = UnitUseItemPoint(U,theitem,TARGET_POINTS[i*2],TARGET_POINTS[i*2+1])
        set b[i*3+1] = UnitUseItemTarget(U,theitem,TARGET_UNITS<i>)
        set b[i*3+2] = UnitUseItem(U,theitem)
        set i = i+1
    endloop
    set i = 0
    loop
        exitwhen i == 6
        if i &lt; 3 then
            set s = &quot; within range = &quot;
        else
            set s = &quot; outside range = &quot;
        endif
        if b<i> then
            call BJDebugMsg(&quot;|cff00ff00&quot;+S[i-(i/3)*3]+s+&quot;true&quot;)
        else
            call BJDebugMsg(&quot;|cffff0000&quot;+S[i-(i/3)*3]+s+&quot;false&quot;)
        endif
        set i = i+1
    endloop
endfunction

private function Init takes nothing returns nothing
    local trigger trig = CreateTrigger()
    local integer i = 0
    local real r = ITEM_RANGE
    
    call TriggerRegisterTimerEvent(trig,1,false)
    call TriggerAddAction(trig,function onTest)
    
    set U = CreateUnit(Player(0),&#039;Hamg&#039;,0,0,0)
    call SetUnitInvulnerable(U,true)
    call UnitRemoveAbility(U,&#039;Aatk&#039;)
    
    loop
        exitwhen r &gt; 2*ITEM_RANGE
        set TARGET_UNITS<i> = CreateUnit(Player(1),TARGET_ID,r,0,0)
        set TARGET_POINTS[i*2] = (r/2)*(i+1)
        set TARGET_POINTS[i*2+1] = 0
        set r = r+ITEM_RANGE
        set i = i+1
    endloop
    
    set ITEM_IDS[0] = &#039;ssil&#039; //Point target item with cooldown.
    set ITEM_IDS[1] = &#039;wswd&#039; //Point target item without cooldown.
    set ITEM_IDS[2] = &#039;wlsd&#039; //Unit target item with cooldown.
    set ITEM_IDS[3] = &#039;wshs&#039; //Unit target item without cooldown.
    
    set S[0] = &quot;Point target&quot;
    set S[1] = &quot;Unit target&quot;
    set S[2] = &quot;No targer&quot;
    call FogModifierStart(CreateFogModifierRect(Player(0),FOG_OF_WAR_VISIBLE,GetWorldBounds(),true,false))
endfunction
endscope</i></i></i>


As I expected, UnitUseItem(...) will always return true, unless ofcourse the item is on cooldown.
UnitUseItemPoint(...) always returned false, even when using point targeted base items. I expect that this has something to do with a flaw in my test.
UnitUseItemTarget(...) works fine.
Range has to impact on the functions.
 
Reaction score
91
> I expect that this has something to do with a flaw in my test.
In both my cases (the non-working and the working one) it returns false... Maybe a flaw in the native? o_O
Good job for the tests, I will try to remember these things.
 
General chit-chat
Help Users
  • No one is chatting at the moment.
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
    +1
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though
  • Ghan Ghan:
    Heard Houston got hit pretty bad by storms last night. Hope all is well with TH.
  • The Helper The Helper:
    Power back on finally - all is good here no damage
    +2
  • V-SNES V-SNES:
    Happy Friday!
    +1
  • The Helper The Helper:
    New recipe is another summer dessert Berry and Peach Cheesecake - https://www.thehelper.net/threads/recipe-berry-and-peach-cheesecake.194169/

      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