Picking a random member of a Type List
BlitzMax Forums/BlitzMax Beginners Area/Picking a random member of a Type List
| ||
Let me introduce a problem, I have a tlist that contains all my boss_fragment instances. The name of the tlist is called boss_fragment_list. I need to pick any instance from that list at random and tell it to proform a spawn() function how would this be done? Thanks in advance. |
| ||
you know how big the list is (List.Count) so you can make a random number from 1 to Count. Then loop through the list with For m:MyType = Each In MyList and keep a counter incrementing each time and when the counter is the same as the precalculated random number, use Exit to break out of the list and m is set to the random type. Hope that makes sense. |
| ||
Ok thanks, sometimes I tend to overcomplicatte things. |
| ||
keep a counter incrementing each time and when the counter is the same as the precalculated random number, use Exit to break out of the list and m is set to the random type. Or just use ValueAtIndex. |
| ||
Agree with PerturbatioLocal P1:TParticle=TParticle(ParticleList.ValueAtIndex(A)) |
| ||
ValueatIndex uses a loop too, so bare in mind it will be as slow as the first method be nice if you could go straight to index A .... |
| ||
be nice if you could go straight to index A .... you could always use an array like this |
| ||
Well I will be having hundreds of objects checking each other at once so optomizations are very important. |
| ||
Ryan: Use my IList code if you'll be having hundreds of objects. It's much faster if you're sorting or getting an object at a specific index. Using my code you can do pretty much the same thing as with TLists for getting objects at a specific index: Local f:Foo = Foo(MyList.ValueAtIndex( Rand( 0, MyList.Count( )-1 ) )) |
| ||
That looks good, Noel...What type should I use for what sort of problems? |
| ||
I use hash tables for storing data over long periods of time (it pretty much ensures that the data is going to stay in memory since it's going to have a reference until I remove it from the table). Binary trees.. Um, yeah, not sure what you'd use those for, I just wrote it for fun. And you can probably find some use for a linked list where others fail. |
| ||
Thanks, Noel |
| ||
Yes, thanks Noel, do you wish to be credited in my game? P.S. Lego for the win!! |
| ||
No, don't care. |
| ||
Actually speed isn't important in this case here is my function. It says Compile Error: Unable to convert from 'boss_fragment' to 'Int' Function choose_cell() Local member%=Rand(1,CountList(boss_fragment.boss_fragment_list)) Local counter%=0 For Local i:boss_fragment=EachIn(boss_fragment.boss_fragment_list) counter:+1 If counter=member Return i Exit End If Next End Function |
| ||
Function choose_cell:boss_fragment() |