I am making RTS but have some nasty problem. If i can make objects move to destination coordinates, later can add path finding function to move between waypoints
i get errors while moving objects
this is main loop part:
if mouseclick()= 1 then activeID= SelectObjects() `left mouse click: select object
SelectionIndicator(activeID) `place indicator over selected object (can load your own object like sword, target sign and etc.)
if mouseclick()= 2 then MakeTarget() `get coordinates
for i=10 to unitcount `if moving to destination coordinates
if unit(i).IsMoving <>0
MoveObject(i)
if abs(unit(i).dx-object position x(i))<1 and abs(unit(i).dz-object position z(i))<1 `if at destination. stop object
unit(i).Ismoving=0
endif
endif
if unit(i).IsFollowing <>0
FollowObject(i)
if abs(unit(i).dx-object position x(Unit(i).MoveTarget))<1 and abs(unit(i).dz-object position z(Unit(i).MoveTarget))<1 `if at destination. stop object
unit(i).IsFollowing=0
unit(i).movetarget=0
endif
endif
next i
and these are functions:
function MakeTarget() `place target indicator if needed (i cant comment this funtion well, because its no my made.
`forum link for it: http://forum.thegamecreators.com/?m=forum_view&t=79830&b=1
obj=SelectObjects() `targeted object number
if obj=0 `if none objects selected
mX = mouseX()
mY = mouseY()
cx# = camera position x()
cy# = camera position y()
cz# = camera position z()
pickState = Pick Object (mX, mY, 1, 1)
If pickState <> 0
Dist# = Get Pick Distance()
Pick Screen mX, mY, Dist#
indX# = cx# + get pick vector x()
indY# = cy# + get pick vector y()
indZ# = cz# + get pick vector z()
Position Object 3, indX#, indY#+3, indZ# `position indicator on terrain
Unit(ActiveID).Dx=indX# `set units destination coordinates
Unit(ActiveID).Dy=indY#
Unit(ActiveID).Dz=indZ#
Unit(ActiveID).IsMoving=0.1 `give starting movement speed
if unit(ActiveID).MoveTarget<>0 then unit(ActiveID).MoveTarget=0
if object visible(3)=0 then show object (3) `handling visibility again
EndIf
else
unit(ActiveID).MoveTarget=obj `set units target value to selected object
Unit(ActiveID).IsFollowing=0.1 `give starting movement speed
if object visible(3)=1 then hide object (3) `handling visibility again
endif
endfunction
`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
function MoveObject(ID)
point object ID, Unit(ID).dx, Unit(ID).dy, Unit(ID).dz
move object ID, Unit(ID).Speed
remstart
Ox#=object position x(ID)
`Oy#=get terrain height (1,Ox#,Oz#)+5
Oz#=object position z(ID)
`position object ID,Ox#,Oy#,Oz#
remend
endfunction
`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
function FollowObject(ID)
point object ID, object Position X(Unit(ID).Movetarget), object Position Y(Unit(ID).Movetarget),object Position Z(Unit(ID).Movetarget)
move object ID, Unit(ID).Speed
remstart
Ox#=object position x(ID)
Oy#=get terrain height (1,Ox#,Oz#)+5
Oz#=object position z(ID)
position object ID,Ox#,Oy#,Oz#
remend
endfunction
when i move object and after that select other target, program crashes. It crashes when try to place objects on ground height too (comment style code in last two functions)
here is full program if need:
Rem Project: Terrain
`--- Constants ---
#constant UnitCount 30
`--- Types ---
type Units `u can add more stats in here, atm for program work need only "target" variable
`hp as integer
`dmg as integer
Speed as float `Object speed
MoveTarget as integer `Object to be followed
Target as integer `object number which was right clicked, while this object was set to "active"
IsMoving as float `what speed is moving
IsFollowing as float `what speed is following
Dx as float `destination coordinates
Dy as float
Dz as float
endtype
`--- Arrays ---
dim Unit(UnitCount) as units `array of all your controlable objects (units, soldiers, robots or whatever u use in game
`--- Variables ---
global activeID as integer `selected object number
`----------------------------------------------------------------------------------------------------------------------------------
`--- Program start ---
`----------------------------------------------------------------------------------------------------------------------------------
Setup() `program setup
TerrainSetup() `make and set up terrain
MakeBoxes() `make random objects for selection (just for testing, remove later)
MakeSelectionIndicator() `make object to be shown above selected objects
`----------------------------------------------------------------------------------------------------------------------------------
`--- Main Loop ---
`----------------------------------------------------------------------------------------------------------------------------------
do
MoveCamera() `camera movement
if mouseclick()= 1 then activeID= SelectObjects() `left mouse click: select object
SelectionIndicator(activeID) `place indicator over selected object (can load your own object like sword, target sign and etc.)
if mouseclick()= 2 then MakeTarget() `get coordinates
for i=10 to unitcount `if moving to destination coordinates
if unit(i).IsMoving <>0
MoveObject(i)
if abs(unit(i).dx-object position x(i))<1 and abs(unit(i).dz-object position z(i))<1 `if at destination. stop object
unit(i).Ismoving=0
endif
endif
if unit(i).IsFollowing <>0
FollowObject(i)
if abs(unit(i).dx-object position x(Unit(i).MoveTarget))<1 and abs(unit(i).dz-object position z(Unit(i).MoveTarget))<1 `if at destination. stop object
unit(i).IsFollowing=0
unit(i).movetarget=0
endif
endif
next i
Info() `when u encounter problems with new functions added, print variables to see if your functions give right answers, very usefull for debugging
update terrain `apply all the changes to terrain
sync `apply all the changes to screen
loop
`----------------------------------------------------------------------------------------------------------------------------------
`--- Functions and procedures ---
`----------------------------------------------------------------------------------------------------------------------------------
function Setup()
sync on
sync rate 90
autocam off `when new objects are created camera isnt positioned at coordinates of new pbject
set display mode 1024,768,32
set camera range 10,20000 `how far objects will be visible
backdrop on
maximize window `make full screen
randomize timer()
xrotate camera 20 `point camera little down
`hide mouse
endfunction
`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
function TerrainSetup()
set dir "media" ` set the directory to media
` load base and detail texture
load image "texture.bmp", 1
load image "detail.tga", 2
make object terrain 1 ` create the terrain object
set terrain heightmap 1, "map.bmp" ` set the heightmap
set terrain scale 1, 3, 0.1, 3 ` set the scale
set terrain split 1, 16 ` split value by 16 * 16
set terrain tiling 1, 4 ` detail map tiling
set terrain light 1, 1, -0.25, 0, 1, 1, 0.78, 0.5 ` light - xdir, ydir, zdir, red, green, blue, intensity
set terrain texture 1, 1, 2 ` base and detail texture
build terrain 1 ` finally build the terrain
position object 1, 0,0,0 ` position terrain at zero coordinates
sc_setupComplexObject 1,1,2 ` setup collision on the terrain
` load our skybox (this is very low quality, u can find smth better)
load object "skybox.x", 6 `this file has images asociated (1-6 jpg images in media directory). U can change or edit them
set object light 6, 0
set object texture 6, 3, 1
position object 6, 500, 50, 500
scale object 6, 30000, 30000, 30000
set dir ".." ` reset the directory
endfunction
`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
function MoveCamera()
`get keyboard input for movement
if upkey()=1 then move camera 3
if downkey()=1 then move camera -3
if leftkey()=1 then yrotate camera wrapvalue(camera angle y()-4)
if rightkey()=1 then yrotate camera wrapvalue(camera angle y()+4)
`find out the camera height
x#=camera position x()
z#=camera position z()
y#=get terrain ground height(1,x#,z#)+30
`position the camera
position camera x#,y#,z#
endfunction
`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
function SelectObjects()
which= pick object(mousex(),mousey(),10,20) `function to pick object using screen coordinates
endfunction which `return value which object was selected, if 0 then none of objects
`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
function MakeBoxes() `make 10 random boxes
for i= 10 to 20
make object box i,10,10,10
position object i,rnd(200)+100,rnd(100),rnd(200)+100
rotate object i,0,rnd(100),0
color object i,rgb(0,255,0)
Unit(i).Speed =0.5
Unit(i).IsMoving =0.0
Unit(i).IsFollowing =0.0
next i
endfunction
`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
function SelectionIndicator(obj) `indicator handler
if obj=0: `if no objects selected
if object visible(2)=1: `if indicator visible
hide object (2) `make it invisible
endif
else `if some object is selected
if object visible(2)=0: `if indicator is not visible
show object (2) `make it visible
endif
position object 2,object position x (obj),object position y (obj)+10,object position z(obj) `position indicator above your selected object
endif
endfunction
`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
function MakeSelectionIndicator() `make objects for indicators
make object cone 2, 5 `for active object
zrotate object 2, 180
set object 2, 1, 1, 0
color object 2,RGB(0, 255, 0) `green color
make object cone 3, 5 `to mark target-coordinates on map
zrotate object 3, 180
set object 3, 1, 1, 0
color object 3,RGB(255, 255, 0) `yellow color
endfunction
`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
function Info()
text 10,10,"Object selected :"+str$(activeID) `selected, active object
text 10,30,"FPS :" + str$(Screen FPS()) `frames per second (how many times screen refreshes)
`determines how hard program is for pc, fps will go down if pc is too weak
text 10,50,"Left mouse click to select object"
text 10,70,"Right mouse click to place target/target object"
text 10,95, str$(unit(activeid).ismoving)
text 10,130, str$(unit(activeid).movetarget)
text 10,150, str$(unit(activeid).isfollowing)
endfunction
`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
function MakeTarget() `place target indicator if needed (i cant comment this funtion well, because its no my made.
`forum link for it: http://forum.thegamecreators.com/?m=forum_view&t=79830&b=1
obj=SelectObjects() `targeted object number
if obj=0 `if none objects selected
mX = mouseX()
mY = mouseY()
cx# = camera position x()
cy# = camera position y()
cz# = camera position z()
pickState = Pick Object (mX, mY, 1, 1)
If pickState <> 0
Dist# = Get Pick Distance()
Pick Screen mX, mY, Dist#
indX# = cx# + get pick vector x()
indY# = cy# + get pick vector y()
indZ# = cz# + get pick vector z()
Position Object 3, indX#, indY#+3, indZ# `position indicator on terrain
Unit(ActiveID).Dx=indX# `set units destination coordinates
Unit(ActiveID).Dy=indY#
Unit(ActiveID).Dz=indZ#
Unit(ActiveID).IsMoving=0.1 `give starting movement speed
if unit(ActiveID).MoveTarget<>0 then unit(ActiveID).MoveTarget=0
if object visible(3)=0 then show object (3) `handling visibility again
EndIf
else
unit(ActiveID).MoveTarget=obj `set units target value to selected object
Unit(ActiveID).IsFollowing=0.1 `give starting movement speed
if object visible(3)=1 then hide object (3) `handling visibility again
endif
endfunction
`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
function MoveObject(ID)
point object ID, Unit(ID).dx, Unit(ID).dy, Unit(ID).dz
move object ID, Unit(ID).Speed
remstart
Ox#=object position x(ID)
`Oy#=get terrain height (1,Ox#,Oz#)+5
Oz#=object position z(ID)
`position object ID,Ox#,Oy#,Oz#
remend
endfunction
`^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
function FollowObject(ID)
point object ID, object Position X(Unit(ID).Movetarget), object Position Y(Unit(ID).Movetarget),object Position Z(Unit(ID).Movetarget)
move object ID, Unit(ID).Speed
remstart
Ox#=object position x(ID)
Oy#=get terrain height (1,Ox#,Oz#)+5
Oz#=object position z(ID)
position object ID,Ox#,Oy#,Oz#
remend
endfunction
I can add download of full program source+media+.exe if need