“Function can only appear in the main loop. " Ques

Blitz3D Forums/Blitz3D Beginners Area/“Function can only appear in the main loop. " Ques

Galdy(Posted 2011) [#1]
I’m having a problem where i get the error “Function can only appear in the main loop. "
I suppose it is kind of self explanatory, but I was wondering...

During the Main loop one of the mouse checks will gosub to the label “.SavePoject” which has
it’s own small loop for checking mouse clicks in the file operations GUI. However, I can’t call
any functions from it as a result. So, can the secondary loop go inside the main loop and be able to call functions?


Yasha(Posted 2011) [#2]
??

That's really weird. Blitz3D isn't supposed to have any concept of a main loop - it's a common game-design idiom but there's inbuilt requirement for it (heck, how would it even recognise it?).

Can you post some code that generates that error?


Galdy(Posted 2011) [#3]
I assume by loop it is talking about the while-wend loop that i'm using.
Din't you typicaly set up a main loop that cycles?

Last edited 2011


Yasha(Posted 2011) [#4]
Din't you typicaly set up a main loop that cycles?


Yes but there are plenty of other ways to design a program (not all programs are games, not all programs have just one event loop, not all loops are While/Wend), so this is an odd restriction for a compiler to make, and in any case the compiler has no way to identify which part that main loop would be.

At a guess I would say look to your use of Gosub ... but really that's just a guess without seeing a code snippet.


Galdy(Posted 2011) [#5]
Everything was functioning fine until i added the "The File Operations section" section of code about half way down.
I tried a small snippet of code to simulate what my larger code was trying to do and it worked so i'm not sure what i'm doing wrong to get the "Function can only appear in the main loop" error. It points to the "Function SaveFile()" at the bottom when it does.





;While for the main Loop
While Not KeyDown(1)

If MouseX()>20 And MouseX()<980 And MouseY()>20 And MouseY()<980 And Tool=7 Then ;Keeps the delete curser drawn where the mouse pointer is.
DrawMap()
EndIf


;***********************************************************************************
;This section produces tool tip floaters
;***********************************************************************************

;Save-------------------------------------------------------------------------------------
If MouseX()>1013 And MouseX()<1044 And MouseY()>823 And MouseY()<854 And HoverStatus<>1 Then
HoverStatus=1
DrawMap()
EndIf
If MouseX()<1014 Or MouseX()>1043 Or MouseY()<824 Or MouseY()>853 And HoverStatus=1 Then
HoverStatus=0
DrawMap()
EndIf

;NewProject--------------------------------------------------------------------------------
If MouseX()>1054 And MouseX()<1085 And MouseY()>823 And MouseY()<854 And HoverStatus=0 Then
HoverStatus=2
DrawMap()
EndIf
If MouseX()<1055 Or MouseX()>1084 Or MouseY()<824 Or MouseY()>853 And HoverStatus=2 Then
HoverStatus=0
DrawMap()
EndIf

;Delete----------------------------------------
If MouseX()>999 And MouseX()<1030 And MouseY()>771 And MouseY()<791 And HoverStatus=0 Then
HoverStatus=3
DrawMap()
EndIf
If MouseX()<100 Or MouseX()>1029 Or MouseY()<772 Or MouseY()>790 And HoverStatus=3 Then
HoverStatus=0
DrawMap()
EndIf

;*****************************************************************************************************
;Selects and assigns a number to the "Tool" variable.
;*****************************************************************************************************
.CheckMenu
If MouseDown(1)=0 Then
ClickStatus=0
EndIf

If MouseDown(1)=1 And ClickStatus=0 And MouseX()>1074 And MouseX()<1095 And MouseY()>96 And MouseY()<117 Then
Tool=1 ;Wall
ClickStatus=1
PlaySound Click
DrawMap()
EndIf
If MouseDown(1)=1 And ClickStatus=0 And MouseX()>1074 And MouseX()<1095 And MouseY()>159 And MouseY()<178 Then
Tool=2 ;Door Unlocked
ClickStatus=1
PlaySound Click
DrawMap()
EndIf
If MouseDown(1)=1 And ClickStatus=0 And MouseX()>1074 And MouseX()<1095 And MouseY()>223 And MouseY()<244 Then
Tool=3 ;Door Locked
ClickStatus=1
PlaySound Click
DrawMap()
EndIf
If MouseDown(1)=1 And ClickStatus=0 And MouseX()>1074 And MouseX()<1095 And MouseY()>287 And MouseY()<308 Then
Tool=4 ;Special
ClickStatus=1
PlaySound Click
DrawMap()
EndIf
If MouseDown(1)=1 And ClickStatus=0 And MouseX()>1067 And MouseX()<1107 And MouseY()>340 And MouseY()<381 Then
Tool=5 ;NPC
ClickStatus=1
PlaySound Click
DrawMap()
EndIf
If MouseDown(1)=1 And ClickStatus=0 And MouseX()>1067 And MouseX()<1107 And MouseY()>407 And MouseY()<448 Then
Tool=6 ;Chest
ClickStatus=1
PlaySound Click
DrawMap()
EndIf
If MouseDown(1)=1 And ClickStatus=0 And MouseX()>999 And MouseX()<1030 And MouseY()>771 And MouseY()<791 Then
Tool=7 ;Delete---Don't need to gosub to DrawMap at this point since we don't need a check mark
ClickStatus=1
PlaySound Click
EndIf
If MouseDown(1)=1 And ClickStatus=0 And MouseX()>1013 And MouseX()<1044 And MouseY()>823 And MouseY()<854 Then
Tool=8
ClickStatus=1
PlaySound Click
Gosub SaveProject
EndIf





;*********************************************************************************************************
;Checks work area (the grid) of the screen for activity.
;*********************************************************************************************************

.CheckWorkArea
If MouseDown(1)=0 Then
ClickStatus=0
EndIf

If MouseDown(1)=1 And ClickStatus=0 And MouseX()>20 And MouseX()<980 And MouseY()>20 And MouseY()<980 Then
Select Tool
Case 1
GetCoordinates()
RecordData()
DrawMap()
Case 2
GetCoordinates()
RecordData()
DrawMap()
Case 3
GetCoordinates()
RecordData()
DrawMap()
Case 4
GetCoordinates()
RecordData()
DrawMap()
Case 5
GetCoordinates()
RecordData()
DrawMap()
Case 6
GetCoordinates()
RecordData()
DrawMap()
Case 7
GetCoordinates()
RecordData()
DrawMap()

End Select
EndIf


Wend ;Wend for the main Loop
End





;****************************************************************************************************
;After it is determined that the work area (grid) was clicked on with the mouse via ".CheckWorkArea,"
;the following Function call determines what element of the array LevelOne(50,50,2) was clicked on.
;****************************************************************************************************

Function GetCoordinates()
CurrentMouseX#=Float#(MouseX())/20
CurrentMouseY#=Float#(MouseY())/20
CurrentMouseX=Ceil#(CurrentMouseX)
CurrentMouseY=Ceil#(CurrentMouseY)
End Function

;****************************************************************************************************
;The following Function determines if and what element is written into the array LevelOne(50,50,2).
;****************************************************************************************************

Function RecordData()
If Tool=1 Then
LevelOne(CurrentMouseX,CurrentMouseY,1)=1
EndIf
;---------------------------------------------------------------------------------------
If CurrentMouseX-1>0 And CurrentMouseY-1>0 And CurrentMouseX+1<51 And CurrentMouseY+1<51 Then
If Tool=2 And LevelOne(CurrentMouseX,CurrentMouseY,1)>0 Or LevelOne(CurrentMouseX+1,CurrentMouseY,1)=1 And LevelOne(CurrentMouseX-1,CurrentMouseY,1)=1 And LevelOne(CurrentMouseX,CurrentMouseY+1,1)=1 And LevelOne(CurrentMouseX,CurrentMouseY-1,1)=1 Then
;Nothing is Written to the array
ElseIf Tool=2 And LevelOne(CurrentMouseX+1,CurrentMouseY,1)=1 And LevelOne(CurrentMouseX-1,CurrentMouseY,1)=1 And LevelOne(CurrentMouseX,CurrentMouseY+1,1)<>1 And LevelOne(CurrentMouseX,CurrentMouseY-1,1)<>1 Then
LevelOne(CurrentMouseX,CurrentMouseY,1)=2 ;Writes a east/west unlocked door to the array
LevelOne(CurrentMouseX,CurrentMouseY,2)=0 ;Door is unlocked
ElseIf Tool=2 And LevelOne(CurrentMouseX,CurrentMouseY+1,1)=1 And LevelOne(CurrentMouseX,CurrentMouseY-1,1)=1 And LevelOne(CurrentMouseX+1,CurrentMouseY,1)<>1 And LevelOne(CurrentMouseX-1,CurrentMouseY,1)<>1 Then
LevelOne(CurrentMouseX,CurrentMouseY,1)=3 ;Writes a North/South door to the array
LevelOne(CurrentMouseX,CurrentMouseY,2)=0 ;Door is unlocked
EndIf
EndIf
;_____________________________________________________________________________________
If CurrentMouseX-1>0 And CurrentMouseY-1>0 And CurrentMouseX+1<51 And CurrentMouseY+1<51 Then
If Tool=3 And LevelOne(CurrentMouseX,CurrentMouseY,1)>0 Or LevelOne(CurrentMouseX+1,CurrentMouseY,1)=1 And LevelOne(CurrentMouseX-1,CurrentMouseY,1)=1 And LevelOne(CurrentMouseX,CurrentMouseY+1,1)=1 And LevelOne(CurrentMouseX,CurrentMouseY-1,1)=1 Then
;Nothing is Written to the array
ElseIf Tool=3 And LevelOne(CurrentMouseX+1,CurrentMouseY,1)=1 And LevelOne(CurrentMouseX-1,CurrentMouseY,1)=1 And LevelOne(CurrentMouseX,CurrentMouseY+1,1)<>1 And LevelOne(CurrentMouseX,CurrentMouseY-1,1)<>1 Then
LevelOne(CurrentMouseX,CurrentMouseY,1)=2 ;Writes a east/west door to the array
LevelOne(CurrentMouseX,CurrentMouseY,2)=1; Writes that the door is locked (1)
ElseIf Tool=3 And LevelOne(CurrentMouseX,CurrentMouseY+1,1)=1 And LevelOne(CurrentMouseX,CurrentMouseY-1,1)=1 And LevelOne(CurrentMouseX+1,CurrentMouseY,1)<>1 And LevelOne(CurrentMouseX-1,CurrentMouseY,1)<>1 Then
LevelOne(CurrentMouseX,CurrentMouseY,1)=3;Writes a North/South door to the array
LevelOne(CurrentMouseX,CurrentMouseY,2)=1; Writes that the door is locked (1)
EndIf
EndIf

;Erases the array location
If Tool=7 Then
LevelOne(CurrentMouseX,CurrentMouseY,1)=0
LevelOne(CurrentMouseX,CurrentMouseY,2)=0
EndIf
End Function





;*********************************************************************
;DrawMap Draws everything in the correct z order.
;*********************************************************************

Function DrawMap()
;Draws User interface and grid
SetBuffer BackBuffer()
DrawImage GUI,0,0
Color 192,192,192
a=0:x=0:y=0
For a=1 To 51
Line 0,y,1000,y
y=y+20
Line x,0,x,1000
x=x+20
Next



;Draws CheckMarks by Menu selections
If Tool=1 Then
DrawImage CheckMark,1017,76
EndIf
If Tool=2 Then
DrawImage CheckMark,1017,138
EndIf
If Tool=3 Then
DrawImage CheckMark,1017,200
EndIf
If Tool=4 Then
DrawImage CheckMark,1017,266
EndIf
If Tool=5 Then
DrawImage CheckMark,1017,330
EndIf
If Tool=6 Then
DrawImage CheckMark,1017,398
EndIf

;Draws the floating Toll tips
If MouseX()>1013 And MouseX()<1044 And MouseY()>823 And MouseY()<854 And HoverStatus=1 Then
DrawImage Save,1007,805
EndIf
If MouseX()>1054 And MouseX()<1085 And MouseY()>823 And MouseY()<854 And HoverStatus=2 Then
DrawImage NewProject,1034,805
EndIf
If MouseX()>999 And MouseX()<1030 And MouseY()>771 And MouseY()<791 And HoverStatus=3 Then
DrawImage Delete1,1008,753
EndIf



;Translates the values in the array LevelOne(50,50,2) into graphic elements,i.e., Walls,doors, etc.
a=0:x=0:y=0:b=0:
For yy=1 To 50 ;y coordinates loop
For xx=1 To 50 ;x coordinates loop
If LevelOne(xx,yy,1)=1 Then
DrawImage Wall,x,y
EndIf
;East-West doors
If LevelOne(xx,yy,1)=2 And LevelOne(xx,yy,2)=0 Then
DrawImage DoorEW,x,y
EndIf
If LevelOne(xx,yy,1)=2 And LevelOne(xx,yy,2)=1 Then
DrawImage DoorLockedEW,x,y
EndIf
;North-south doors
If LevelOne(xx,yy,1)=3 And LevelOne(xx,yy,2)=0 Then
DrawImage DoorNS,x,y
EndIf
If LevelOne(xx,yy,1)=3 And LevelOne(xx,yy,2)=1 Then
DrawImage DoorLockedNS,x,y
EndIf
x=x+20
Next
x=0
y=y+20
Next

If Tool=7 And MouseX()>20 And MouseX()<980 And MouseY()>20 And MouseY()<980 Then
DrawImage DeleteCurser,MouseX(),MouseY()
EndIf

;If Tool=8 Then
;Cls
;DrawImage FileOpGUI,0,0
;End If

Flip
End Function




;****************************************************************************************************
;The File Operations section
;****************************************************************************************************


.SaveProject
Cls
DrawFileOpGUI()
Leave=0

While Leave=0 ; As long as Exit has not been clicked
If MouseDown(1)=0 Then
ClickStatus=0
EndIf

;NewProject
If MouseDown(1)=1 And ClickStatus=0 And MouseX()>73 And MouseX()<137 And MouseY()>610 And MouseY()<713 Then
PlaySound Click
ClickStatus=1
Gosub NewProject
EndIf

;Save
If MouseDown(1)=1 And ClickStatus=0 And MouseX()>73 And MouseX()<139 And MouseY()>753 And MouseY()<776 Then
PlaySound Click
ClickStatus=1
Gosub Save
EndIf

;Load
If MouseDown(1)=1 And ClickStatus=0 And MouseX()>73 And MouseX()<137 And MouseY()>818 And MouseY()<841 Then
PlaySound Click
ClickStatus=1
Gosube Load
EndIf

;DeleteFiles
If MouseDown(1)=1 And ClickStatus=0 And MouseX()>73 And MouseX()<224 And MouseY()>885 And MouseY()<907 Then
PlaySound Click
ClickStatus=1
Gosub DeleteFiles
EndIf

;SaveAs
If MouseDown(1)=1 And ClickStatus=0 And MouseX()>231 And MouseX()<336 And MouseY()>753 And MouseY()<776 Then
PlaySound Click
ClickStatus=1
Gosub SaveAs
EndIf

;Exit
If MouseDown(1)=1 And ClickStatus=0 And MouseX()>73 And MouseX()<133 And MouseY()>847 And MouseY()<972 Then
PlaySound Click
ClickStatus=1
Leave=1
EndIf
Wend ;Main Loop
Return




;***********************************************************************************
;Draws the screen,sets drawing to the backbuffer and draws the GUI.
;***********************************************************************************

Function DrawFileOpGUI()
SetBuffer BackBuffer()
DrawImage FileOpGUI,0,0
Flip
End Function

;*************************************************************************************
;This Calls Up the directory of projects
;*************************************************************************************
;

Function ListFiles()
Cls
myDir=ReadDir(folder$)

Repeat
; Assign the next entry in the folder to file$
file$=NextFile$(myDir)

; If there isn't another one, let's exit this loop
If file$="" Then Exit

; Use FileType to determine if it is a folder (value 2) or a file and print results
If FileType("C:\DungeonCrawlGame\Map editor\"+file$) = 2 Then
Print "Folder:" + file$
Else
Print "File:" + file$
End If
Forever

;Properly close the open folder
CloseDir myDir

; We're done!
Print "Done listing files!"
End Function

;****************************************************************************************
;Creates a new Project.
;****************************************************************************************

.NewProject
SetBuffer BackBuffer()
Levels=0
Condition=0
Cls
ListFiles()
DrawFileOpGUI()
Text 454,665,"Enter name of Project"
Locate 454,680
ProjectFile$=Input()
If ProjectFile$="" Then
Text 454,695,"Not a valid Name"
Condition=0
Else
Condition=1
EndIf
If Condition=1 Then
Text 454,695,"How many Levels? Maximum of 20"
Locate 454,710
Levels=Input() ;Make Levels Global
If Levels>0 And Levels<21 Then
Dim Dungeon(50,50,20,Levels)
Else
Text 454,725, "Not a valid number of levels. Select NEW again"
EndIf
EndIf
Return


;****************************************************************************************
;Saves the current Project if there is a current one underway.
;****************************************************************************************


.Save
Cls
ListFiles()
DrawFileOpGUI()
If ProjectFile$<>"" Then
SaveFile()
Else
Text 454,665,"You need to create a NEW project"
EndIf
Return

;**************************************************************************************
;If you want to save the current project under a new name
;**************************************************************************************

.SaveAs
Cls
ListFiles()
DrawFileOpGUI()
If ProjectFile$<>"" Then
Text 454,665, "Enter new file mame."
Locate 454,680
NewProjectFiles$=Input()
If NewProjectFile$="" Then
Text 454,695, "Not a valid name.Try again."
Else
ProjectFile$=NewProjectFile$
SaveFile()
EndIf
Return



;*************************************************************************************
;Loads in a Previously saved project.
;*************************************************************************************

.Load
Cls
ListFiles()
DrawFileOpGUI()
Text 454,665, "Enter Project Name"
ProjectFile$=Input()

Infile=ReadFile("C:\DungeonCrawlGame\Map editor\MyDungeons\"+ProjectFile$)
Levels=ReadInt(InFile)
Dim Dungeon(50,50,20,Levels)
For TotalLevels=1 To Levels
For y=1 To 50
For x=1 To 50
Dungeon(x,y,1,TotalLevels)=ReadInt(InFile)
Dungeon(x,y,2,TotalLevels)=ReadInt(InFile)
Dungeon(x,y,3,TotalLevels)=ReadInt(InFile)
Dungeon(x,y,4,TotalLevels)=ReadInt(InFile)
Dungeon(x,y,5,TotalLevels)=ReadInt(InFile)
Dungeon(x,y,6,TotalLevels)=ReadInt(InFile)
Dungeon(x,y,7,TotalLevels)=ReadInt(InFile)
Dungeon(x,y,8,TotalLevels)=ReadInt(InFile)
Dungeon(x,y,9,TotalLevels)=ReadInt(InFile)
Dungeon(x,y,10,TotalLevels)=ReadInt(InFile)
Dungeon(x,y,11,TotalLevels)=ReadInt(InFile)
Dungeon(x,y,12,TotalLevels)=ReadInt(InFile)
Dungeon(x,y,13,TotalLevels)=ReadInt(InFile)
Dungeon(x,y,14,TotalLevels)=ReadInt(InFile)
Dungeon(x,y,15,TotalLevels)=ReadInt(InFile)
Dungeon(x,y,16,TotalLevels)=ReadInt(InFile)
Dungeon(x,y,17,TotalLevels)=ReadInt(InFile)
Dungeon(x,y,18,TotalLevels)=ReadInt(InFile)
Dungeon(x,y,19,TotalLevels)=ReadInt(InFile)
Dungeon(x,y,20,TotalLevels)=ReadInt(InFile)
Next
Next
CloseFile(Infile)
Return

;*****************************************************************************
;This is the function that is called from the labels ".Save" and ".SaveAs"
;*****************************************************************************

Function SaveFile()
FileOut=WriteFile("C:\DungeonCrawlGame\Map editor\MyDungeons\"+ProjectFile$)
If FileOut=0 Then
RuntimeError "Could not open file for writing."
EndIf
Dungeon(0,0,0,0)=Levels
WriteInt(FileOut,Dungeon(0,0,0,0)) ;Store the number of levels here.
For TotalLevels=1 To Levels
For y=1 To 50
For x=1 To 50
WriteInt(FileOut,Dungeon(x,y,1,TotalLevels))
WriteInt(FileOut,Dungeon(x,y,2,TotalLevels))
WriteInt(FileOut,Dungeon(x,y,3,TotalLevels))
WriteInt(FileOut,Dungeon(x,y,4,TotalLevels))
WriteInt(FileOut,Dungeon(x,y,5,TotalLevels))
WriteInt(FileOut,Dungeon(x,y,6,TotalLevels))
WriteInt(FileOut,Dungeon(x,y,7,TotalLevels))
WriteInt(FileOut,Dungeon(x,y,8,TotalLevels))
WriteInt(FileOut,Dungeon(x,y,9,TotalLevels))
WriteInt(FileOut,Dungeon(x,y,10,TotalLevels))
WriteInt(FileOut,Dungeon(x,y,11,TotalLevels))
WriteInt(FileOut,Dungeon(x,y,12,TotalLevels))
WriteInt(FileOut,Dungeon(x,y,13,TotalLevels))
WriteInt(FileOut,Dungeon(x,y,14,TotalLevels))
WriteInt(FileOut,Dungeon(x,y,15,TotalLevels))
WriteInt(FileOut,Dungeon(x,y,16,TotalLevels))
WriteInt(FileOut,Dungeon(x,y,17,TotalLevels))
WriteInt(FileOut,Dungeon(x,y,18,TotalLevels))
WriteInt(FileOut,Dungeon(x,y,19,TotalLevels))
WriteInt(FileOut,Dungeon(x,y,20,TotalLevels))
Next
Next
Next
CloseFile(FileOut)
Text 454,665, "Finished saving."+ProjectFile$
End Function


Charrua(Posted 2011) [#6]
i never get an error like that, but it is likely:

const can only apperar in main program (same for global), but obviously isn't related with a Main Loop!

probably Gosub is sending the program control to "don't know where" and there is something that doesn't supose to be there...

please: post some code or the exact error message

Juan


Galdy(Posted 2011) [#7]
The problem starts after it gosubs from the Section that Selects and assigns a number to the "Tool" variable.

This line..


If MouseDown(1)=1 And ClickStatus=0 And MouseX()>1013 And MouseX()<1044 And MouseY()>823 And MouseY()<854 Then
Tool=8
ClickStatus=1
PlaySound Click
Gosub SaveProject
EndIf


Galdy(Posted 2011) [#8]
hmm, I should check to see if i missed a global. That could very well be it.


Charrua(Posted 2011) [#9]
you are getting a:

Function Can only appear in main program:

you can't start the definition o a new functions inside one, you must enclose a function with:

Function Bla()
..Sentences of function Bla
End Function

the following gives you an error:

Function Bla()
Function BlaBla()
End Function


so is a problem of structured programing... (a job for super Yasha!!lol)

Juan


Galdy(Posted 2011) [#10]
I need to correct number of things and then i'll post the code again. There were some variabe changes i made that need to carried through out the code.


Warner(Posted 2011) [#11]
You get that error when doing this:
For i = 1 to 10

	Function Test2()
	End Function
	
Next

And the error reads: "Function can only appear in main program".
It seems that in the part labeled '.load', there is a Next missing (right above CloseFile(InFile))

Last edited 2011


Yasha(Posted 2011) [#12]
Well I can see one possible error: the very last function "SaveFile" is defined before one of the preceding loops is closed (the loop "For TotalLevels=1 To Levels" is never closed with a "Next".

However, the code posted above seems to be missing some bits (one of the arrays is never defined so it doesn't run?) so I could be wrong.

If you're wondering, Blitz3D does allow some really ugly shortcuts - you don't need to close loops or If blocks within "superior" structures like functions, because since functions can't nest, the compiler knows the block must have closed. I guess you can do the same with the end of a program file, and the function defined at the very end therefore falls within what should be the scope of the block.

This all hinges on - is Charrua's guess right, that it's actually a different error message about main program, not main loop? Because they're quite opposite things - when it says main program, it means the very outermost scope, outside all functions and loops. Functions can only be defined in the outermost level in B3D - no nesting of any kind permitted.

In yet another case, I would recommend you consider IDEal editor. It has auto-indentation support (indents and dedents correctly without you needing to tab), which makes this sort of thing immediately obvious.

(Also, why are you using both Gosub and real functions?)


Galdy(Posted 2011) [#13]
I need to correct a number of mistakes and repost if I'm still having the same problem. The actual error message was "Function can only appear in the main program" not "loop." Sorry. I'll check that iDEal Yash. Thanks.


Who was John Galt?(Posted 2011) [#14]
Functions declarations should not be in the middle of executing code.

Stick all your function definitions at the top or bottom of the code, not inside a loop or any other logic.


Charrua(Posted 2011) [#15]
hi
when posting code, is a good practice to use the code, /code or codebox to enclose in a small window lot of lines, shows better and makes the thread more readable, every time you write a post, there is a (forum codes) link avobe the text area, read it, its very helpful

(the Warners post uses Code ... /Code markers

and it look diferent. isn' it? )

regards

Juan

Last edited 2011


Galdy(Posted 2011) [#16]
Wow! The IDEal is so much better to work with. Charrua, so when posting the code put / before and after the code I take it?


Charrua(Posted 2011) [#17]
you have to write the word "code" enclosed with square brackets!

the first is a marker to signal the begining of code (that folows)
[ code ]

here you code

[ / code ]
and with the slash signals the end

i put spaces inbetween so they dont work in this post this forum codes are hidden by the software that manages our posts, are like commands

for a long picece of code, [codebox] is better

when doing a post, use "Preview" before POST.

and IDEAL is a very handy tool

Juan

Last edited 2011


Rob the Great(Posted 2011) [#18]
Put simply, here's how the error comes about, the wrong way to code this:
Graphics 800,600,0,2

SetBuffer BackBuffer()

While Not KeyDown(1)
	
	DoSomething()
	
	Function DoSomething()
		x = x + 1
	End Function
	
	Cls
	Text 0,0,x
	Flip
	
Wend

Here's the right way:
Graphics 800,600,0,2

SetBuffer BackBuffer()

While Not KeyDown(1)
	
	DoSomething()
	
	Cls
	Text 0,0,x
	Flip
	
Wend

Function DoSomething()
   x = x + 1
End Function

Functions cannot be defined inside of any loop, which includes While/Wend, Repeat/Until/Forever, and For/Next. Functions cannot be defined inside of another function definition, nor can they be defined inside any If statements or Type definitions. Basically, the Function command can only appear by itself, not inside anything else. You can call functions from anywhere, but you can only define them outside of any loop or subroutine.

I learned this the hard way by trying to define a function inside of the main loop, very similar to the example above. After I figured that out, I later couldn't figure out why this message popped up again even though all functions were where they need to be. Come to find out, it was because I forgot to end an If statement which was directly in front of the functions.

In this situation, Warner and Yasha are right about why this would be happening. Without the last Next command, the next thing to appear is the Function command. Take a look at this example:
For x = 0 to 10
   For y = 0 to 10
      For z = 0 to 10
         ;Do some stuff here
      Next
   Next

;There should be another Next command here. Without it, you're still in the x = 0 to 10 Loop.

Function Whatever()

   ;As soon as Blitz sees the line above, it's going to throw out that error because you are still in the loop, and you can't do that.

End Function