How do I make items(with recipe) that has 2 same item

Status
Not open for further replies.
Level 24
Joined
Aug 1, 2013
Messages
4,658
I do have a function that can check items of a unit:

JASS:
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //
    //  Item functions:
    //
    library ItemFunctions
    
    //Checks if the given unit has items of the given types.
    //Fill the parameters with 0 to have fewer required item types.
    //Works with multiple items of the same type.
    function UnitCheckItems takes unit whichUnit, integer i1, integer i2, integer i3, integer i4, integer i5, integer i6 returns boolean
        local integer array heldItems
        local integer array requestedItems
        local integer index1
        local integer index2
        local integer requestedItemsLength = 6
        
        set requestedItems[0] = i1
        set requestedItems[1] = i2
        set requestedItems[2] = i3
        set requestedItems[3] = i4
        set requestedItems[4] = i5
        set requestedItems[5] = i6
        set heldItems[0] = GetItemTypeId(UnitItemInSlot(whichUnit, 0))
        set heldItems[1] = GetItemTypeId(UnitItemInSlot(whichUnit, 1))
        set heldItems[2] = GetItemTypeId(UnitItemInSlot(whichUnit, 2))
        set heldItems[3] = GetItemTypeId(UnitItemInSlot(whichUnit, 3))
        set heldItems[4] = GetItemTypeId(UnitItemInSlot(whichUnit, 4))
        set heldItems[5] = GetItemTypeId(UnitItemInSlot(whichUnit, 5))
        
        set index1 = 0
        loop
            if requestedItems[index1] == 0 then
                set requestedItemsLength = requestedItemsLength-1
                set requestedItems[index1] = requestedItems[requestedItemsLength]
                if requestedItemsLength == 0 then
                    return true
                endif
            endif
            exitwhen index1 == 5
            set index1 = index1 +1
        endloop
        
        set index1 = 0
        loop
            set index2 = 0
            loop
                exitwhen index2 >= requestedItemsLength
                
                if heldItems[index1] == requestedItems[index2] then
                    set requestedItemsLength = requestedItemsLength-1
                    set requestedItems[index2] = requestedItems[requestedItemsLength]
                    if requestedItemsLength == 0 then
                        return true
                    endif
                    exitwhen true
                endif
                
                set index2 = index2 +1
            endloop
            
            exitwhen index1 == 5
            set index1 = index1 +1
        endloop
        
        return false
    endfunction
    
    function UnitCountItemsOfType takes unit whichUnit, integer itemType returns integer
        local integer result = 0
        local integer i = 0
        loop
            exitwhen i >= 6
            
            if GetItemTypeId(UnitItemInSlot(whichUnit, i)) == itemType then
                set result = result +1
            endif
            
            set i = i +1
        endloop
        
        return result
    endfunction
    
    endlibrary
    //
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Removing the items is ussually no problem.
But searching for a system that does it might be a better idea in the first place.
 
Level 24
Joined
Aug 1, 2013
Messages
4,658
Then it is still simple:
JASS:
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //
    //  Item functions:
    //
    library ItemFunctions initializer init
    
    //Checks if the given unit has items of the given types.
    //Fill the parameters with 0 to have fewer required item types.
    //Works with multiple items of the same type.
    function UnitCheckItems takes unit whichUnit, integer i1, integer i2, integer i3, integer i4, integer i5, integer i6 returns boolean
        local integer array heldItems
        local integer array requestedItems
        local integer index1
        local integer index2
        local integer requestedItemsLength = 6
        
        set requestedItems[0] = i1
        set requestedItems[1] = i2
        set requestedItems[2] = i3
        set requestedItems[3] = i4
        set requestedItems[4] = i5
        set requestedItems[5] = i6
        set heldItems[0] = GetItemTypeId(UnitItemInSlot(whichUnit, 0))
        set heldItems[1] = GetItemTypeId(UnitItemInSlot(whichUnit, 1))
        set heldItems[2] = GetItemTypeId(UnitItemInSlot(whichUnit, 2))
        set heldItems[3] = GetItemTypeId(UnitItemInSlot(whichUnit, 3))
        set heldItems[4] = GetItemTypeId(UnitItemInSlot(whichUnit, 4))
        set heldItems[5] = GetItemTypeId(UnitItemInSlot(whichUnit, 5))
        
        set index1 = 0
        loop
            if requestedItems[index1] == 0 then
                set requestedItemsLength = requestedItemsLength-1
                set requestedItems[index1] = requestedItems[requestedItemsLength]
                if requestedItemsLength == 0 then
                    return true
                endif
            endif
            exitwhen index1 == 5
            set index1 = index1 +1
        endloop
        
        set index1 = 0
        loop
            set index2 = 0
            loop
                exitwhen index2 >= requestedItemsLength
                
                if heldItems[index1] == requestedItems[index2] then
                    set requestedItemsLength = requestedItemsLength-1
                    set requestedItems[index2] = requestedItems[requestedItemsLength]
                    if requestedItemsLength == 0 then
                        return true
                    endif
                    exitwhen true
                endif
                
                set index2 = index2 +1
            endloop
            
            exitwhen index1 == 5
            set index1 = index1 +1
        endloop
        
        return false
    endfunction
    
    function UnitCountItemsOfType takes unit whichUnit, integer itemType returns integer
        local integer result = 0
        local integer i = 0
        loop
            exitwhen i >= 6
            
            if GetItemTypeId(UnitItemInSlot(whichUnit, i)) == itemType then
                set result = result +1
            endif
            
            set i = i +1
        endloop
        
        return result
    endfunction
    
    /*globals
        trigger udg_TriggerUnitCheckItems
        unit udg_UnitCheckItems_Unit
        itemtype array udg_UnitCheckItems_Items
    endglobals*/
    
    private function UnitCheckItemsHandler takes nothing returns boolean
        local boolean b = UnitCheckItems(udg_UnitCheckItems_Unit, udg_UnitCheckItems_Items[0], udg_UnitCheckItems_Items[1], udg_UnitCheckItems_Items[2], udg_UnitCheckItems_Items[3], udg_UnitCheckItems_Items[4], udg_UnitCheckItems_Items[5])
        set udg_UnitCheckItems_Items[0] = 0
        set udg_UnitCheckItems_Items[1] = 0
        set udg_UnitCheckItems_Items[2] = 0
        set udg_UnitCheckItems_Items[3] = 0
        set udg_UnitCheckItems_Items[4] = 0
        set udg_UnitCheckItems_Items[5] = 0
        return b
    endfunction
    
    private function init takes nothing returns nothing
        
        set udg_TriggerUnitCheckItems = CreateTrigger()
        call TriggerAddCondition(udg_TriggerUnitCheckItems, Filter(function UnitCheckItemsHandler))
        
    endfunction
    
    endlibrary
    //
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

You have to make 3 GUI variables:
trigger TriggerUnitCheckItems
unit UnitCheckItems_Unit
itemtype array UnitCheckItems_Items
Then you can do this:
  • Actions
    • Set UnitCheckItems_Unit = (Triggering unit)
    • Set UnitCheckItems_Items[0] = Claws of Attack +15
    • Set UnitCheckItems_Items[1] = Claws of Attack +15
    • Set UnitCheckItems_Items[2] = Crown of Kings +5
    • Set UnitCheckItems_Items[3] = Kelen's Dagger of Escape
    • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
      • If - Conditions
        • (Evaluate TriggerUnitCheckItems conditions) Equal to True
      • Then - Actions
        • -------- unit has the items --------
      • Else - Actions
        • -------- unit doesnt have the items --------
(You do need JNGP cause this uses vJASS.)
 
Level 14
Joined
Nov 30, 2013
Messages
926
Something like this maybe?

Code:
local integer a = 0
local integer b = 0
local integer c = 0
local integer d = 5

loop
   exitwhen b == 6
   if ( *Hero has an item of (What Item) on slot #(b) ) then
      if ( a < (How many number of that item does it requires) ) then
          a = a + 1
          c = a
      endif
   endif
   b = b + 1
endloop

//Item checking
**A unit acquired an item**
if (*Hero has an item of (The item from the recipe, not the item that requires 2 or more) then
    if ( *Hero has an item of (The item that requires 2 or more.) ) ) then
        if (a == (Number of that item that requires)) then
          loop
                if ( *Hero has an item of (Same Item) on slot #(d) ) then
                      **Remove the item from that slot**
                      c = c - 1
                endif
                d = d - 1
                exitwhen c == 0
                if ( c == 0 ) then
                      **Actions here**
                endif
          endloop
        endif
    endif
endif

Fnck my invalid Jass, I removed my WCIII from my PC.
 
Last edited:
Level 24
Joined
Aug 1, 2013
Messages
4,658
doesn't work.

That is quite an informative response...
If it doesn't work, then can you please explain what is not working?

Often when I post some code here, people state that it doesnt work, then they realize that it actually worked, but there was another problem that caused the bug, but as it is my code that was recently added, it is the easiest to just blame that one instead.

In fact:
Pick up two claws of attack and one helmet (those are the requirements) and press [esc].
 

Attachments

  • Test2.w3x
    16.6 KB · Views: 37
Item Stacking System
by Googlieeyes

  • ChargesAddGUI
    • Events
      • Unit - A unit Acquires an item
    • Conditions
      • (Item-class of (Item being manipulated)) Equal to Charged
    • Actions
      • For each (Integer A) from 1 to 6, do (Actions)
        • Loop - Actions
          • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
            • If - Conditions
              • (Item-type of (Item carried by (Triggering unit) in slot (Integer A))) Equal to (Item-type of (Item being manipulated))
              • (Item carried by (Triggering unit) in slot (Integer A)) Not equal to (Item being manipulated)
            • Then - Actions
              • Item - Set charges remaining in (Item carried by (Triggering unit) in slot (Integer A)) to ((Charges remaining in (Item carried by (Triggering unit) in slot (Integer A))) + (Charges remaining in (Item being manipulated)))
              • Item - Remove (Item being manipulated)
            • Else - Actions
This is a basic item stacking system. But, to make new items you can use the same concept and add the conditions you want. Here it add the charges but you could also replace the old items with a new one.

So if Item carried by hero in slot x = item being manipulated, and it is the right type of item for your recipe. Then remove the old 2 items and add the new one. BOOM
 
Level 24
Joined
Aug 1, 2013
Messages
4,658
It's not that simple Legal_Ease.

Imagine my recipe is two daggers and one sword, then I buy both daggers and then one sword, then how will it work?
Item being manipulated should be out of the question when checking recipes in order to make it properly work.

That not to mention that you might have two different items with the same recipe.
 
He only asked to add to items together to make a new one. I gave him the most simple and functional answer. I see nothing in the thread to suggest he understood anything anyone posted. So I almost always post the simplest thing because it is the most helpful. All you fancy coders usually go way over peoples heads (mine included). This isn't to say that you are wrong, all of you are probably better at coding than me, but sometimes people need a place to start where they can understand. If he wanted an advanced system then I'm sure what everyone else wrote will work, I wrote mine in case he needed it simple. And, so yes, it is that simple if that's all you want. Who said he wanted more?
 
Level 24
Joined
Aug 1, 2013
Messages
4,658
"the simplest thing because it is the most helpful."
You might want to try to post the most helpful answer and make it easy to use.

I posted (maybe) the most helpful script that you can imagine... however, he said that it doesnt work.
My bet is that he didnt use it properly because it worked for me, bribe (who made it with me) and everyone else who we gave it to.
In fact, we made this when someone asked almost the same question because that would have been the most helpful answer.

  • Craft
    • Events
      • Unit - A unit Starts the effect of an ability
    • Conditions
    • Actions
      • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
        • If - Conditions
          • (Ability being cast) Equal to Craft Claw
        • Then - Actions
          • Set UnitCheckItems_Unit = (Triggering unit)
          • Set UnitCheckItems_Items[0] = Claws of Attack +3
          • Set UnitCheckItems_Items[1] = Claws of Attack +3
          • Set UnitCheckItems_Items[2] = Claws of Attack +3
          • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
            • If - Conditions
              • (Evaluate TriggerUnitCheckItems conditions) Equal to True
            • Then - Actions
              • Item - Remove (Item carried by UnitCheckItems_Unit of type Claws of Attack +3)
              • Item - Remove (Item carried by UnitCheckItems_Unit of type Claws of Attack +3)
              • Item - Remove (Item carried by UnitCheckItems_Unit of type Claws of Attack +3)
              • Hero - Create Claws of Attack +15 and give it to UnitCheckItems_Unit
            • Else - Actions
              • Game - Display to (All players) the text: You dont have the r...
          • Skip remaining actions
        • Else - Actions
      • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
        • If - Conditions
          • (Ability being cast) Equal to Craft Moonstone
        • Then - Actions
          • Set UnitCheckItems_Unit = (Triggering unit)
          • Set UnitCheckItems_Items[0] = Mooncrystal
          • Set UnitCheckItems_Items[1] = Partial Key of the Three Moons
          • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
            • If - Conditions
              • (Evaluate TriggerUnitCheckItems conditions) Equal to True
            • Then - Actions
              • Item - Remove (Item carried by UnitCheckItems_Unit of type Mooncrystal)
              • Item - Remove (Item carried by UnitCheckItems_Unit of type Partial Key of the Three Moons)
              • Hero - Create Key of Three Moons and give it to UnitCheckItems_Unit
            • Else - Actions
              • Set UnitCheckItems_Unit = (Triggering unit)
              • Set UnitCheckItems_Items[0] = Mooncrystal
              • Set UnitCheckItems_Items[1] = Mooncrystal
              • Set UnitCheckItems_Items[2] = Mooncrystal
              • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                • If - Conditions
                  • (Evaluate TriggerUnitCheckItems conditions) Equal to True
                • Then - Actions
                  • Item - Remove (Item carried by UnitCheckItems_Unit of type Mooncrystal)
                  • Item - Remove (Item carried by UnitCheckItems_Unit of type Mooncrystal)
                  • Item - Remove (Item carried by UnitCheckItems_Unit of type Mooncrystal)
                  • Hero - Create Key of Three Moons and give it to UnitCheckItems_Unit
                • Else - Actions
                  • Set UnitCheckItems_Unit = (Triggering unit)
                  • Set UnitCheckItems_Items[0] = Mooncrystal
                  • Set UnitCheckItems_Items[1] = Mooncrystal
                  • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                    • If - Conditions
                      • (Evaluate TriggerUnitCheckItems conditions) Equal to True
                    • Then - Actions
                      • Item - Remove (Item carried by UnitCheckItems_Unit of type Mooncrystal)
                      • Item - Remove (Item carried by UnitCheckItems_Unit of type Mooncrystal)
                      • Hero - Create Partial Key of the Three Moons and give it to UnitCheckItems_Unit
                    • Else - Actions
                      • Game - Display to (All players) the text: You dont have the r...
          • Skip remaining actions
        • Else - Actions
This works perfect for me.
 

Attachments

  • Test2.w3x
    20.2 KB · Views: 33
Last edited:
Level 24
Joined
Aug 1, 2013
Messages
4,658
Ah, I see.
Well that certainly seems like a lot of code compared to the other pure GUI solutions presented.
Can you quote that solution please?

Your way may provide a better API but it seems like you use more function calls, maybe that's just an illusion the 'huge' code makes when compared to the GUI presented in this thread.
Seems != is.
If you care about function calls:
I use 12 function calls (13 if you also take in the actual call of UnitCheckItems()) in my code. (The GUI code only increases that by about 4 including "(Triggering unit)" (only checking the items which is the only difference here))
In the code that would have given the solution, you have to check for the item type of each item in any case, so you wont have less.
Next to whixh GUI uses BJ functions which create at least double the function calls, your statement of me using more function calls can be removed ok?

On the other hand, I do make a trigger evaluation, which is more expensive than a simple function call.
But I doubt that my code is slower than the code would be in GUI.

Next to the fact that in GUI, you will rewrite my code.
 

Chaosy

Tutorial Reviewer
Level 40
Joined
Jun 9, 2011
Messages
13,207
Good point. Maybe I should actually start to get a it invested.

If I was to create this you merely need an array for each item needed.
itemNeeded[1] = x
itemNeeded[2] = x
itemNeeded[3] = y
loop (1-3)
if unit has item ItemNeeded
remove detected item
itemFound = true
endloop
if itemfound[1-3] == true
create item
else
loop and give items back

that's how I'd do it in GUI, not quite sure if it's the absolute best though. However it's fairly simple which should give it some bonus points.
 
Level 8
Joined
Jan 28, 2016
Messages
158
i am new at triggers so i don't understand variable

plus you use jass for that and jass is uncopyable

you can do it even in GUI.

So what the hex is this??

14ke2pg.jpg
 
Level 24
Joined
Aug 1, 2013
Messages
4,658
Sorry, I forgot you cant read.
I told you that you had to copy that, just like that and keep it like that, and it will work like that.

The ONLY thing you want to care about is the GUI part.
You need JASS to make it work, so get over it and copy and paste it there.
Also, you need JNGP... which I also mentioned -_-
I mean seriously, learn to read.
Your posts dont even make sense any more.
 
Level 24
Joined
Aug 1, 2013
Messages
4,658
JASS is executable code.
A Minecraft command block is nowhere near that power.

GUI Triggers are an "easy", "user friendly" version of JASS where you can easily make simple behavior of the map and its content.

Pure JASS is a relatively simple (text based) code syntax to execute native functions and store data in variables. GUI Triggers are converted to JASS code when you save the map.

However, you dont have to understand JASS, you only have to understand GUI Triggers.
 
Status
Not open for further replies.
Top