[Solved] Difficulty understanding loop + random integers

Status
Not open for further replies.
Level 9
Joined
Mar 17, 2016
Messages
153
Hi, I want to make a simple drop system where every time a unit dies there is a small chance to get 1 of many items at random. The items will be non-recurring (only drop once per game). I don't want them to drop in a linear format, and I think that's what I'm having the most trouble understanding. Eg. the first item to drop could be #6, the second #1, the third #8. Will this suffice/are there issues/could it be done better? Thanks!

  • ItemDrop RiddleMap
    • Events
      • Unit - A unit Dies
    • Conditions
      • (Random real number between 0.00 and 100.00) Less than or equal to 0.25
    • Actions
      • Set VariableSet ItemDropTempPoint = (Position of (Dying unit))
      • For each (Integer ItemDropInteger) from 1 to 10, do (Actions)
        • Loop - Actions
          • Set VariableSet ItemDropInteger = (Random integer number between 1 and 10)
          • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
            • If - Conditions
              • ItemDropBoolean[ItemDropInteger] Equal to False
            • Then - Actions
              • Custom script: exitwhen true
              • Set VariableSet ItemDropBoolean[ItemDropInteger] = True
              • Item - Create ItemDropType[ItemDropInteger] at ItemDropTempPoint
              • Custom script: call RemoveLocation(udg_ItemDropTempPoint)
            • Else - Actions
 
Level 25
Joined
Feb 9, 2009
Messages
1,800
If you are experiencing the same items dropping regardless of what you do be sure to check that silly setting is off in your preferences: Preferences>Testmap (Tab)>Fixed random seed, uncheck it and you should get random results instead of the same sequence.
Lastly, the location is destroyed after one instance, move the (RemoveLocation) script to outside of the loop.
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,285
Generally the best solution is to use a set of possible drops and pick and remove a random element from that set each time. Such a set could be implemented using an array and a count variable where element removal replaces the removed element with the element at index count (end element) and decrements count.
 
Level 9
Joined
Mar 17, 2016
Messages
153
Thank you both for the info! I've gotten it working & moved the RemovePoint out of the loop :)
I'm going to stick to this version because It's very simple and I'm not actually knowledgeable about script/code outside of what I've researched myself to make this RPG. Perhaps one day I'll understand!
 

Uncle

Warcraft Moderator
Level 73
Joined
Aug 10, 2018
Messages
7,866
Thank you both for the info! I've gotten it working & moved the RemovePoint out of the loop :)
I'm going to stick to this version because It's very simple and I'm not actually knowledgeable about script/code outside of what I've researched myself to make this RPG. Perhaps one day I'll understand!
What the Dr is saying is actually quite simple and I think seeing it would help a lot.

  • Actions
    • Set Variable RandomNumber = (Random integer number between 1 and TotalItems)
    • Item - Create ItemType[RandomNumber] at (Center of (Playable map area))
    • Set Variable ItemType[RandomNumber] = ItemType[TotalItems]
    • Set Variable TotalItems = (TotalItems - 1)
ItemType[1] through [10] are set to the different item-types
TotalItems is set to 10 because there are 10 item-types in total


So if RandomNumber lands on 4, the trigger would create ItemType[4], and then change the variable ItemType[4] to be equal to ItemType[10]. So the last ItemType would replace the randomed ItemType.

Then the trigger subtracts 1 from TotalItems, removing ItemType[10] from the Array. But remember, ItemType[10] was just moved from Index position [10] to Index position [4], so it's still able to be selected. Now the next time the trigger runs it will set RandomNumber to a value between 1 and 9.

In other words, if ItemType[4] = Boots of Speed and ItemType[10] = Claws of Attack, ItemType[4] would be changed to Claws of Attack, and ItemType[10] would be removed from the possible outcomes (since it now rolls a random number between 1 and 9).

If the process were to repeat again, the next RandomNumber would be replaced by ItemType[9], then [8], then [7], all the way down to 0.

And if you wanted to prevent Items from dropping once TotalItems reaches 0, you could add a Condition:
  • Conditions:
  • TotalItems Greater than 0
 
Last edited:
Level 9
Joined
Mar 17, 2016
Messages
153
Wow, you know that's actually quite simple with the explanations. Sometimes it really baffles me how helpful this site has been for me. Honestly, the farther I get into making this RPG the more awesome it is to see all the things you can do with a few simple variables & conditions.
 
Status
Not open for further replies.
Top