HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Randomly removing an item from a Hero

08-04-2011, 05:13 AM#1
muse1988
Hey guys,

I've been failing miserably at trying this trigger. I am trying to make a spell that randomly removes up to 2 items(random slots) from an enemy Hero.

My trigger doesnt seem to count or detect any items so it doesn't remove anything

Trigger:
Mind Rot
Collapse Events
Unit - A unit Begins casting an ability
Collapse Conditions
(Ability being cast) Equal to Mind Rot
Collapse Actions
Set DiscardCardCounter[1] = 0
Set P1_Rnd = (Random integer number between 1 and 6)
Wait 0.01 seconds
Collapse For each (Integer A) from 1 to 6, do (Actions)
Collapse Loop - Actions
Collapse If (All Conditions are True) then do (Then Actions) else do (Else Actions)
Collapse If - Conditions
DiscardCardCounter[1] Not equal to 2
Collapse Then - Actions
Collapse If (All Conditions are True) then do (Then Actions) else do (Else Actions)
Collapse If - Conditions
DiscardSlotChecker[(Integer A)] Equal to False
Collapse Then - Actions
Collapse If (All Conditions are True) then do (Then Actions) else do (Else Actions)
Collapse If - Conditions
(Item carried by (Target unit of ability being cast) in slot P1_Rnd) Equal to No item
Collapse Then - Actions
Game - Display to (All players) the text: (removed item + (Name of (Item carried by (Target unit of ability being cast) in slot P1_Rnd)))
Item - Remove (Item carried by (Target unit of ability being cast) in slot P1_Rnd)
Set DiscardSlotChecker[P1_Rnd] = True
Collapse Else - Actions
Set DiscardCardCounter[1] = (DiscardCardCounter[1] + 1)
Set DiscardSlotChecker[P1_Rnd] = True
Else - Actions
Else - Actions
08-04-2011, 08:41 AM#2
Bribe
That's because you're using things like (Target unit of ability being cast) after a wait period. You could be using variables/hashtables in order to track event responses better.
08-04-2011, 10:12 AM#3
Anitarf
As Bribe pointed out, due to a WC3 bug the cast event responses do not work after a wait. Is that wait really necessary, though? You only wait for 0.01 seconds (actually, closer to 0.2 seconds because WC3 waits are not that accurate). If you must have a wait, you should set the two variables after it, not before it. In fact, you should set your P1_Rnd variable inside the loop, else the code will keep checking the same slot.

Aside from that, your if/then/else statements seem oddly structured. You only remove an item from a slot if there is no item in the slot, which does nothing, while you increase the DiscardCardCounter if an item is found, but you don't remove it. I get that DiscardSlotChecker is supposed to mark already checked slots but it won't work correctly on subsequent casts if you don't set it to false before doing the loop. Even if you were to correct these issues, making 6 random checks does not mean you will check all six slots in random order, you might check some slots multiple times while not checking others, so you could end up not removing a single item even if the unit has them.

A simpler way to do this would be to use an item array to create a list of all items carried by the unit. Simply loop through slots 1 to 6 and if the unit has an item in that slot, add it to the array and increase an integer variable that marks how many items you found (make sure you set that variable to 0 before you start the loop). Then, if any items were found, get a random number between 1 and the number of items and remove the item stored in the array under that number. Repeat the process to remove another item.

Edit: I just noticed that ItemUtils includes a UnitGetRandomItem function which already does what I described above.
08-04-2011, 09:35 PM#4
muse1988
Quote:
Originally Posted by Anitarf
A simpler way to do this would be to use an item array to create a list of all items carried by the unit. Simply loop through slots 1 to 6 and if the unit has an item in that slot, add it to the array and increase an integer variable that marks how many items you found (make sure you set that variable to 0 before you start the loop). Then, if any items were found, get a random number between 1 and the number of items and remove the item stored in the array under that number. Repeat the process to remove another item.


Thank you so much, I did this and it works perfectly now!

I also learned a bit more about triggering (I've just started mapping again from a loooong break), thanks again