What does a FUNCTION do?

Blitz3D Forums/Blitz3D Beginners Area/What does a FUNCTION do?

CHALLENGER426(Posted 2008) [#1]
I'm new at this... i add a gun code to my game and it says..."Function can only appear in main program"... how do i fix this, Why? THANKS


GfK(Posted 2008) [#2]
Bit difficult to say without seeing the code that's causing the error.


Perturbatio(Posted 2008) [#3]
In order to assist you fully, you may need to post some sample code, it doesn't necessarily need to be the entire thing, but at least the line the error occurs on and perhaps a few lines above and below.


CHALLENGER426(Posted 2008) [#4]
Here ya go... thanks again



;Welcome
welcome()
Function welcome()
Graphics 1020,740
screen=LoadImage("title.jpg")
DrawImage screen,0,0
While Not KeyDown(1)
If KeyDown(28) Then Return
Wend
End Function
Graphics3D 640,480
HidePointer

Graphics3D 640,480
SetBuffer BackBuffer()


Global info1$="Driver"

Const GRAVITY#=-.01

Const BODY=1,WHEEL=2,SCENE=3,Water=4,CAM=5,BULLET=6,GUN=7
Graphics3D 1024,768
SetBuffer BackBuffer()



;Creating a plane and texture

terr=CreateTexture(32,32,8)
ScaleTexture terr,10,10
SetBuffer TextureBuffer(terr)
Color 0,0,0:Rect 0,0,32,32
Color 0,0,0:Rect 0,0,32,32,False
SetBuffer BackBuffer()




;create cloud planes
tex=LoadTexture( "cloud_2.bmp",3 )
ScaleTexture tex,1000,1000
p=CreatePlane()
EntityTexture p,tex
EntityFX p,1
PositionEntity p,0,70,0
p=CopyEntity( p )
RotateEntity p,0,0,180



;Load terrain
terr=LoadTerrain( "ground.jpg" )
ScaleEntity terr,20,800,20
PositionEntity terr,-20*512,0,-20*512
EntityFX terr,1
EntityType terr,10

;apply textures to terrain
tex1=LoadTexture( "grass.jpg",1 )
ScaleTexture tex1,10,10
tex2=LoadTexture( "muddy.jpg" )
ScaleTexture tex2,TerrainSize(terr),TerrainSize(terr)
EntityTexture terr,tex1,0,0
EntityTexture terr,tex2,0,1

;and ground plane
plane=CreatePlane()
ScaleEntity plane,20,1,20
PositionEntity plane,-20*512,0,-20*512
EntityTexture plane,tex1,0,0
EntityOrder plane,3
EntityFX plane,1
EntityType plane, SCENE


;create water plane
tex=LoadTexture( "water-2_mip.bmp",3 )
ScaleTexture tex,10,10
p=CreatePlane()
EntityTexture p,tex
EntityBlend p,1
EntityAlpha p,.75
PositionEntity p,0,10,0
EntityFX p,1
EntityType p, WATER




terrain=LoadTerrain("ground.jpg")
ScaleEntity terrain,5,100,5
PositionEntity terrain,-500,0,-500

Texture=LoadTexture("grassy.jpg")
EntityTexture terrain,texture

EntityType terrain,SCENE

;plane

plane=LoadMesh("747.x")
ScaleMesh plane,1,1,-1
FlipMesh plane
FitMesh plane,100,100,-50,100,50,100
PositionEntity plane,150,-110,-100
EntityShininess plane,-10
EntityType plane,SCENE
RotateEntity plane, -5,8,0

;debris
a=LoadMesh("wcrate1.3ds")
ScaleMesh a,1,1,-1
FlipMesh a
FitMesh a,5,5,-5,5,5,5
PositionEntity a,300,-2,-30
EntityShininess a,-10
EntityType a,SCENE
RotateEntity a, -5,8,0

a2=LoadMesh("wcrate1.3ds")
ScaleMesh a2,1,1,-1
FlipMesh a2
FitMesh a2,5,5,-5,5,5,5
PositionEntity a2,280,-5,-30
EntityShininess a2,-10
EntityType a2,SCENE
RotateEntity a2, -5,8,0

a3=LoadMesh("wcrate1.3ds")
ScaleMesh a3,1,1,-1
FlipMesh a3
FitMesh a3,5,5,-5,5,5,5
PositionEntity a3,270,0,-20
EntityShininess a3,-10
EntityType a3,SCENE
RotateEntity a3, -5,8,0

a4=LoadMesh("wcrate1.3ds")
ScaleMesh a4,1,1,-1
FlipMesh a4
FitMesh a4,5,5,-5,5,5,5
PositionEntity a4,250,-6,-10
EntityShininess a4,-10
EntityType a4,SCENE
RotateEntity a4, -5,8,0



;tank
car=LoadMesh("warrior.3ds")
ScaleMesh car,1,1,-1
FlipMesh car
FitMesh car,-1.5,-1,-3,3,2,6
PositionEntity car, 0,70,0
EntityShininess car,.1
EntityType car,BODY

Global wheels[4]

cnt=1
For z#=1.5 To -1.5 Step -3
For x#=-1 To 1 Step 2
wheels[cnt]=CreateSphere( 8,car )
EntityColor wheels[cnt],0,0,0
EntityAlpha wheels[cnt],.5
ScaleEntity wheels[cnt],.5,.5,.5
EntityRadius wheels[cnt],.5
PositionEntity wheels[cnt],x,0,z
EntityType wheels[cnt],WHEEL
cnt=cnt+1
Next
Next


;GUN/SHOOTING
For car=Each car
b.bullet=New bullet
b\e=LoadMesh("media\meshes\bullet.b3d",p\gpiv)
b\x=2.5
b\y=-4
b\z=-80
RotateEntity b\e,0,180,0
PositionEntity b\e,b\x,b\y,b\z
EntityType b\e,bul
EntityParent b\e,0
;EmitSound firesnd,p\e
Next
New Function updatefire()
For b.bullet=Each bullet
If b<>Null
MoveEntity b\e,0,0,-80
LinePick EntityX(b\e),EntityX(b\e),EntityZ(b\e),0,0,100
If EntityCollided (b\e,world_col)
EndIf
If PickedEntity()<>0
h.particle=New particle
h\e=LoadSprite("media\bullet_hole.png",2)
PositionEntity h\e,PickedX(),PickedY(),PickedZ()
AlignToVector h\e,PickedNX(),PickedNY(),PickedNZ(),1
SpriteViewMode h\e,2
End If

FreeEntity b\e:Delete b
End If
EndIf
Next
End New Function

New Function mouselook()
For p.player=Each player
; Mouse look
; ----------

; Mouse x and y speed
mxs=MouseXSpeed()
mys=MouseYSpeed()

; Mouse shake (total mouse movement)
mouse_shake#=Abs(((mxs+mys)/2)/1000.0)

; Destination camera angle x and y values
dest_cam_yaw#=dest_cam_yaw#-mxs
dest_cam_pitch#=dest_cam_pitch#+mys

; Current camera angle x and y values
cam_yaw#=cam_yaw+((dest_cam_yaw-cam_yaw)/5)
cam_pitch#=cam_pitch+((dest_cam_pitch-cam_pitch)/5)

RotateEntity p\e,cam_pitch#,cam_yaw#,0
;RotateEntity p\e,mxs,mys,0

; Rest mouse position to centre of screen
MoveMouse 400,300
Next
End Function





;light
light=CreateLight()
TurnEntity light,45,45,0



target=CreatePivot( car )
PositionEntity target,0,5,-12

camera=CreateCamera()
CameraClsColor camera,0,128,255
EntityType camera, CAM
CameraFogColor camera,100,192,255
CameraFogRange camera,1000,3000

speed#=0
x_vel#=0:prev_x#=EntityX( car )
y_vel#=0:prev_y#=EntityY( car )
z_vel#=0:prev_z#=EntityZ( car )





;ENEMIES
Dim ducks(5)
For x=1 To 1
ducks(x)=LoadMesh("fighter.3ds")
PositionEntity ducks(x),x*15,60,90
EntityColor ducks(x),255,255,0
ScaleEntity ducks(x),0.1,0.1,0.1
EntityType ducks(x), type_gallery
EntityRadius ducks(x),3
Next
Dim seahorse(5)
For x=1 To 1
seahorse(x)=LoadMesh("stealth.3ds")
PositionEntity seahorse(x),x*6,40,45
EntityColor seahorse(x),255,102,0
ScaleEntity seahorse(x),0.03,0.03,0.03
EntityType seahorse(x),type_gallery
EntityRadius seahorse(x),3
Next
Dim Donkey(5)
For x=1 To 1
donkey(x)=LoadMesh("solarsail.3ds")
PositionEntity donkey(x),x*5,50,80
EntityColor donkey(x),204,204,204
ScaleEntity donkey(x),.003,.003,.003
EntityType donkey(x),type_gallery
EntityRadius donkey(x),3
Next
Dim Flamingo(5)
For x=1 To 1
flamingo(x)=LoadMesh("fsaucer1.3ds")
PositionEntity flamingo(x),x*6,45,0
EntityColor flamingo(x),0,0,0
ScaleEntity flamingo(x),0.003,0.003,0.003
EntityType flamingo(x),type_gallery
EntityRadius flamingo(x),3
Next
Dim Dolphin(5)
For x=1 To 1
dolphin(x)=LoadMesh("mak_robotic.3ds")
PositionEntity dolphin(x),x*6,45,0
EntityColor dolphin(x),102,153,255
ScaleEntity dolphin(x),0.3,0.3,0.3
EntityType dolphin(x),type_gallery
EntityRadius dolphin(x),3
Next
Dim snake(5)
For x=1 To 1
snake(x)=LoadMesh("fsaucer1.3ds")
PositionEntity snake(x),x*16,60,10
EntityColor snake(x),153,255,102
ScaleEntity snake(x),0.06,0.06,0.06
EntityType snake(x),type_gallery
EntityRadius snake(x),5
Next





;collisions

Collisions BODY,SCENE,2,3
Collisions WHEEL,SCENE,2,3
Collisions BODY, WATER,2,5
Collisions CAM,SCENE,3,2
Collisions WHEEL,WATER,2,5


While Not KeyHit(1)

;align car to wheels
zx#=(EntityX( wheels[2],True )+EntityX( wheels[4],True ))/2
zx=zx-(EntityX( wheels[1],True )+EntityX( wheels[3],True ))/2
zy#=(EntityY( wheels[2],True )+EntityY( wheels[4],True ))/2
zy=zy-(EntityY( wheels[1],True )+EntityY( wheels[3],True ))/2
zz#=(EntityZ( wheels[2],True )+EntityZ( wheels[4],True ))/2
zz=zz-(EntityZ( wheels[1],True )+EntityZ( wheels[3],True ))/2
AlignToVector car,zx,zy,zz,1

zx#=(EntityX( wheels[1],True )+EntityX( wheels[2],True ))/2
zx=zx-(EntityX( wheels[3],True )+EntityX( wheels[4],True ))/2
zy#=(EntityY( wheels[1],True )+EntityY( wheels[2],True ))/2
zy=zy-(EntityY( wheels[3],True )+EntityY( wheels[4],True ))/2
zz#=(EntityZ( wheels[1],True )+EntityZ( wheels[2],True ))/2
zz=zz-(EntityZ( wheels[3],True )+EntityZ( wheels[4],True ))/2
AlignToVector car,zx,zy,zz,3

;calculate car velocities
cx#=EntityX( car ):x_vel=cx-prev_x:prev_x=cx
cy#=EntityY( car ):y_vel=cy-prev_y:prev_y=cy
cz#=EntityZ( car ):z_vel=cz-prev_z:prev_z=cz


cnt=1
For z=1.5 To -1.5 Step -3
For x=-1 To 1 Step 2
; PositionEntity wheels[cnt],0,0,0
; ResetEntity wheels[cnt]
PositionEntity wheels[cnt],x,-1,z
cnt=cnt+1
Next
Next
;Moving the Targets
For a=1 To 1
MoveEntity ducks(a),0.1,0,0
MoveEntity seahorse(a),.2,0,0
MoveEntity donkey(a),0,0,.1
MoveEntity flamingo(a),0.1,0,.2
MoveEntity snake(a),-0.2,0,0
MoveEntity dolphin(a),.1,0,0
Next

;move car

If KeyDown(203) TurnEntity car,0,3,0
If KeyDown(205) TurnEntity car,0,-3,0
If EntityCollided( car,SCENE )
If KeyDown(200)
speed=speed+.02
If speed>.5 speed=.5
Else If KeyDown(208)
speed=speed-.02
If speed<-.5 speed=-.5
Else
speed=speed*.9
EndIf
MoveEntity car,0,0,speed
TranslateEntity car,0,GRAVITY,0
Else
TranslateEntity car,x_vel,y_vel+GRAVITY,z_vel
EndIf
If KeyDown (32) Collisions BODY,SCENE,2,3
If KeyDown (32) Collisions WHEEL,SCENE,2,3

Cls



;get mouse
mxspd#=MouseXSpeed()
myspd#=MouseYSpeed()
MoveMouse 400,300

;turn camera
;note, we change only camera pitch as it's attached to the pivot of the player which
;only changes it's yaw. This for various reasons makes it easy to control.

pitch#=pitch#+myspd*0.1
yaw#=yaw#-mxspd*0.1
RotateEntity camera,pitch,0,0
RotateEntity camera,0,yaw,0


;Help Create Game
If KeyDown(31) HideEntity car
If KeyDown(32) ShowEntity car
; Toggle wireframe enable value between true and false when spacebar is pressed
If KeyHit( 17 )=True Then enable=1-enable
; Enable/disable wireframe rendering
WireFrame enable

;update camera
If speed>=0
dx#=EntityX( target,True )-EntityX( camera )
dy#=EntityY( target,True )-EntityY( camera )
dz#=EntityZ( target,True )-EntityZ( camera )
TranslateEntity camera,dx*.1,dy*.1,dz*.1
EndIf
PointEntity camera,car


;Pausing the game
If KeyDown(25) Then
Cls
pause=LoadImage("image1.jpg")
DrawImage pause,0,0
Flip
WaitMouse
EndIf

UpdateWorld
RenderWorld
Flip
Wend
End


GfK(Posted 2008) [#5]
What's "New Function" and "End New Function" all about?


grable(Posted 2008) [#6]
First of all that is Blitz3D code, thus in the wrong forum ;)

Ok, the one thing i can see causing errors would be the
New Function updatefire()
and
End New Function

There are a few of them around the code so removing those will probably make it compile.
As you can only use New with Types, the correct usage is like your first function (welcome) at the top of the code.

Oh btw, please use the forum codes. There is even a link to them above the reply textbox!