Heya
I have been working on a selection box system for my game for some while, but there are still bugs in the on i have got here. Im using the 'brute force' approach, checking the distance between the player and every object within a certain range (5-54 inclusive). In my game I dont think a collision-sphere based system would work to well, the loading times are large enough already, and the framerate is slowly decreasing as i add more features. Although a bit laggy, this method seems to be the quickest approach to the problem.
To avoid a for t=5 to 54 loop right in the middle of my game loop (which may slow things down the code begins like this:
---------------------------------------------
and ends with
so each run through of the loop, it checks a different object. Within the loop there is this following section of code:
`Calculate closest selection box position and position accordingly
a#=prox_obj(t,X#,Y#,Z#)
if a#>0 then gloopfunctionglobals(1)=a#
if a#=0 then gloopfunctionglobals(1)=0
if a#>0
posit_boxes(a#)
else
if gloopfunctionglobals(1)>0
posit_boxes(a#)
endif
endif
Note - a# is the object for which the selection box must be positioned against, and gloopfunctionglobals(1) is the last object to have the box positioned against it, the prox_obj and posit_boxes functions are as below:
`Position selection box
function posit_boxes(t#)
`I have no idea why it does this but it requires these to be here
` it either does a severe exception or states that object num must be between blah and blah, even though the loop shouldnt allow values<5 or >54
if t#=0 then exitfunction 0
if t#<1 then exitfunction
if t#>54 then exitfunction
if object exist(t#)=1
sprite 1,object screen x(t#)-40,object screen y(t#)-65,100
text object screen x(t#),object screen y(t#),interdat$(t#,2)
set cursor 40,40
print t#
endif
exitfunction 1
endfunction
`Find the closest object
function prox_obj(t,x#,y#,z#)
if object exist(t)=1
dist#=(x#-object position x(t))^2+(y#-object position y(t))^2+(z#-object position z(t))^2
else
dist#=0
endif
if dist#<90000 and dist#>0 then exitfunction t
if dist#>89401
exitfunction 0
endif
if gloopfunctionglobals(1)>0
if object exist(gloopfunctionglobals(1))=1
oldist#=dist#=(x#-object position x(gloopfunctionglobals(1)))^2+(y#-object position y(gloopfunctionglobals(1)))^2+(z#-object position z(gloopfunctionglobals(1)))^2
if oldist#>90000
gloopfunctionglobals(1)=0
exitfunction 0
endif
if dist#=0 and oldist#<90001 then exitfunction gloopfunctionglobals(1)
endif
endif
exitfunction 0
endfunction
Problem is with this code, it seems to work for one object in the world (the first one it selects) and then for all of the others it just flickers on them for a moment and then returns zero, im probably missing something really obvious (its always the way) but I can't see it.
I'd be grateful for any help
Thanks
Toipot
P.s dont worry about the lack of a 'delete sprite' command to get rid of the sprite when there is nothing to position it against, left it out for debug purposes