@Mad Nightmare, if you're using the object screen x(objID) and object screen y(objID) functions like this:
if object screen x(objID)>old_m_x and object screen x(objID)<m_x
if object screen y(objID>old_m_y and object screen y(objID)<m_y
unit(index).isSelected=true
endif
endif
if object screen x(objID)<old_m_x or object screen x(objID)>m_x
unit(index).isSelected=false
endif
if object screen y(objID)<old_m_y or object screen y(objID)>m_y
unit(index).isSelected=false
endif
when you check for the the units being selected,
your code will run slower than if you assign the return of those values prior to checking it. I made the basis of an RTS before I got frustrated with DBPro's types, and here's how I did the main loop
do
cameraTimer.time=hitimer()
for i=0 to get array count(unit(),1)
unit(i).unit_timer.time=hitimer()
next i
//pasting GUI sprites(so text can be rendered over them)
paste sprite GUI_unitdisplay.sprID,0,screen_height-image height(GUI_unitdisplay.imgID)
paste sprite GUI_minimap_rt_sprID,image width(GUI_unitdisplay.imgID),screen_height-image height(GUI_minimap.imgID)
paste sprite GUI_minimap.sprID,image width(GUI_unitdisplay.imgID),screen_height-image height(GUI_minimap.imgID)
//one time system calls
mouse_x=mousex()
mouse_y=mousey()
mouse_click=mouseclick()
sprite SPRITE_MOUSECOL,mouse_x,mouse_y,SPRITE_MOUSECOL
if mouse_click<>1
if drawSelectBox=true
drawSelectBox=false
old_mouse_x=0
old_mouse_y=0
endif
bldg_selected=false
endif
if mouse_click=1 and mouse_y<screen_height-256
pick_obj=pick object(mouse_x,mouse_y,0,500)
if drawSelectBox=false
drawSelectBox=true
old_mouse_x=mouse_x
old_mouse_y=mouse_y
endif
endif
for k=0 to get array count(bldg(),1)
if mouse_click=1
if bldg(k).isSelected=false
if pick_obj=bldg(k).objID
bldg(k).isSelected=true
bldg_selected=true
endif
endif
if bldg(k).isSelected=true
if mouse_y<screen_height-256
if pick_obj<>bldg(k).objID
bldg(k).isSelected=false
endif
endif
if mouse_y>screen_height-256
if sprite collision(SPRITE_MOUSECOL,GUI_bldgcmds(0).sprID)
bldg(k).setSpwnPnt=true
endif
if sprite collision(SPRITE_MOUSECOL,GUI_bldgcmds(1).sprID) or sprite collision(SPRITE_MOUSECOL,GUI_bldgcmds(2).sprID)
bldg(k).isSpawning=true
bldg(k).isIdle=false
endif
endif
endif
endif
if mouse_click=2
if bldg(k).isSelected=true
if mouse_y<screen_height-256
//this function sets the spawn point
bldg_getMouse3DCoord(k,mouse_x,mouse_y)
endif
endif
endif
select bldg(k).build_type
case BLDG_TOWNCENTER :
if bldg(k).isSelected=true
//Display information about this building, and show sprites that are supposed to be shown
D3D_StartText
D3D_Text 1,100,screen_height-190,0,Str$(bldg(k).health)
D3D_Text 1,100,screen_height-150,0,Str$(bldg(k).attack)
D3D_Text 1,100,screen_height-110,0,Str$(bldg(k).defense)
D3D_EndText
paste sprite GUI_bldgcmds(0).sprID,400,screen_height-190
paste sprite GUI_bldgcmds(1).sprID,500,screen_height-190
endif
if bldg(k).isSpawning=true
//spawn unit
endif
if bldg(k).isAttacking=true
//attack target objID until it's dead then switch to another
endif
if bldg(k).isAttacked=true
bldg(k).isAttacking=true
bldg(k).isAttacked=false
//revert to isAttacking, set target unit(objID) to first detected(first in array)
endif
endcase
case BLDG_BARRACKS :
if bldg(k).isSelected=true
D3D_StartText
D3D_Text 1,100,screen_height-190,0,Str$(bldg(k).health)
D3D_Text 1,100,screen_height-150,0,Str$(bldg(k).attack)
D3D_Text 1,100,screen_height-110,0,Str$(bldg(k).defense)
D3D_EndText
paste sprite GUI_bldgcmds(0).sprID,400,screen_height-190
paste sprite GUI_bldgcmds(2).sprID,500,screen_height-190
endif
if bldg(k).isSpawning=true
//spawn unit
endif
if bldg(k).isAttacking=true or bldg(k).isAttacked=true
//can't do anything to enemies, perhaps call attention to it?
endif
endcase
endselect
next k
for i=0 to get array count(unit(),1)
objScrX=object screen x(unit(i).objID)
objScrY=object screen y(unit(i).objID)
if drawSelectBox=true and mouse_click=1 and bldg_selected=false
selectUnit(objScrX,objScrY,i,old_mouse_x,old_mouse_y,mouse_x,mouse_y)
endif
if unit(i).isSelected=true
D3D_StartText
D3D_Text 1,100,screen_height-190,0,Str$(unit(i).health)
D3D_Text 1,100,screen_height-150,0,Str$(unit(i).attack)
D3D_Text 1,100,screen_height-110,0,Str$(unit(i).defense)
D3D_EndText
endif
if mouse_click=2 and unit(i).mouse_rclicked=false
unit(i).mouse_rclicked=true
if unit(i).isSelected=true
unit_getMouse3DCoord(i,mouse_x,mouse_y)
unit(i).isMoving=true
unit(i).isIdle=false
unit(i).angleToTgt=atanfull(unit(i).tgt.x-unit(i).pos.x,unit(i).tgt.z-unit(i).pos.z)
endif
endif
if mouse_click<>2 then unit(i).mouse_rclicked=false
if unit(i).isMoving=true
position object unit(i).objID,unit(i).pos.x,unit(i).pos.y,unit(i).pos.z
yrotate object unit(i).objID,unit(i).angleToTgt
if getSQ_2DDist(i)<unit(i).speedSQ
unit(i).isMoving=false
unit(i).isIdle=true
endif
endif
if unit(i).unit_timer.time>=unit(i).unit_timer.next#
if unit(i).isMoving=true
unit(i).pos.x=newxvalue(unit(i).pos.x,unit(i).angleToTgt,unit(i).speed*unit(i).unit_timer.delta)
unit(i).pos.y=unit(i).pos.y
unit(i).pos.z=newzvalue(unit(i).pos.z,unit(i).angleToTgt,unit(i).speed*unit(i).unit_timer.delta)
endif
unit(i).unit_timer.delta=updateDelta(unit(i).unit_timer.time,unit(i).unit_timer.next#,unit(i).unit_timer.fps,unit(i).unit_timer.delta)
unit(i).unit_timer.next#=updateNext(unit(i).unit_timer.time,unit(i).unit_timer.fps)
endif
next i
if cameraTimer.time>=cameraTimer.next#
moveCamera()
endif
updateMapCamera()
if escapekey()
exit
endif
D3D_StartText
D3D_Text 1,10,10,0,"FPS:"+Str$(screen fps())
D3D_EndText
sync
loop
close datafile 1
end
function drawSelectionBox( x1 as integer , y1 as integer , x2 as integer , y2 as integer )
local temp as integer
if x1>x2
temp=x1
x1=x2
x2=temp
endif
if y1>y2
temp=y1
y1=y2
y2=temp
endif
D3D_line x1,y1,x2,y1,D3D_RGBA(255,255,255,180)
D3D_line x1,y1,x1,y2,D3D_RGBA(255,255,255,180)
D3D_line x2,y2,x2,y1,D3D_RGBA(255,255,255,180)
D3D_line x2,y2,x1,y2,D3D_RGBA(255,255,255,180)
endfunction
function selectUnit(objX as integer ,objY as integer ,index as integer ,old_m_x as integer ,old_m_y as integer ,m_x as integer ,m_y as integer )
if old_m_x>m_x
if old_m_y>m_y
mouse_UL=true
mouse_DL=false
mouse_UR=false
mouse_DR=false
endif
if old_m_y<m_y
mouse_DL=true
mouse_UL=false
mouse_UR=false
mouse_DR=false
endif
endif
if old_m_x<m_x
if old_m_y>m_y
mouse_UR=true
mouse_UL=false
mouse_DL=false
mouse_DR=false
endif
if old_m_y<m_y
mouse_DR=true
mouse_DL=false
mouse_UL=false
mouse_UR=false
endif
endif
//draw the selection box and put object's screen coordinates into variables
drawSelectionBox(old_m_x,old_m_y,m_x,m_y)
//if unit is inside the selection box area then it is selected
if mouse_DR=true
if objX>old_m_x and objX<m_x
if objY>old_m_y and objY<m_y
unit(index).isSelected=true
endif
endif
if objX<old_m_x or objX>m_x
unit(index).isSelected=false
endif
if objY<old_m_y or objY>m_y
unit(index).isSelected=false
endif
endif
if mouse_DL=true
if objX<old_m_x and objX>m_x
if objY>old_m_y and objY<m_y
unit(index).isSelected=true
endif
endif
if objX>old_m_x or objX<m_x
unit(index).isSelected=false
endif
if objY<old_m_y or objY>m_y
unit(index).isSelected=false
endif
endif
if mouse_UR=true
if objX>old_m_x and objX<m_x
if objY<old_m_y and objY>m_y
unit(index).isSelected=true
endif
endif
if objX<old_m_x or objX>m_x
unit(index).isSelected=false
endif
if objY>old_m_y or objY<m_y
unit(index).isSelected=false
endif
endif
if mouse_UL=true
if objX<old_m_x and objX>m_x
if objY<old_m_y and objY>m_y
unit(index).isSelected=true
endif
endif
//if the object isn't within the selection box(going up-left)
if objX>old_m_x or objX<m_x
unit(index).isSelected=false
endif
if objY>old_m_y or objY<m_y
unit(index).isSelected=false
endif
endif
endfunction
//this function will end up like below
//where index is the current value of the for loop this function will be called from
function unit_getMouse3DCoord( index as integer , mouse_x as integer , mouse_y as integer )
local null
null=pick object(mouse_x,mouse_y,groundObjID,groundObjID)
if null=0
unit(index).isMoving=false
unit(index).isIdle=true
else
unit(index).tgt.x=camera position x(0)+get pick vector x()
unit(index).tgt.z=camera position z(0)+get pick vector z()
unit(index).isMoving=true
unit(index).isIdle=false
unit(index).angleToTgt=atanfull(unit(i).tgt.x-unit(i).pos.x,unit(i).tgt.z-unit(i).pos.z)
endif
endfunction
function bldg_getMouse3DCoord(index as integer , mouse_x as integer , mouse_y as integer )
local null
null=pick object(mouse_y,mouse_y,groundObjID,groundObjID)
if null=0
bldg(index).setSpwnPnt=false
else
bldg(index).spwnpnt.x=camera position x(0)+get pick vector x()
bldg(index).spwnpnt.z=camera position z(0)+get pick vector z()
bldg(index).setSpwnPnt=false
endif
endfunction
//gets the squared distance
function getSQ_2DDist( index as integer )
local dist : local dx : local dy : local dz
dx=unit(index).tgt.x-unit(index).pos.x
//dy=unit(index).tgt.y-unit(index).pos.y
dz=unit(index).tgt.z-unit(index).pos.z
dist=(dx*dx)+(dz*dz)
endfunction dist
function moveCamera()
if mouse_x<15
if mouse_x<7
main_Camera.pos.x=main_Camera.pos.x-(5*cameraTimer.delta)
else
main_Camera.pos.x=main_Camera.pos.x-(2.5*cameraTimer.delta)
endif
endif
if mouse_x>screen_width-15
if mouse_x>screen_width-8
main_Camera.pos.x=main_Camera.pos.x+(5*cameraTimer.delta)
else
main_Camera.pos.x=main_Camera.pos.x+(2.5*cameraTimer.delta)
endif
endif
if mouse_y<15
if mouse_y<7
main_Camera.pos.z=main_Camera.pos.z+(5*cameraTimer.delta)
else
main_Camera.pos.z=main_Camera.pos.z+(2.5*cameraTimer.delta)
endif
endif
if mouse_y>screen_height-15
if mouse_y>screen_height-8
main_Camera.pos.z=main_Camera.pos.z-(5*cameraTimer.delta)
else
main_Camera.pos.z=main_Camera.pos.z-(2.5*cameraTimer.delta)
endif
endif
cameraTimer.delta=updateDelta(cameraTimer.time,cameraTimer.next#,cameraTimer.fps,cameraTimer.delta)
cameraTimer.next#=updateNext(cameraTimer.time,cameraTimer.fps)
position camera main_Camera.camID,main_Camera.pos.x,main_Camera.pos.y,main_Camera.pos.z
endfunction
function updateMapCamera()
map_Camera.pos.x=main_Camera.pos.x
map_Camera.pos.y=map_Camera.pos.y
map_Camera.pos.z=main_Camera.pos.z
position camera map_Camera.camID,map_Camera.pos.x,map_Camera.pos.y,map_Camera.pos.z
endfunction
Note: A lot of this code is specific to my game and how I set it up, but you should be able to see that I only used function calls when they were absolutely needed. With that main loop, I got ~500 fps.