HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Drop random item from invintory

01-20-2007, 09:14 PM#1
TheXenocide
I would like to make it so that a specific spell when cast on a unit will cause the unit to randomly drop two items.
01-20-2007, 09:28 PM#2
Feroc1ty
set random = random between 1-6
drop item from targeted unit slot 'random'
01-20-2007, 09:54 PM#3
maximilianx
Trigger:
spell
Collapse Events
Unit - A unit Starts the effect of an ability
Collapse Conditions
(Ability being cast) Equal to YourSpell
Collapse Actions
Hero - Drop (Item carried by (Target unit of ability being cast) in slot (Random integer number between 1 and 6)) from (Target unit of ability being cast)
01-20-2007, 10:26 PM#4
The)TideHunter(
Quote:
Originally Posted by maximilianx
Trigger:
spell
Collapse Events
Unit - A unit Starts the effect of an ability
Collapse Conditions
(Ability being cast) Equal to YourSpell
Collapse Actions
Hero - Drop (Item carried by (Target unit of ability being cast) in slot (Random integer number between 1 and 6)) from (Target unit of ability being cast)

...
Say the hero has 3 items, in slot 1, 2 and 3.
The random number is 6, its obviously not going to drop a item...

Here is a trigger that will work:

Ok, this is quite complicated.
Put this code in your main map script (to open your main map script, on the trigger list to the left, click your maps name at the top) then paste this code into it:

Collapse JASS:
function DropTwoRandomItems takes unit u returns nothing
    local integer i = 0
    local integer n = 0
    loop
        exitwhen i==6
        if(UnitItemInSlot(u, i)!=null) then
            set n=n+1
        endif
        set i=i+1
    endloop
    set i=0
    loop
        exitwhen n==0
        set i = GetRandomInt(1, 6)
        if(UnitItemInSlot(u, i)!=null) then
            call UnitRemoveItemFromSlot(u, i)
            set n=n-1
        endif
    endloop
endfunction

Then, to drop 2 random items, use this trigger:

Trigger:
Custom script: call DropTwoRandomItems(GetTriggerUnit())

All you have to do, it write "call DropTwoRandomItems()", and have a unit in between the "()", which should be GetTriggerUnit(), because that gets the triggering unit, so a final trigger would be:

Trigger:
Drop Random Item
Collapse Events
Unit - A unit Starts the effect of an ability
Collapse Conditions
(Ability being cast) Equal to Your Spell
Collapse Actions
Custom script: call DropTwoRandomItems(GetTriggerUnit())

If you need any help with this, you know where to post.
01-20-2007, 11:08 PM#5
Ammorth
Um, unless I am mistaken that second loop will loop until All items have dropped.

This should fix it:
Collapse JASS:
function DropTwoRandomItems takes unit u returns nothing
    local integer i = 0
    local integer n = 0
    local integer d = 0
    loop
        exitwhen i==6
        if(UnitItemInSlot(u, i)!=null) then
            set n=n+1
        endif
        set i=i+1
    endloop
    set i=0
    loop
        exitwhen n==0 or d==2
        set i = GetRandomInt(0, 5)
        if(UnitItemInSlot(u, i)!=null) then
            call UnitRemoveItemFromSlot(u, i)
            set n=n-1
            set d=d+1
        endif
    endloop
endfunction
01-21-2007, 04:48 AM#6
TheXenocide
Thanks, that is exactly what I was looking for. I dont know jass, but that seems fairly straightforeward. I had realised that it wouldnt work as the first two suggested (thanks for the imput though), but I didnt have enought time to elaborate on the problem.

-TheXenocide
01-21-2007, 06:18 AM#7
Pyrogasm
Erm... you're not going to want to use GetTriggerUnit(), because you don't want to drop items from the triggering (otherwise known as casting) unit. You'd want to drop them from the target of the spellcast using GetSpellTargetUnit().
01-21-2007, 07:25 AM#8
TheXenocide
Actually I didnt really care, because i have the target unit in a variable, i just needed to see how it was coded in jass. I just phrased the question in a general manner so that i could modify it for my needs.
01-21-2007, 08:20 AM#9
zen87
you use 2 loops, first check the target have how many items in his inventory, if 2 or less, drop all, if more than 2, then run another loop to drop 2 random item.

Collapse JASS:
function drop2items takes unit target returns nothing
 local integer n=0
 local integer r=0
 local real x = GetUnitX(target)
 local real y = GetUnitY(target)
    loop
        if UnitItemInSlot(target,n) !=null then
            set r=r+1
        endif
        set n=n+1
        exitwhen n>5
    endloop
    set n=0
    if r<=2 then
        loop
            call UnitDropItemPoint(target,UnitItemInSlot(target,n),x,y)
            set n=n+1
            exitwhen n>5
        endloop
    else
        loop
            set r = GetRandomInt(0,5)
            if UnitItemInSlot(target,r) !=null then
                call UnitDropItemPoint(target,UnitItemInSlot(target,r),x,y)
                set n=n+1
            endif
            exitwhen n>=2
        endloop
    endif
endfunction

tide n Ammorth : if im not wrong the item slow start from 0 and ends with 5... O_o

well i didnt do any syntax check, but it should be something like this
01-21-2007, 10:50 AM#10
The)TideHunter(
Quote:
Originally Posted by zen87
you use 2 loops, first check the target have how many items in his inventory, if 2 or less, drop all, if more than 2, then run another loop to drop 2 random item.

Collapse JASS:
function drop2items takes unit target returns nothing
 local integer n=0
 local integer r=0
 local real x = GetUnitX(target)
 local real y = GetUnitY(target)
    loop
        if UnitItemInSlot(target,n) !=null then
            set r=r+1
        endif
        set n=n+1
        exitwhen n>5
    endloop
    set n=0
    if r<=2 then
        loop
            call UnitDropItemPoint(target,UnitItemInSlot(target,n),x,y)
            set n=n+1
            exitwhen n>5
        endloop
    else
        loop
            set r = GetRandomInt(0,5)
            if UnitItemInSlot(target,r) !=null then
                call UnitDropItemPoint(target,UnitItemInSlot(target,r),x,y)
                set n=n+1
            endif
            exitwhen n>=2
        endloop
    endif
endfunction

tide n Ammorth : if im not wrong the item slow start from 0 and ends with 5... O_o

well i didnt do any syntax check, but it should be something like this

The code that Ammorth fixed should work fine.
Just add a -1 to the end of GetRandomInt, it is 0-5 =/.
01-21-2007, 02:38 PM#11
Ammorth
Quote:
Originally Posted by The)TideHunter(
The code that Ammorth fixed should work fine.
Just add a -1 to the end of GetRandomInt, it is 0-5 =/.

Updated in the post.
01-22-2007, 04:04 AM#12
TheXenocide
I recieved a compile error, I couldn't figure out how to call the function when passing it an array.

The udg_stack_target is an array of units where udg_stack_numberSpells is the index.
Also, what is the jass command for destroying an item at position x?

Thanks for all your help so far,
-TheXenocide
Attached Images
File type: gifcompile error.GIF (20.6 KB)
01-22-2007, 03:00 PM#13
The)TideHunter(
To destroy a item:
native RemoveItem takes item whichItem returns nothing
To make a unit drop a item:
native UnitRemoveItem takes unit whichUnit, item whichItem returns nothing

call RemoveItem(SomeItem) call UnitRemoveItem(SomeUnit, SomeItem)
01-22-2007, 03:56 PM#14
Fireeye
For the compile error, are you sure you wrote the function name correct (including big and small letters)?
01-22-2007, 04:35 PM#15
The)TideHunter(
Well, whats the function Discard for?
Does it even exsist? The parser says different.