Problem with function

Homer

New Member
Reaction score
2
Alright, I'm stumped to why one of the functions is not working. I have three functions dealing with items(part of an item stacking script) They all have the same three arguements and I know for a fact the third one(AddItem) works as it triggers as a result of the first function(checkexistingitems) always returns false even if there is the same item.

JASS:
function CheckExistingItems takes unit u, integer i, integer r returns boolean

if (r == 0) then
    if (GetInventoryIndexOfItemTypeBJ(u, udg_Human_item<i>) &gt; 0) then
            return true
    endif
endif
    if (r == 1) then
        if (GetInventoryIndexOfItemTypeBJ(u, udg_Orc_item<i>) &gt; 0) then
            return true
        endif
     endif
        if (r == 2) then
            if (GetInventoryIndexOfItemTypeBJ(u, udg_Nightelf_item<i>) &gt; 0) then
            return true
            endif
        endif
            if (r == 3) then
                if (GetInventoryIndexOfItemTypeBJ(u, udg_Human_item<i>) &gt; 0) then
                return true
                endif
            endif
return false
endfunction

function AddItemCharge takes unit u, integer i, integer r returns boolean
if (r == 0) then
call SetItemCharges( GetItemOfTypeFromUnitBJ(u, udg_Human_item<i>), GetItemCharges(GetItemOfTypeFromUnitBJ(u, udg_Human_item<i>)) + 1) 
else
    if (r == 1) then
        call SetItemCharges( GetItemOfTypeFromUnitBJ(u, udg_Orc_item<i>), GetItemCharges(GetItemOfTypeFromUnitBJ(u, udg_Orc_item<i>)) + 1)
    else
        if (r == 2) then
            call SetItemCharges( GetItemOfTypeFromUnitBJ(u, udg_Nightelf_item<i>), GetItemCharges(GetItemOfTypeFromUnitBJ(u, udg_Nightelf_item<i>)) + 1)
        else
            if (r == 3) then
                call SetItemCharges( GetItemOfTypeFromUnitBJ(u, udg_Undead_item<i>), GetItemCharges(GetItemOfTypeFromUnitBJ(u, udg_Undead_item<i>)) + 1)
            endif
        endif
    endif
endif
return false
endfunction

function AddItem takes unit u, integer i, integer r returns boolean
if (r == 0) then
call UnitAddItemByIdSwapped( udg_Human_item<i>, u )
else
    if (r == 1) then
        call UnitAddItemByIdSwapped( udg_Orc_item<i>, u )
    else
        if (r == 2) then
            call UnitAddItemByIdSwapped( udg_Nightelf_item<i>, u )
        else
            if (r == 3) then
                call UnitAddItemByIdSwapped( udg_Undead_item<i>, u )
            endif
        endif
    endif
endif
return false
endfunction</i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i>


I'm using the same three inputs for the arguments on all of them. So why is the first function failing?

Thanks
 

SerraAvenger

Cuz I can
Reaction score
234
Alright, I'm stumped to why one of the functions is not working. I have three functions dealing with items(part of an item stacking script) They all have the same three arguements and I know for a fact the third one(AddItem) works as it triggers as a result of the first function(checkexistingitems) always returns false even if there is the same item.

JASS:
function CheckExistingItems takes unit u, integer i, integer r returns boolean

if (r == 0) then
    if (GetInventoryIndexOfItemTypeBJ(u, udg_Human_item<i>) &gt; 0) then
            return true
    endif
endif
    if (r == 1) then
        if (GetInventoryIndexOfItemTypeBJ(u, udg_Orc_item<i>) &gt; 0) then
            return true
        endif
     endif
        if (r == 2) then
            if (GetInventoryIndexOfItemTypeBJ(u, udg_Nightelf_item<i>) &gt; 0) then
            return true
            endif
        endif
            if (r == 3) then
                if (GetInventoryIndexOfItemTypeBJ(u, udg_Human_item<i>) &gt; 0) then
                return true
                endif
            endif
return false
endfunction

function AddItemCharge takes unit u, integer i, integer r returns boolean
if (r == 0) then
call SetItemCharges( GetItemOfTypeFromUnitBJ(u, udg_Human_item<i>), GetItemCharges(GetItemOfTypeFromUnitBJ(u, udg_Human_item<i>)) + 1) 
else
    if (r == 1) then
        call SetItemCharges( GetItemOfTypeFromUnitBJ(u, udg_Orc_item<i>), GetItemCharges(GetItemOfTypeFromUnitBJ(u, udg_Orc_item<i>)) + 1)
    else
        if (r == 2) then
            call SetItemCharges( GetItemOfTypeFromUnitBJ(u, udg_Nightelf_item<i>), GetItemCharges(GetItemOfTypeFromUnitBJ(u, udg_Nightelf_item<i>)) + 1)
        else
            if (r == 3) then
                call SetItemCharges( GetItemOfTypeFromUnitBJ(u, udg_Undead_item<i>), GetItemCharges(GetItemOfTypeFromUnitBJ(u, udg_Undead_item<i>)) + 1)
            endif
        endif
    endif
endif
return false
endfunction

function AddItem takes unit u, integer i, integer r returns boolean
if (r == 0) then
call UnitAddItemByIdSwapped( udg_Human_item<i>, u )
else
    if (r == 1) then
        call UnitAddItemByIdSwapped( udg_Orc_item<i>, u )
    else
        if (r == 2) then
            call UnitAddItemByIdSwapped( udg_Nightelf_item<i>, u )
        else
            if (r == 3) then
                call UnitAddItemByIdSwapped( udg_Undead_item<i>, u )
            endif
        endif
    endif
endif
return false
endfunction</i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i>


I'm using the same three inputs for the arguments on all of them. So why is the first function failing?

Thanks

oh hell , please start using elseifs!


JASS:
function AddItem takes unit u, integer i, integer r returns boolean
if (r == 0) then
    call UnitAddItemByIdSwapped( udg_Human_item<i>, u )
elseif (r == 1) then
    call UnitAddItemByIdSwapped( udg_Orc_item<i>, u )
elseif (r == 2) then
    call UnitAddItemByIdSwapped( udg_Nightelf_item<i>, u )
elseif (r == 3) then
     call UnitAddItemByIdSwapped( udg_Undead_item<i>, u )
endif
return false
endfunction
</i></i></i></i>

Anyway, just programm yourself a 2 dimensional array.

JASS:
globals
    constant integer       NUMRACES = 4
             integer array udg_Items
endglobals

function SetRaceItem takes integer itemtypeid, integer itemid, integer raceid returns nothing
    if raceid &lt; NUMRACES then
        set udg_Items[ raceid + itemid * NUMRACES ] = itemtypeid 
    endif
endfunction

function UnitAddRaceItem takes unit whichunit, integer itemid, integer raceid returns boolean
    if raceid &lt; NUMRACES then 
        call UnitAddItemById( whichunit, udg_Items[ raceid + itemid * NUMRACES ] )
    else
        return false
    endif
    return true
endfunction

function UnitHasRaceItem takes unit whichunit, integer itemid, integer raceid returns boolean
    local integer slotId = 0
    loop
         exitwhen slotId &gt; 5
         if GetItemTypeId( UnitItemInSlot( whichunit, slotId ) ) == udg_Items[ raceid + itemid * NUMRACES ] then
            return true
        endif
        set slotId = slotId + 1
    endloop
    return false
endfunction

function AddRaceItemCharges takes unit whichunit, integer itemid, integer raceid, integer count returns boolean
    local integer slotId = 0
    local item    slotItem    loop
         exitwhen slotId &gt; 5
         set slotItem = UnitItemInSlot( whichunit, slotId ) 
         if GetItemTypeId( slotItem ) == udg_Items[ raceid + itemid * NUMRACES ] then
            call SetItemCharges( slotItem, GetItemCharges( slotItem ) + count )
            return true
        endif
        set slotId = slotId + 1
    endloop
    return false
endfunction


Apart from that, you could perhaps use my itemstacker.
http://www.thehelper.net/forums/showthread.php?t=82070

Greetings, Davey
 

Homer

New Member
Reaction score
2
god damn it 2d arrays exist in jass? god damn it haha wow that wasted so much time of mine.

ur example only shows a 1d array, is that a fake 2d? minioulating integer numbers and multiplying by the race? Unfortuneatly that wont work for me, i'd have to reprogram so much, thanks though. I'll try using elses there.
 

SerraAvenger

Cuz I can
Reaction score
234
god damn it 2d arrays exist in jass? god damn it haha wow that wasted so much time of mine.

ur example only shows a 1d array, is that a fake 2d? minioulating integer numbers and multiplying by the race? Unfortuneatly that wont work for me, i'd have to reprogram so much, thanks though. I'll try using elses there.
Aye.
Believe me, it is much easier and cleaner to use such a code, you won't almost have to reprogram anything. Just your "set udg_Human_item = 'xxxx'". The rest will stay absolutely the same.
Just do
call SetRaceItem( 'xxxx', i, 0 ) instead.
 

SerraAvenger

Cuz I can
Reaction score
234
No 2d arrays in jass, but gamecache can work as a 2d array.

First of all, there are 2d arrays in vJass, second I heard gamecache was buggy ( never used it though so I cannot prove this ) and third one can easily fake 2d arrays ; )
 

chobibo

Level 1 Crypt Lord
Reaction score
48
vJass is jass + textmacro, how would vJass have 2d arrays if jass didn't. and no gamecache isn't buggy, only slow, it only bugs out on you if you use any I2H return bugs on it.
Note: vJass emulates 2d arrays using textmacros.
 

Homer

New Member
Reaction score
2
i have over 100 vars split up into the four races(im making a heroes of might and magic clone) So it would be quite time consumming, after i re-declare those then i'd have to redo all the functions that includes them.

Anyways whats wrong with the way im checking the number of items. Cuz thats whats causing the problem. its pissing me off :)
 

SerraAvenger

Cuz I can
Reaction score
234
vJass is jass + textmacro, how would vJass have 2d arrays if jass didn't. and no gamecache isn't buggy, only slow, it only bugs out on you if you use any I2H return bugs on it.
Note: vJass emulates 2d arrays using textmacros.

Hm, perhaps because of this:
JASS:
            if (r == 3) then
                if (GetInventoryIndexOfItemTypeBJ(u, udg_Human_item<i>) &gt; 0) then
                return true
                endif
            endif</i>


needn't it be undead?^^

Source? I never heard anything about that. Gamecache access is slower than array access though. probably even after you do the calculations necessary for a 2d-array in jass.

Thanks for correcting me, then. I only know what I heard concerning gamecaches ; )
 

SerraAvenger

Cuz I can
Reaction score
234
How do you get the race of the unit? GetUnitRace?

with the parameter r :rolleyes:
JASS:
function CheckExistingItems takes unit u, integer i, integer r returns boolean

if (r == 0) then
    return GetInventoryIndexOfItemTypeBJ(u, udg_Human_item<i>) != 0
elseif (r == 1) then
    return GetInventoryIndexOfItemTypeBJ(u, udg_Orc_item<i>) != 0
elseif (r == 2) then
    return GetInventoryIndexOfItemTypeBJ(u, udg_Nightelf_item<i>) != 0
elseif (r == 3) then
    return GetInventoryIndexOfItemTypeBJ(u, udg_Undead_item<i>) != 0
endif
return false
endfunction</i></i></i></i>


this is your function, just a bit more readable. could you please try out this one? it should perfectly return true : /
 

chobibo

Level 1 Crypt Lord
Reaction score
48
How would you get r then? Do you write the data manually or you get the value through a function?
 

Homer

New Member
Reaction score
2
Thats the "code"(just gui) that runs it. I have afew display texts to make sure things are registering right. In my updated functions I put display text inside and outside the first function for the second if. ie. if (r==0) then
display text
second if(check items)
display text
etc.
and it seems to be the second if thats causing the problem
Code:
If (All Conditions are True) then do (Then Actions) else do (Else Actions)
    If - Conditions
        ((Sold unit) is A structure) Equal to False
    Then - Actions
        Custom script:   set udg_temp_int = identify_building(GetSellingUnit(),identify_race(GetAttachedUnit(GetSellingUnit(),"parent")))
        Custom script:   set udg_temp_crystal = GetAttachedInt(GetSellingUnit(),"crystal")
        Custom script:   set udg_temp_gold = GetAttachedInt(GetSellingUnit(),"gold")
        Custom script:   set udg_temp_lumber = GetAttachedInt(GetSellingUnit(),"lumber")
        Custom script:   set udg_temp_ore = GetAttachedInt(GetSellingUnit(),"ore")
        Custom script:   set udg_temp_int = GetAttachedInt(GetSellingUnit(),"stock")
        If (All Conditions are True) then do (Then Actions) else do (Else Actions)
            If - Conditions
                player_crystal[temp_player] Greater than or equal to temp_crystal
                player_gold[temp_player] Greater than or equal to temp_gold
                player_lumber[temp_player] Greater than or equal to temp_lumber
                player_ore[temp_player] Greater than or equal to temp_ore
            Then - Actions
                Custom script:   set udg_temp_unit = GetAttachedUnit(GetSellingUnit(),"parent")
                Custom script:   call DisplayTextToForce(GetPlayersAll(), I2S(identify_race(udg_temp_unit)))
                Set temp_int2 = (Number of items carried by temp_unit)
                Custom script:   set udg_temp_boolean = CheckExistingItems(udg_temp_unit,udg_temp_int,identify_race(udg_temp_unit))
                If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                    If - Conditions
                        Or - Any (Conditions) are true
                            Conditions
                                temp_int2 Less than 6
                    Then - Actions
                        If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                            If - Conditions
                                temp_boolean Equal to True
                            Then - Actions
                                Game - Display to (All players) the text: ffdsfffff
                                Custom script:   call AddItemCharge(udg_temp_unit,udg_temp_int,identify_race(udg_temp_unit))
                            Else - Actions
                                Custom script:   call DisplayTextToForce( GetPlayersAll(), GetUnitName(GetAttachedUnit(GetSellingUnit(),"parent")) )
                                Game - Display to (All players) the text: dfdsafs
                                Custom script:   call AddItem(GetAttachedUnit(GetSellingUnit(),"parent"),0,identify_race(GetAttachedUnit(GetSellingUnit(),"parent")))
                        Set player_crystal[temp_player] = (player_crystal[temp_player] - temp_crystal)
                        Set player_gold[temp_player] = (player_gold[temp_player] - temp_gold)
                        Set player_lumber[temp_player] = (player_lumber[temp_player] - temp_lumber)
                        Set player_ore[temp_player] = (player_ore[temp_player] - temp_ore)
                        Custom script:   call AttachInt(GetSellingUnit(),"stock",udg_temp_int-1)
                    Else - Actions
                Unit - Remove (Sold unit) from the game
            Else - Actions
                Neutral Building - Add (Unit-type of (Sold unit)) to (Selling unit) with temp_int in stock and a max stock of temp_int
                Unit - Remove (Sold unit) from the game
                Game - Display to (All players) the text: You do not have the...
    Else - Actions
 

Homer

New Member
Reaction score
2
Wow I just figured it out and im retarded. Sorry for wasting ur time guys. In my trigger that initiated it, it was first defining temp_int as the integer to find which item i needed. But it then over ride it with the amount of stock left. Thanks so much.

yeah, but it outputs correctly. Btw the code is alot cleaner serra but its a no go. Still same problems.

First let me explain what is happening. There is a main castle, this castle builds different structures. These structures build units. I store the parent unit in each building which is the castle that built it.

JASS:
function identify_building takes unit u, integer i returns integer
local integer z
    set z=0
    if (i==0) then
    loop
    exitwhen (z&gt;7)
    if (GetUnitTypeId(u)==udg_Human_building[z]) then
        set udg_temp_crystal = udg_Human_building_crystal[z]
        set udg_temp_gold = udg_Human_building_gold[z]
        set udg_temp_lumber = udg_Human_building_lumber[z]
        set udg_temp_ore = udg_Human_building_ore[z]
        return z
    endif  
    set z=z+1
    endloop
    else
        if (i==1) then
        loop
        exitwhen (z&gt;7)
        if (GetUnitTypeId(u)==udg_Orc_building[z]) then
            set udg_temp_crystal = udg_Orc_building_crystal[z]
            set udg_temp_gold = udg_Orc_building_gold[z]
            set udg_temp_lumber = udg_Orc_building_lumber[z]
            set udg_temp_ore = udg_Orc_building_ore[z]
            return z
        endif
        set z=z+1
        endloop
        else
            if (i==2) then
            loop
            exitwhen (z&gt;7)
            if (GetUnitTypeId(u)==udg_Nightelf_building[z]) then
                set udg_temp_crystal = udg_Nightelf_building_crystal[z]
                set udg_temp_gold = udg_Nightelf_building_gold[z]
                set udg_temp_lumber = udg_Nightelf_building_lumber[z]
                set udg_temp_ore = udg_Nightelf_building_ore[z]
                return z
            endif
            set z=z+1
            endloop
            else 
                if (i==3) then
                loop
                exitwhen (z&gt;7)
                if (GetUnitTypeId(u)==udg_Undead_building[z]) then
                    set udg_temp_crystal = udg_Undead_building_crystal[z]
                    set udg_temp_gold = udg_Undead_building_gold[z]
                    set udg_temp_lumber = udg_Undead_building_lumber[z]
                    set udg_temp_ore = udg_Undead_building_ore[z]
                    return z
                endif
                set z=z+1
                endloop
                else
                endif
            endif    
        endif
    endif
    
set u = null
return -1
    
endfunction

I've tested it multiple times and it is outputing the correct race as the last function would not work then.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top