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

Itempools Guide

MindWorX

Tool Moderator
Level 20
Joined
Aug 3, 2004
Messages
709
This is a tutorial I decided to make, because from what i've experienced, very few people actually know about the itempool natives, and even fewer knows how to use them.

Itempools are an internal system, just like a normal units droptable, you know, the ones where you doubleclick a unit, and sets it's droptable.

Short explanation of the natives:

JASS:
//Creates an itempool.
native CreateItemPool           takes nothing returns itempool

//Destroys an itempool.
native DestroyItemPool          takes itempool whichItemPool returns nothing

//Adds an item to an itempool.
native ItemPoolAddItemType      takes itempool whichItemPool, integer itemId, real weight returns nothing

//Removes an item from an itempool.
native ItemPoolRemoveItemType   takes itempool whichItemPool, integer itemId returns nothing

//Creates a random item from an itempool.
native PlaceRandomItem          takes itempool whichItemPool, real x, real y returns item
That's the natives from Common.j.

Here's some examples:
This creates an item pool, with 1/2(50%) chance of dropping item 'I001' and 1/2(50%) chance of dropping item 'I002'.
JASS:
 local itempool ip = CreateItemPool()
    call ItemPoolAddItemType(ip, 'I001', 1)
    call ItemPoolAddItemType(ip, 'I002', 1)

This creates an item pool, with 1/3(33%) chance of dropping item 'I001' and 2/3(66%) chance of dropping item 'I002'.
JASS:
 local itempool ip = CreateItemPool()
    call ItemPoolAddItemType(ip, 'I001', 1)
    call ItemPoolAddItemType(ip, 'I002', 2)

This creates an item pool, with 1/5(20%) chance of dropping item 'I001', 2/5(40%) chance of dropping item 'I002' and 2/5(40%) chance of dropping item 'I003'.
JASS:
 local itempool ip = CreateItemPool()
    call ItemPoolAddItemType(ip, 'I001', 1)
    call ItemPoolAddItemType(ip, 'I002', 2)
    call ItemPoolAddItemType(ip, 'I003', 2)

As you can see it's pretty simple actually, the "weight" determines the chance of how often an item will drop. The easiest way to get the drop chance of an itempool, is to add the weight's together and divide the individual weight. Like the last example there's 1+2+2 which is 5, then 'I001' has 1 as weight, then you just do 1/5 which is 0.20, then you multiply by 100, and you have 20%.

To create an item using an itempool, you just use the PlaceRandomItem native, like this:
JASS:
 local itempool ip = CreateItemPool()
    call ItemPoolAddItemType(ip, 'I001', 1)
    call ItemPoolAddItemType(ip, 'I002', 2)
    call ItemPoolAddItemType(ip, 'I003', 2)
    call PlaceRandomItem(ip,x,y)
This will place a random item from the itempool created, and return the item created, incase you want to do something with it.

An example of using the returned item would be the following function i use in hero rewards:
JASS:
function GiveUnitRandomItem takes unit u, itempool ip returns nothing
 local item i = PlaceRandomItem(ip,0,0)
    call UnitAddItem(u,ip)
    set i = null
endfunction

Leaks: You should always remember to destroy itempools that you don't use. And also, remember to null any itempool variabels after you're done using them.

Hope this tutorial helps someone understanding itempools.

- MindWorX
 

Attachments

  • itempool_example.w3x
    12.7 KB · Views: 588
Last edited:
Top