• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

[JASS] Function to check the inventory and count 2 item of Type X

Status
Not open for further replies.
Level 14
Joined
Jul 1, 2008
Messages
1,314
Hey guys,
Here is an example to explain, whats the problem:
Inventory Slots // Item Type
0 // Car
1 // Light
2 // Apple
3 // Light
4 // Car
5 // Apple

0 // Car
1 // Apple
2 // null
3 // Apple
4 // Apple
5 // Car

Function Should return
1. example : 0 = Car, 1 =Light, 2 = Apple
2. example : 0 = Car, 2 = Apple


THE FUNCTIONS
1. Finds out, if there are >=2 items of type x in inventory
JASS:
function CountItemsOfTypeFromUnit takes unit whichUnit, integer whatItemtype returns integer
    local integer index = 0
    local integer count = 0
    loop
        exitwhen index >= bj_MAX_INVENTORY
        if ( GetItemTypeId ( UnitItemInSlot ( whichUnit, index ) ) == whatItemtype ) then
            set count = count + 1
        endif
        set index = index + 1
    endloop
    return count
endfunction

2. Checks the inventory, and should return things as in the examples
JASS:
function CountEveryItemInSlot takes nothing returns nothing
      local unit u = udg_X
      local integer i = 0
      local integer array amount
      local integer Type
      set amount[0] = 0
      set amount[1] = 0
      set amount[2] = 0
      set amount[3] = 0
      set amount[4] = 0
      set amount[5] = 0
      set udg_TempIntegerCount[0] = 0
      set udg_TempIntegerCount[1] = 0
      set udg_TempIntegerCount[2] = 0
    loop
        exitwhen i > 5
        set Type = GetItemTypeId(UnitItemInSlot(u,i))
        if  Type != 0 then
        set amount[i] = CountItemsOfTypeFromUnit(u,Type)
          if amount[i] >= 2 then

              if Type != udg_TempIntegerCount[0] and amount[0] < 2 then
                 set udg_TempIntegerCount[0] = Type
                 set amount[0] = amount[i]

              elseif Type != udg_TempIntegerCount[1] and amount[1] < 2 then
                 set udg_TempIntegerCount[1] = Type
                 set amount[1] = amount[i]

              elseif Type != udg_TempIntegerCount[2] and amount[2] < 2 then
                 set udg_TempIntegerCount[2] = Type
                 set amount[2] = amount[i]

              endif
          endif


        endif
        set i = i+1    
    endloop
endfunction

It returns in example 1 : 0 = Car, 1 = Light
but why doesnt it return the Apple?

Would be very great if you could help me out with some logic ;)
Greets
 
Level 22
Joined
Dec 31, 2006
Messages
2,216
Can't you just do this?

JASS:
function CountEveryItemInSlot takes nothing returns nothing
      local unit u = udg_X
      local integer i = 0
      loop
          exitwhen i > 5
          set udg_TempIntegerCount[i] = GetItemTypeId(UnitItemInSlot(u,i))
          set i = i + 1
     endloop
     set u = null
endfunction
 
Level 14
Joined
Jul 1, 2008
Messages
1,314
hey thanks for the answer,

but then it will return in
0 = null
1 = null
2 = Car
3 = Apple
4 = Apple
5 = Light

0 = , 1 = , 2= Car, 3 = apple, 4 = Apple, 5 = Light

and i want it to return in this case 0 = apple.

But i have an idea now. I could compare them, after using ur idea, and get them to the right order like this...

Any other ideas?

Greets
 
Level 14
Joined
Jul 1, 2008
Messages
1,314
then it would NOT register this example:

0 = null
1 = null
2 = null
3 = null
4 = Apple
5 = Apple

But it should actually return 0 = Apple in this case.
So i have to check every slot in the inventory of the player.

Short explanation again:

I want to check the whole inventory of UNit x. If unit x has >= 2 items of the same kind, then i want to set them into the array udg_TempIntegerCount from 0 to 3.

(because max. 2 items of one kind 3 times)

Thank you, the rebronDevil, for your answers, but the problem is still not solved.

Greets
 
Level 22
Joined
Dec 31, 2006
Messages
2,216
Ok, now I understand (I hope ^^)

This new code will store all items which the unit has >= 2 of and that isn't already stored in a variable.

JASS:
function CountItemsOfTypeFromUnit takes unit whichUnit, integer whatItemtype returns integer
    local integer index = 0
    local integer count = 0
    loop
        exitwhen index >= bj_MAX_INVENTORY
        if ( GetItemTypeId ( UnitItemInSlot ( whichUnit, index ) ) == whatItemtype ) then
            set count = count + 1
        endif
        set index = index + 1
    endloop
    return count
endfunction

function CountEveryItemInSlot takes nothing returns nothing
      local unit u = udg_X
      local integer i = 0
      local integer i2 = 0
      local integer n = 0
      local integer temp
      local integer b = 0
      loop
          exitwhen i > 5
          set temp = GetItemTypeId(UnitItemInSlot(u,i))
          set n = CountItemsOfTypeFromUnit(u, temp)
          if n >= 2 then
              loop
                  exitwhen i2 == i
                  if udg_TempIntegerCount[i2] == temp then
                      set b = 1
                  endif
                  set i2 = i2 + 1
              endloop
              if b != 1 then
                  set udg_TempIntegerCount[i] = temp
              endif
              set b = 0
          endif
          set i = i + 1
     endloop
     set u = null
endfunction
 
Level 14
Joined
Jul 1, 2008
Messages
1,314
hey thanks :D

Thats really nice,

But there are still some problems in it. I am not sure, i tried a few things, but they are either kind of giant scripts or they dont work.

Well maybe you could test your function in a little map?
I made this one, just pick up the items, you can type anything you want to test it. (Player Chat Event will take any string to run the trigger)

UPLOAD

Would be really great, if you, or someone other could help..

Greets

Edit:

Goal is, whatever you pick up, it should never return more than 3 places, and it should count them from 0 to 3.
 
Level 22
Joined
Dec 31, 2006
Messages
2,216
It picked only 1 of each kind, didn't you want that?
If you want it to pick all the items then use this code instead:
JASS:
function CountEveryItemInSlot takes nothing returns nothing
      local unit u = udg_X
      local integer i = 0
      loop
          exitwhen i > 5
          set udg_TempIntegerCount[i] = GetItemTypeId(UnitItemInSlot(u,i))
          set i = i + 1
     endloop
     set u = null
     call ClearTextMessages()
     loop
           exitwhen i > 5
           call DisplayTextToForce( GetPlayersAll(),"item " + I2S(udg_TempIntegerCount[i]) + " place: |c00ff0000" + I2S(i) + "|r")
           set udg_TempIntegerCount[i] = 0
           set i = i+1
     endloop
endfunction

//===========================================================================
function InitTrig_Count_Test takes nothing returns nothing
    set gg_trg_Count_Test = CreateTrigger(  )
    call TriggerRegisterPlayerChatEvent( gg_trg_Count_Test, Player(0), "", true )
    call TriggerAddAction( gg_trg_Count_Test, function CountEveryItemInSlot )
endfunction
I'm not exactly sure what you want now (I thought I did when I posted my last post). What do you want to do with this code? The way I understood it was that you wanted to store all items that you have >= 2 of in a variable. That was what the other code did, but it didn't store items that already was stored. Which part of the code do you want changed?
 
Level 14
Joined
Jul 1, 2008
Messages
1,314
Sorry for the confusion, and thanks for your patience :)

I want exactly, what you understood, that i would like too. But the code does not work correctly, but somehow, the return test, to see, what it stored, does not return the right things.

I inserted this to just check, if the function works, so either there are some logic mistakes, or im completely wrong with this test down there
JASS:
set i = 0
 call ClearTextMessages()
     loop
           exitwhen i > 5
           call DisplayTextToForce( GetPlayersAll(),"item " + I2S(udg_TempIntegerCount[i]) + " place: |c00ff0000" + I2S(i) + "|r")
           set i = i+1
     endloop

Thats why i uploaded the map for you, hoping that you could test it a bit, pick up some items, and type something to start the function and see whats wrong ^^.

It somehow doesnt work sometimes, and i really have no idea, why...because the code seems very logic to me.

I hope, you understand, where the problem is?

Greetings
 
Level 22
Joined
Dec 31, 2006
Messages
2,216
I fixed two small things and I hope it works the way you want :)
It does exactly what I think you want, which is that it stores all items that you have >= 2 of, but it doesn't store more than 1 of each kind.
JASS:
function CountItemsOfTypeFromUnit takes unit whichUnit, integer whatItemtype returns integer
    local integer index = 0
    local integer count = 0
    loop
        exitwhen index >= bj_MAX_INVENTORY
        if ( GetItemTypeId ( UnitItemInSlot ( whichUnit, index ) ) == whatItemtype ) then
            set count = count + 1
        endif
        set index = index + 1
    endloop
    return count
endfunction

function CountEveryItemInSlot takes nothing returns nothing
      local unit u = udg_X
      local integer i = 0
      local integer i2 = 0
      local integer n = 0
      local integer temp
      local integer b = 0
      loop
          exitwhen i > 5
          set udg_TempIntegerCount[i] = 0
          set temp = GetItemTypeId(UnitItemInSlot(u,i))
          set n = CountItemsOfTypeFromUnit(u, temp)
          if n >= 2 then
              loop
                  exitwhen i2 == i
                  if udg_TempIntegerCount[i2] == temp then
                      set b = 1
                  endif
                  set i2 = i2 + 1
              endloop
              if b != 1 then
                  set udg_TempIntegerCount[i] = temp
              endif
              set b = 0
              set i2 = 0
          endif
          set i = i + 1
     endloop
     set u = null
     set i = 0
     call ClearTextMessages()
     loop
           exitwhen i > 5
           if udg_TempIntegerCount[i] != 0 then
               call DisplayTextToForce( GetPlayersAll(), GetObjectName(udg_TempIntegerCount[i]) + " place: |c00ff0000" + I2S(i) + "|r" )
           else
               call DisplayTextToForce( GetPlayersAll(), "------" + " place: |c00ff0000" + I2S(i) + "|r" )
           endif
           set i = i+1
     endloop
endfunction

//===========================================================================
function InitTrig_Count_Test takes nothing returns nothing
    set gg_trg_Count_Test = CreateTrigger(  )
    call TriggerRegisterPlayerChatEvent( gg_trg_Count_Test, Player(0), "", true )
    call TriggerAddAction( gg_trg_Count_Test, function CountEveryItemInSlot )
endfunction
If you want it to store all items that you have >= 2 of and store all of them and not only 1 of each kind then replace this part:
JASS:
              loop
                  exitwhen i2 == i
                  if udg_TempIntegerCount[i2] == temp then
                      set b = 1
                  endif
                  set i2 = i2 + 1
              endloop
              if b != 1 then
                  set udg_TempIntegerCount[i] = temp
              endif
with this:
JASS:
              set udg_TempIntegerCount[i] = temp
You can also remove the local variable "b" and "i2".
 
Level 14
Joined
Jul 1, 2008
Messages
1,314
DUDE; YOU REALLY ARE MY HERO

I will add you to the credits in m map.
I lilttle modified your code, and now it really works how i want it.

JASS:
function CountItemsOfTypeFromUnit takes unit whichUnit, integer whatItemtype returns integer
local integer index = 0
    local integer count = 0
    loop
        exitwhen index >= bj_MAX_INVENTORY
        if ( GetItemTypeId ( UnitItemInSlot ( whichUnit, index ) ) == whatItemtype ) then
            set count = count + 1
        endif
        set index = index + 1
    endloop
    return count
endfunction

function CountEveryItemInSlot takes nothing returns nothing
      local unit u = udg_X
      local integer i = 0
      local integer i2 = 0
      local integer n = 0
      local integer temp
      local integer b = 0
      loop
          exitwhen i > 5
          set udg_TempIntegerCount[i] = 0
          set temp = GetItemTypeId(UnitItemInSlot(u,i))
          set n = CountItemsOfTypeFromUnit(u, temp)
          if n >= 2 then
              loop
                  exitwhen i2 == i
                  if udg_TempIntegerCount[i2] == temp then
                      set b = 1
                  endif
                  set i2 = i2 + 1
              endloop
              if b != 1 then
                if udg_TempIntegerCount[0] == 0 then
                  set udg_TempIntegerCount[0] = temp
                endif
                if udg_TempIntegerCount[1] == 0 then
                  set udg_TempIntegerCount[1] = temp
                endif
               if udg_TempIntegerCount[2] == 0 then
                  set udg_TempIntegerCount[2] = temp
               endif
              endif
              set b = 0
              set i2 = 0
          endif
          set i = i + 1
     endloop
     set u = null
set i = 0
     call ClearTextMessages()
loop
           exitwhen i > 5
           if udg_TempIntegerCount[i] != 0 then
               call DisplayTextToForce( GetPlayersAll(), GetObjectName(udg_TempIntegerCount[i]) + " place: |c00ff0000" + I2S(i) + "|r" )
           else
               call DisplayTextToForce( GetPlayersAll(), "------" + " place: |c00ff0000" + I2S(i) + "|r" )
           endif
           set i = i+1
     endloop
endfunction

//===========================================================================
function InitTrig_Count_Test takes nothing returns nothing
    set gg_trg_Count_Test = CreateTrigger( )
    call TriggerRegisterPlayerChatEvent( gg_trg_Count_Test, Player(0), "", true )
    call TriggerAddAction( gg_trg_Count_Test, function CountEveryItemInSlot )
endfunction

You are really really great, and i have to thank you that much. :D

Edit: Well i recognized a little bug thoug, when you only have one kind in inventar it fills all the 3 slots, but thats a problem, only because i edited your code. But this doesnt matter, as you can only press one dialog button at once. But its important, that i sets the items from 0 to 2
Greets
 
Status
Not open for further replies.
Top