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

[Snippet] Item Position

JASS:
local unit u
            if (IsItemOwned(i)) then
                set u = GetItemOwner(i)
                call SetUnitX(u, x)
                call SetUnitY(u, y)
                set u = null
            else
                call SetItemPosition(i, x, y)
            endif
Dude, what are you doing? Most will definitely not want to have this. Just ignore the case that the item is owned.

Nobody would expect the owner to be relocated. This is really important.
 
Level 31
Joined
Jul 10, 2007
Messages
6,306
Er... if you are setting an item's x, you are setting the position of the unit that owns it if it is picked up ;o... otherwise it wouldn't do anything, similarly to how retrieving an item's x doesn't do anything when the item is picked up.

The widget that represents an item when it is picked up is the owning unit, so it only makes sense that setting an item's positing would set the unit's position instead if it is owned.
 
Level 31
Joined
Jul 10, 2007
Messages
6,306
Again, the item owner's position is the item's position, so it would only make sense that changing or reading the item's position would change/change the owner's position >.>.

Now what I could do is make the unit drop the item and then set the item's position... actually, that would be a pretty useful function.


If an item performs a teleport, it carries the holder
If an item is summoned, it vanishes from holder

;o
 
Again, the item owner's position is the item's position, so it would only make sense that changing or reading the item's position would change/change the owner's position >.>.
Read = Yes
Change = No.

Nobody wants to move the unit. The function is called Itempos.setpos() and nobody would expect the unit to be moved. Just could cause bugs.

Just imagine you use the function to move keys in your map. If an unit picks it up you will move the unit to the key position instead of the item. This would be really stupid in an escape map.
 
Level 31
Joined
Jul 10, 2007
Messages
6,306
Hmm.. need to test something with SetItemX, but you make a valid point.

Moving an item while it's carried by a unit will move the item only by forcing the unit to drop the item.

I'll implement it when I can : )

This week is last week of semester, so I'm going to be a busy little bee for a bit : P.

At the moment, I still gotta do 2.5 finals, 6 quizzes, and 1 essay within the next 3 days = ). I'm hoping to get .5 finals and the essay done tonight, 1 final + 6 quizzes done tomorrow, and the last final done on Thursday ^_^. After Thursday's last final, I will be free >: O, but I will probably be so exhausted that I won't be coding until Monday, haha : D.

If you don't know, each final has taken about 10-12 hours. Each quiz takes about 1 hour. The essay will take about 1.5 hours.

/cry
 
Level 31
Joined
Jul 10, 2007
Messages
6,306
Updated to v2.0.0.0

Changed everything into plain old function calls
Added modOwner argument to most of the sets (check API for details)



Now, I included modOwner because the argument on whether setting an item's position should set the carrying unit's position doesn't seem to have a right answer : |. I thought about it and it could go either way. In the end, it's all based on preference/situation.
 
Level 31
Joined
Jul 10, 2007
Messages
6,306
->Bribe

Yea, I coded that fast if you couldn't tell, lol ;P

->Anachron

edit
Hmm, SetItemPosition will drop the item from unit and set it's coordinates... interesting ;o. Guess if modOwner is false, you want it to do nothing ;).

will perform an update when I can :)

updated
if mod owner is false and the item is being carried, nothing will happen.
 
Last edited:
You should change this back; if you use ItemSetX or ItemSetY and DON'T want to set the owner's position, you might as well use SetItemPosition and forget the function wrappers.

JASS:
library ItemPosition uses GetItemOwner // hiveworkshop.com/forums/showthread.php?t=184563
    
    //Version 3.0.0.0
    
    /*
        function ItemGetX takes item i returns real
        function ItemGetY takes item i returns real
        function ItemGetZ takes item i returns real
        //! runtextmacro ITEM_POSITION("whichItem", "xVarName", "yVarName")
            - A textmacro that automatically sets variables "x" and "y".
        
        function ItemSetX takes item i, real x returns nothing
        function ItemSetY takes item i, real y returns nothing
        function ItemSetZ takes item i, real z, real rate returns nothing
        function ItemSetPosition takes item i, real x, real y returns nothing
        
        Setting the item's position using these controls will set the item
        owner's position as well. If you don't want this feature, don't use
        these functions.
    */
    
    globals
        private location l = Location(0., 0.)
        public unit tU
    endglobals
    
    function ItemGetX takes item i returns real
        if (IsItemOwned(i)) then
            return GetUnitX(GetItemOwner(i))
        endif
        return GetItemX(i)
    endfunction
    
    function ItemGetY takes item i returns real
        if (IsItemOwned(i)) then
            return GetUnitY(GetItemOwner(i))
        endif
        return GetItemY(i)
    endfunction
    
    function ItemGetZ takes item i returns real
        if (IsItemOwned(i)) then
            set tU = GetItemOwner(i)
            call MoveLocation(l, GetUnitX(tU), GetUnitY(tU))
            return GetLocationZ(tU) + GetUnitFlyHeight(u)
        endif
        call MoveLocation(l, GetItemX(i), GetItemY(i))
        return GetLocationZ(l)
    endfunction
    
    //! textmacro ITEM_POSITION takes ITEM, X, Y
            if (IsItemOwned($ITEM$)) then
                set ItemPosition_tU = GetItemOwner($ITEM$)
                set $X$ = GetUnitX(ItemPosition_tU)
                set $Y$ = GetUnitY(ItemPosition_tU)
            endif
            set $X$ = GetItemX($ITEM$)
            set $Y$ = GetItemY($ITEM$)
    //! endtextmacro
    
    function ItemSetPosition takes item i, real x, real y returns nothing
        if (IsItemOwned(i)) then
            set tU = GetItemOwner(i)
            call SetUnitX(tU, x)
            call SetUnitY(tU, y)
        else
            call SetItemPosition(i, x, y)
        endif
    endfunction
    
    function ItemSetX takes item i, real x returns nothing
        if (IsItemOwned(i)) then
            call SetUnitX(GetItemOwner(i), x)
        else
            call SetItemPosition(i, x, GetItemY(i))
        endif
    endfunction
    
    function ItemSetY takes item i, real y returns nothing
        if (IsItemOwned(i)) then
            call SetUnitY(GetItemOwner(i), y)
        else
            call SetItemPosition(i, GetItemX(i), y)
        endif
    endfunction
    
    function ItemSetZ takes item i, real z, real rate returns nothing
        if (IsItemOwned(i)) then
            set tU = GetItemOwner(i)
            call MoveLocation(l, GetUnitX(tU), GetUnitY(tU))
            call SetUnitFlyHeight(tU, z + GetLocationZ(l), rate)
        endif
    endfunction
    
endlibrary
 
GetUnitX is faster than GetWidgetX, GetWidgetX is faster than GetItemX, and you have implemented the exact opposite :x

The ItemGetZ function could be broken down to be slightly more optimal:

JASS:
    private function GetUnitZ takes unit u returns real
        call MoveLocation(zl, GetUnitX(u), GetUnitY(u))
        return GetUnitFlyHeight(u) + GetLocationZ(zl)
    endfunction
    function ItemGetZ takes item i returns real
        if (IsItemOwned(i)) then
            return GetUnitZ(GetItemOwner(i))
        endif
        call MoveLocation(zl, GetWidgetX(i), GetWidgetY(i))
        return GetLocationZ(zl)
    endfunction

I still don't get the point of ModOwner. Is it just for the "Position" library?
 
Top