Select Random Type

Blitz3D Forums/Blitz3D Programming/Select Random Type

xmlspy(Posted 2006) [#1]
How would you do a random type selection function? Let's say I want my spaceship to target a random enemy?

Type Enemy...


Steven Noyce(Posted 2006) [#2]
Here is a simple little program. It does not realy do much, but if you look at the code I think it does basicaly what you want. Use the left mouse button to select a new random type.
Type whatever
	Field number
End Type

For whatevers=0 To 100
	w.whatever=New whatever
	w\number=whatevers
Next

While Not KeyDown(1)
Delay 1
If MouseHit(1)
	pick=Rand(0,100)
EndIf

For w.whatever=Each whatever
	If w\number=pick
		Print w\number
	EndIf
Next

Wend

If this is not what you want, just say so!


Matty(Posted 2006) [#3]
or another way is like this:

enemycount=0
for enemy.enemyobject=each enemyobject
enemycount=enemycount+1
next
enemyindex=rand(1,enemycount)
targetenemy.enemyobject=null
for enemy.enemyobject=each enemyobject
enemyindex=enemyindex-1

if enemyindex=0 then 
targetenemy.enemyobject=enemy.enemyobject
exit
endif
next

if targetenemy.enemyobject<>null then 
;do what you want with the selected enemy
endif