Here's an outline of how I would go about doing it. There's probably lots of ways to make this faster, but here's how I would do it anyway:
for t = 1 to [NumberOfStoredObjectsInArray]
if [DistanceFromTheCameraToTheObjectInThisArraySlot] >500
Exclude Object On [Object]
else
Exclude Object Off [Object]
endif
next t
If you ran that code every loop (or every few loops for even more speed) you would hide all objects that are far away. I use EXCLUDE OBJECT ON/OFF instead of HIDE/SHOW object because hide object doesn't actually improve speed. Even though the object is invisible, its polys still appear in the poly count (I think, anyway. Exclude object ON/OFF
is faster though.)
That code won't actually run. You would have to adapt it to your game, but hopefully it will point you in the right direction. Good luck
EDIT: Whoops! I thought you already had the array made. Try reading some tutorials on arrays, then coming back to this concept.
EDIT2: Alright here's a simple example that doesn't use User Defined Types:
Sync On
Sync Rate 0 `Let the game run as fast as it wants. You can set this to 60 if it's too fast...
Autocam Off
`We will have 8000 cubes.
NumOfCubes=8000
`Now we create our array. It will have one main slot for each cube, and each cube will have 4 of its own slots for stiring X/Y/Z positions and object numbers.
DIM Cubes#(NumOfCubes,4)
`Each cube will have 4 slots:
` slot 1 will hold the X Position
` slot 2 will hold the Y Position
` slot 3 will hold the Z Position
` slot 4 will hold the object number
for c = 1 to NumOfCubes
Cubes#(c,4)= c `this could easily be replaced with a FreeObject function if you wanted to get some free object IDs, but try to understand it the way it is now. The objects are now numbered from 1 to 8000.
`make a cube
make object cube Cubes#(c,4),10
`randomly position it
position object Cubes#(c,4),-2000+rnd(4000),0,-2000+rnd(4000)
`record its position into our array.
Cubes#(c,1)=OBJECT POSITION X(Cubes#(c,4))
Cubes#(c,2)=OBJECT POSITION Y(Cubes#(c,4))
Cubes#(c,3)=OBJECT POSITION Z(Cubes#(c,4))
`color the cubes for fun :D
color object Cubes#(c,4),rgb( rnd(255) , rnd(255) , rnd(255) )
next c
do
set cursor 0,0
print Screen FPS()
control camera using arrowkeys 0,2,1
cx#=camera position x()
cy#=camera position y()
cz#=camera position z()
`Try commenting this part out with REMSTART and REMEND and see how slow it gets :P
`cycle through the cubes.
`remstart
for c = 1 to NumOfCubes
`get the distance between the camera and this particular cube.
Dist#=SQRT( (Cubes#(c,1)-cx#)*(Cubes#(c,1)-cx#) + (Cubes#(c,2)-cy#)*(Cubes#(c,2)-cy#) + (Cubes#(c,3)-cz#)*(Cubes#(c,3)-cz#) )
`if the camera is farther than 200 units OR the cube isn't even visible on the screen, then hide the cube. Otherwise show it.
if Dist#>200 OR Object In Screen( Cubes#(c,4) )=0
exclude object on Cubes#(c,4)
else
exclude object off Cubes#(c,4)
endif
next c
`remend
sync
loop
And an example with User Defined Types:
Sync On
Sync Rate 0 `Let the game run as fast as it wants. You can set this to 60 if it's too fast...
Autocam Off
`We will have 8000 cubes.
NumOfCubes=8000
Type CubesType
obj as integer `The object number of this cube.
X as float `The X Position
Y as float `The Y Position
Z as float `The Z Position
Endtype
`Now we create our array. It will have one main slot for each cube, and each cube will have 4 of its own slots for stiring X/Y/Z positions and object numbers.
DIM Cubes(NumOfCubes) as CubesType
for c = 1 to NumOfCubes
Cubes(c).obj= c `this could easily be replaced with a FreeObject function if you wanted to get some free object IDs, but try to understand it the way it is now. The objects are now numbered from 1 to 8000.
`make a cube
make object cube Cubes(c).obj,10
`randomly position it
position object Cubes(c).obj,-2000+rnd(4000),0,-2000+rnd(4000)
`record its position into our array.
Cubes(c).X=OBJECT POSITION X(Cubes(c).obj)
Cubes(c).Y=OBJECT POSITION Y(Cubes(c).obj)
Cubes(c).Z=OBJECT POSITION Z(Cubes(c).obj)
`color the cubes for fun :D
color object Cubes(c).obj,rgb( rnd(255) , rnd(255) , rnd(255) )
next c
do
set cursor 0,0
print Screen FPS()
control camera using arrowkeys 0,2,1
cx#=camera position x()
cy#=camera position y()
cz#=camera position z()
`Try commenting this part out with REMSTART and REMEND and see how slow it gets :P
`cycle through the cubes.
`remstart
for c = 1 to NumOfCubes
`get the distance between the camera and this particular cube.
Dist#=SQRT( ( Cubes(c).X - cx# )*( Cubes(c).X - cx# ) + ( Cubes(c).Y - cy# )*( Cubes(c).Y - cy# ) + ( Cubes(c).Z - cz# )*( Cubes(c).Z - cz# ) )
`if the camera is farther than 200 units OR the cube isn't even visible on the screen, then hide the cube. Otherwise show it.
if Dist#>200 OR Object In Screen( Cubes(c).obj )=0
exclude object on Cubes(c).obj
else
exclude object off Cubes(c).obj
endif
next c
`remend
sync
loop
The space changes in the distance calculation are just to make things easier to read.
<-- Spell based team dueling game!