HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

removing value from array

01-26-2004, 01:12 PM#1
volatile
I have my trigger so it will pick a random number in my array. I need an event that will remember what previous values have been picked and not pick it again. I don't want a number coming up more than once as a random number. If the same number comes up I want it to search for another random number until it gets one that has not already been used. When all random numbers are used (0 - 11) I want the trigger to exit. Here's my trigger.


ChoosingRandomSpell
Events
Conditions
Actions
Set INTEGERVARIABLE = (Random integer number between 0 and 11)
Unit - Add GoodSpells[INTEGERVARIABLE] to (Triggering unit)
01-26-2004, 01:34 PM#2
AllPainful
Do you need the random number back again later?

If not then this is what you want.

Variables:
IntArray - Integer array
IntCount - Integer
RandomInt - Integer

IntArray is an array with all the possible numbers in it that you want to be randomized, IntCount is an integer that shows how many are in the IntArray, and randomint is our random integer temp variable.

2 Triggers:
Code:
Set Variable
Event
  Map Init
Conditions
Actions
  Set IntCount = 0
  Do loop for integer a from 1 to [color=red]#[/color] [color=green]Where # is the highest num you want in the array.[/color]
    Set IntArray[integer a] = integer a
    Set IntCount = IntCount + 1

ChoosingRandomSpell
Events
Conditions
Actions
  If
    IntCount = 0
  Then
    Turn off (this trigger)
    Trigger (skip remaing actions)[color=green] I just put this there in case you had more actions you wanted to add[/color]
  Else
    Set RandomInt = (random integer between 1 and IntCount)
    Unit - Add GoodSpells[IntArray[RandomInt]] to (Triggering unit)
    Set IntArray[RandomInt] = IntArray[IntCount]
    Set IntCount = IntCount - 1

If you need a breakdown of how that works just let me know.


If you want it to be "Reset" instead of stopped, replace the:
Turn off (this trigger)
Trigger (skip remaing actions)
With:
Trigger run (Set Variable)
01-26-2004, 01:45 PM#3
volatile
My trigger above covers getting a random integer and installing the ability into my unit, I just have to remove the number from the array... Maybe I'm not seeing it in your code. I don't understand what you did, maybe you can do it a different way to help me understand?
01-26-2004, 01:48 PM#4
Vexorian
Greetings, hopefully my random number experience will help you:

Map Initialization:

set N=3
set footman[0] = gg_hfoo_0000
set footman[1] = gg_hfoo_0001
set footman[2] = gg_hfoo_0002
set footman[3] = gg_hfoo_0003


The trigger were you pick a random number:

set rnd = Math - Random number betwen 0 and N

Now use footman[rnd] where you need it, after using it do:

set footman[rnd] = footman[N]
set N= N-1
01-26-2004, 01:53 PM#5
volatile
why did you say footman[0], etc...?
01-26-2004, 01:55 PM#6
AllPainful
Heres what I did

randomint = This is NOT the random number were using for the spell, this is the random number were using to get the REAL random number out of an array.

IntArray = THIS is where the random numbers we are going to use are.

IntCount = This is how many numbers are in the array.

So. This is prevent the trigger from running anymore if we have already used all the numbers:
Code:
  If
    IntCount = 0
  Then
    Turn off (this trigger)
    Trigger (skip remaing actions)

Ok, this sets my Randomint (we already know this is NOT the int your using for your spell, this is the int we are using to GET your random int) notice it will not choose a number that is not in the array, because it uses IntCount as its high number, where IntCount is how many numbers are in the array:
Code:
    Set RandomInt = (random integer between 1 and IntCount)
Now, we know IntArray[RandomInt] is our TRUE int that we want to use so, we put it in the GoodSpells.
Code:
    Unit - Add GoodSpells[IntArray[RandomInt]] to (Triggering unit)
Now to remove the used number from the array, lets say the used number was 5 and there was 9 numbers in the array that looked like this 123456789, if we go through what I did in this next action, the array now only has 8 numbers and it looks like this 12349678, Notice the 5 is gone. That is what these 2 actions do.
Code:
    Set IntArray[RandomInt] = IntArray[IntCount]
    Set IntCount = IntCount - 1



Now if you were lost as to what THIS trigger did, its the one that puts all the numbers in the array for us to pull out later:
Code:
Set Variable
Event
  Map Init
Conditions
Actions
  Set IntCount = 0
  Do loop for integer a from 1 to #
    Set IntArray[integer a] = integer a
    Set IntCount = IntCount + 1







Hey Vex, using the 0 index in the array confuses poeple, in case your wondering why I didn't use it. :ggani: