GOTO

Blitz3D Forums/Blitz3D Beginners Area/GOTO

panton(Posted 2007) [#1]
Why is goto bad? I need it for in my game. Or is there another way. And why dont work this code:

Text "hello ,press Enter to play"
Goto label1
.label2

;HERE COMES THE GAME

.label1

If KeyHit (28) Then
Goto label2
EndIf


Lordon(Posted 2007) [#2]
Don't understand your question, here's and example on Goto and labels

Print "Press (1) or (2)"

Repeat
If KeyHit(2) Goto option1
If KeyHit(3) Goto option2
Forever

.option1
FlushKeys()
Print "You pressed 1."
WaitKey()
End

.option2
FlushKeys()
Print "You pressed 2."
WaitKey()
End


Stevie G(Posted 2007) [#3]
I never use goto's unless I want to skip a section of code for testing purposes. Using them is sloppy imo and there's always a more elegant alternative.

In your example you don't give the user any time to register 'Enter' before it moves on.

Text "hello ,press Enter to play"
repeat : until keyhit( 28 )

;HERE COMES THE GAME



panton(Posted 2007) [#4]
Ok thx!


Kev(Posted 2007) [#5]
i have not used goto since my sinclair ZX spectrum days, i agree with stevie there sloppy. does blitz even have them :P


GfK(Posted 2007) [#6]
Using them is sloppy imo and there's always a more elegant alternative.
If your 'more elegant alternative' will take you hours of work to solve a problem that a Goto will solve in five seconds, use Goto.

The end user doesn't see your code anyway, so why put yourself out?


Kev(Posted 2007) [#7]
i recall reading back when i first got into coding that over use of goto and gosubs made spaghetti code. so really the odd goto wont make much spaghetti.

my personal taste is not to use them. however each to there own so to say.


Rob Farley(Posted 2007) [#8]
I agree with Gfk here, however, being as anally retentive as myself I will actually waste the hours making it work without a goto.


Naughty Alien(Posted 2007) [#9]
..play dirty man...everything what will make your code work is welcome..including GOTO...thats my suggestion...


big10p(Posted 2007) [#10]
I've had occasion to use GOTO before. If you use it sparingly in a clear, contained way, I don't see what the problem is. If it was really that bad, it wouldn't be part of the language. I admit it's open to terrible abuse - particularly by beginners - though.


Damien Sturdy(Posted 2007) [#11]
Another way?

Function Blah()
;Code here
End Function



Stevie G(Posted 2007) [#12]

If your 'more elegant alternative' will take you hours of work to solve a problem that a Goto will solve in five seconds, use Goto.



I can't think of a single situation where a 'goto' would solve in 5 mins what an alternative would take hours to achieve.

Each to their own though :)


jfk EO-11110(Posted 2007) [#13]
For example when you have a few nested FOR loops, with a conditional EXIT somwhere in the inner loop then it will not exit all loops, but a goto will.

It also acts as some kind of block-remark.

Stay away from wildly jumping across the entire app, of course.

BTW how about RISBASIC ? Reducted instruction set basic. Allowed are only Assembler equivalent commands such as:

PeekBank, PokeBank
If AND OR NOT
GOTO GOSUB RETURN
PLOT to screen (as Poking to the screenbuffer)
labels and a few other low level commands.


A simple FOR loop would then look like this:

PokeInt bank1,0,100
pokeInt bank2,0,0
.my_for_label
PokeInt bank2,0,peekInt(bank2,0)+1
if PeekInt(bank2,0) <= PeekInt(bank1,0) then goto my_for_label


Remember, after all every exe code is based on this structure, no matter if it's c++ or basic.


Fernhout(Posted 2007) [#14]
But to awnser your question. if you look good to your code you will see that the second part of you program is looping forever.
Because you say at the end of the program
GOTO Label1
you program run and come again at the command
GOTO label1
this is a loop forever.
Thats why a lot of people do not want to use the GOTO. Its make your program do things unconditional.
And thats uncontroled.
Do not banned the GOTO because it can be usefull onluy use it wisely.


Buggy(Posted 2007) [#15]
I think darktremor's original example was using it as a "replay" button, which I myself have done on occasion. Nothing wrong with using all of the language.


Fernhout(Posted 2007) [#16]
I see what you mean. There is realy no problem of using GOTO. In the way he like it to use it. Why not. There are always people who thinks there is a better way. But if the solution works its ok to use it.


Trader3564(Posted 2007) [#17]
what would be a more elegant alternative is this:

psuedo code:

switch(gamestate){
case 1:
gamemenu()
case 2:
level()
case 3:
gameover()
case 4:
options()
}

and pack everything into functions.
thats how i work.

the same structure is applied on AI for NPCs