Ending A Loop From Procedure
Author |
Message |
Warsick
|
Posted: Mon Feb 08, 2010 1:06 pm Post subject: Ending A Loop From Procedure |
|
|
End A Loop From A Procedure
I'm trying to call a procedure that will end a loop without having to implement the entire code into my loop.
'Exit' Wont Work
I've tried to put exit in the procedure, it said that it is only available if it is within a loop or for statement. I do understand the problem but I'm trying to find a work around.
Attempted To Insert A Process
I tried to use a processes rather then a procedure in a blind attempt to fork around it but that also failed.
Here is the code:
Turing: |
%End Game%
process endGame
if chars (KEY_ESC) then
exit
end if
end endGame
|
I Am Using Turing 4.1.1
Also the last time I actually worked on Turing was about four years ago, so I'm a bit rusty on the commands. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
chrisbrown
|
Posted: Mon Feb 08, 2010 1:39 pm Post subject: Re: Ending A Loop From Procedure |
|
|
I assume you are trying to do something like this:
Turing: |
procedure p(some args)
[some stuff]
exit when [some_exit_condition]
end p
loop
[some stuff]
p(args)
end loop
|
Depending on where/how you use p, you could try something like:
Turing: |
function f(some args) : boolean
[some stuff]
return [some_exit_condition]
end f
loop
[some stuff]
exit when f(args)
end loop
|
|
|
|
|
|
|
USEC_OFFICER
|
Posted: Mon Feb 08, 2010 8:25 pm Post subject: RE:Ending A Loop From Procedure |
|
|
I believe that you can't do that. The procedure may not always be in a loop the way you have it, which would explain the error. Having a procedure for only three lines of code is a bit of a waste though. |
|
|
|
|
|
TerranceN
|
Posted: Mon Feb 08, 2010 9:54 pm Post subject: Re: Ending A Loop From Procedure |
|
|
There are two different ways you could do this AFAIK:
One, like what methodoxx posted, you could return a boolean indicating whether or not to exit the loop. Working example:
Turing: | function CheckForExit (var keys : array char of boolean) : boolean
% Check for escape
if (keys (KEY_ESC)) then
result true
end if
% Default result
result false
end CheckForExit
% Variable to store window ID
var winID : int := Window.Open ("graphics:500;500,offscreenonly,nobuttonbar,title:Exiting Loop From Inside A Function")
% Variable to store x-position of oval
var x : int := 0
% Variable to hold key states
var keys : array char of boolean
loop
% Gets key states
Input.KeyDown (keys )
% Moves ball
x + = 1
% Clears screen
cls
% Draws oval
Draw.FillOval (x, 250, 10, 10, black)
% flip buffers
View.Update ()
% Delay for 30 fps
Time.DelaySinceLast (33)
% exits loop when function returns true
exit when CheckForExit (keys )
end loop
% Closes window when program exits
Window.Close (winID ) |
The other would be to use a global boolean variable to indicate whether to continue looping, then in the procedure, toggle the variable. Working Example:
Turing: | % Variable to store whether program is running
var isRunning : boolean := true
procedure CheckForExit (var keys : array char of boolean)
% Check for escape
if (keys (KEY_ESC)) then
% Set program to exit
isRunning := false
end if
end CheckForExit
% Variable to store window ID
var winID : int := Window.Open ("graphics:500;500,offscreenonly,nobuttonbar,title:Exiting Loop From Inside A Function")
% Variable to store x-position of oval
var x : int := 0
% Variable to hold key states
var keys : array char of boolean
loop
% Gets key states
Input.KeyDown (keys )
% Moves ball
x + = 1
% Clears screen
cls
% Draws oval
Draw.FillOval (x, 250, 10, 10, black)
% flip buffers
View.Update ()
% Delay for 30 fps
Time.DelaySinceLast (33)
% Check to see if game should exit
CheckForExit (keys )
% exits loop when program is set to not running
exit when not isRunning
end loop
% Closes window when program exits
Window.Close (winID ) |
Hope this helps. |
|
|
|
|
|
copthesaint
|
Posted: Mon Feb 08, 2010 10:31 pm Post subject: Re: Ending A Loop From Procedure |
|
|
Here a better one for you, and I bet fits with your knowledge of turing.
Turing: | procedure quitProgram () % procedure that takes nothing and returns nothing *faster then function
var chars : array char of boolean % local variable called chars, doesnt need to be called chars but what ever.
Input.KeyDown (chars ) %Checks to see if a key is pressed
if chars (KEY_ESC) then %Returns if chars = the escape key, and if so, is returned true.
quit% quits the program, donot pass go, do not collect 200$ just terminates the program.
end if %closes the if statement
end quitProgram % closes the procedure
put "just hurry up and press esc.... 8P" %outputs text to the window.
loop
quitProgram () % calls the procedure
end loop
|
Now really this method of completing the task is basic, but If all you want to do is exit the program then this is perfect for you, dont worry about declairing the variable over and over again, the real thing that slows turings speed is functions, procedures (but not as bad as functions), and anything that involves GUI, and well Accually EVERYTHING, but declairing variables and seting primative values. Lol... |
|
|
|
|
|
DemonWasp
|
Posted: Tue Feb 09, 2010 1:39 am Post subject: RE:Ending A Loop From Procedure |
|
|
@copthesaint:
I can think of nothing to indicate that a procedure taking no values is any faster than a function. Possibly the rare exception if you're passing several dozen parameters, but the difference should only be noticable if you're calling it millions of times a second.
Worse, your version won't allow the program to do any cleanup / finalization tasks, like terminating network connections, closing open file streams, freeing memory (although Turing handles this for you) and the like. |
|
|
|
|
|
copthesaint
|
Posted: Tue Feb 09, 2010 2:12 am Post subject: RE:Ending A Loop From Procedure |
|
|
Hey, I said it was ez, not good lol... I just put it because it because I dont know how much experience this user has with programing...as for leaks, never bothered in turing lol., Your right I read back what I wrote for procedures and functions lol. |
|
|
|
|
|
USEC_OFFICER
|
Posted: Tue Feb 09, 2010 12:54 pm Post subject: RE:Ending A Loop From Procedure |
|
|
Wait, how does putting a procedure that does nothing in a loop end the game? |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Euphoracle
|
Posted: Tue Feb 09, 2010 2:41 pm Post subject: RE:Ending A Loop From Procedure |
|
|
He used `quit` to terminate, which is not the right way to do this. |
|
|
|
|
|
andrew.
|
Posted: Tue Feb 09, 2010 3:10 pm Post subject: Re: RE:Ending A Loop From Procedure |
|
|
copthesaint @ Tue Feb 09, 2010 2:12 am wrote: Hey, I said it was ez, not good lol... I just put it because it because I dont know how much experience this user has with programing...as for leaks, never bothered in turing lol., Your right I read back what I wrote for procedures and functions lol. If he/she doesn't have much experience, then why are you trying to teach them the incorrect way? Teach them the proper way so that learn, otherwise they will not become better and more experienced with this kind of stuff. Also, what was the point of putting everything in a procedure and calling it from a loop? You may as well just copy and paste the stuff into the loop because the procedure does nothing; It doesn't even organize the code a bit. |
|
|
|
|
|
|
|