| 01-20-2007, 09:14 PM | #1 |
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 |
set random = random between 1-6 drop item from targeted unit slot 'random' |
| 01-20-2007, 09:54 PM | #3 |
| 01-20-2007, 10:26 PM | #4 | |
Quote:
... 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: 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: If you need any help with this, you know where to post. |
| 01-20-2007, 11:08 PM | #5 |
Um, unless I am mistaken that second loop will loop until All items have dropped. This should fix it: 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 |
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 |
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 |
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 |
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. 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 | |
Quote:
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 | |
Quote:
Updated in the post. |
| 01-22-2007, 04:04 AM | #12 |
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 |
| 01-22-2007, 03:00 PM | #13 |
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 |
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 |
Well, whats the function Discard for? Does it even exsist? The parser says different. |
