Author Topic: Goto routines . . .  (Read 5992 times)

0 Members and 1 Guest are viewing this topic.

Offline DrewPee

  • I Toast Therefore I am
  • Pentium
  • *****
  • Posts: 563
  • Karma: 25
  • Eat Cheese - It's good for you!
    • View Profile
    • Retro Computer Museum
Goto routines . . .
« on: June 13, 2009 »
Hello
I am in the process of writing a very basic game to test some drawing routines I am messing about with . . . I am stuck with re-starting a game once you die.
Do I need to use something like a goto command or is there something else I can use in Freebasic?

When I try to use goto I keep on getting this error

94: Branching to other functions or to module-level, to label: strt

Thanks in anticipation

DrewPee

DrewPee
aka Falcon of The Lost Boyz (Amiga)
Ex-Amiga Coder and Graphic Designer
Administrator of > www.retrocomputermuseum.co.uk

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17414
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: Goto routines . . .
« Reply #1 on: June 13, 2009 »
There's no need to use commands like Goto nowadays Drewpee.

You could structure your game code something like this;

Code: [Select]
QUIT  = 0
GAME = 0

MAIN LOOP (LOOPS UNTIL QUIT=1)
    IF QUIT = 0 THEN HANDLE TITLE SCREEN SUB
    IF GAME=1 THEN HANDLE GAME SUB
END OF MAIN LOOP

END PROGRAM


HANDLE GAME SUB
START OF LOOP UNTIL GAME=0
   CALL GAME SUBS
   UPDATE SCREEN
   IF DEAD THEN GAME=0
END OF LOOP UNTIL GAME=0
END OF HANDLE GAME SUB



HANDLE TITLE SCREEN SUB
START OF LOOP UNTIL GAME=1
   CALL TITLE SCREEN SUBS
   UPDATE SCREEN
   IF START BUTTON PRESSED THEN GAME=1
   IF QUIT BUTTON PRESSED THEN QUIT=1
END OF LOOP UNTIL GAME=1 OR QUIT=1
END OF HANDLE TITLE SCREEN SUB

Shockwave ^ Codigos
Challenge Trophies Won:

Offline DrewPee

  • I Toast Therefore I am
  • Pentium
  • *****
  • Posts: 563
  • Karma: 25
  • Eat Cheese - It's good for you!
    • View Profile
    • Retro Computer Museum
Re: Goto routines . . .
« Reply #2 on: June 13, 2009 »
Shockwave, thanks (as always for your help!),

code removed for now - due to odd problems with some of it - I will re-post at a later date ;)

DrewPee
« Last Edit: June 13, 2009 by DrewPee »
DrewPee
aka Falcon of The Lost Boyz (Amiga)
Ex-Amiga Coder and Graphic Designer
Administrator of > www.retrocomputermuseum.co.uk

Offline DrewPee

  • I Toast Therefore I am
  • Pentium
  • *****
  • Posts: 563
  • Karma: 25
  • Eat Cheese - It's good for you!
    • View Profile
    • Retro Computer Museum
Re: Goto routines . . .
« Reply #3 on: June 13, 2009 »
Meant to say - this is still early code so there are bits in there that are not needed etc. ;)
« Last Edit: June 13, 2009 by DrewPee »
DrewPee
aka Falcon of The Lost Boyz (Amiga)
Ex-Amiga Coder and Graphic Designer
Administrator of > www.retrocomputermuseum.co.uk

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17414
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: Goto routines . . .
« Reply #4 on: June 13, 2009 »
You need to understand what happens to the stack.

You branch to a sub and the memory address is stored
when the end sub is encountered, you go back to that last address.

Using goto the way you have in a sub potentially could cause a stack overflow which is why it's terrible programming practice and why freebasic wont let you do it.

The technique I posted is very simple to impliment, why not have a try yourself and post back if you run into problems.

Seriously, forget about goto, there is no need to use it... ever.
Shockwave ^ Codigos
Challenge Trophies Won:

Offline DrewPee

  • I Toast Therefore I am
  • Pentium
  • *****
  • Posts: 563
  • Karma: 25
  • Eat Cheese - It's good for you!
    • View Profile
    • Retro Computer Museum
Re: Goto routines . . .
« Reply #5 on: June 13, 2009 »
Thanks Shockwave I will go and try it again.
DrewPee
aka Falcon of The Lost Boyz (Amiga)
Ex-Amiga Coder and Graphic Designer
Administrator of > www.retrocomputermuseum.co.uk

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17414
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: Goto routines . . .
« Reply #6 on: June 13, 2009 »
Good luck mate
Shockwave ^ Codigos
Challenge Trophies Won:

Offline DrewPee

  • I Toast Therefore I am
  • Pentium
  • *****
  • Posts: 563
  • Karma: 25
  • Eat Cheese - It's good for you!
    • View Profile
    • Retro Computer Museum
Re: Goto routines . . .
« Reply #7 on: June 13, 2009 »
I have figured it out a little more and this is where I am at the moment - still early days but playable now! ;)

@Shockwave - thanks again for your help!

btw - the highest score so far is 2392, not by me I might add, by one of our members on RCM!

Drew
DrewPee
aka Falcon of The Lost Boyz (Amiga)
Ex-Amiga Coder and Graphic Designer
Administrator of > www.retrocomputermuseum.co.uk

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17414
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: Goto routines . . .
« Reply #8 on: June 13, 2009 »
It looks pretty cool indeed :)

The only flaw I can see is the method you used to detect your keypresses causes a delay in movement...

Use this method instead..

IF GETASYNCKEYSTATE( VK_UP ) Then do stuff.

:)

Shockwave ^ Codigos
Challenge Trophies Won:

Offline Hotshot

  • DBF Aficionado
  • ******
  • Posts: 2114
  • Karma: 91
    • View Profile
Re: Goto routines . . .
« Reply #9 on: June 14, 2009 »
very good.....Original game

GOTO is horrible but have you thought about using Select case?

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17414
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: Goto routines . . .
« Reply #10 on: June 14, 2009 »
Select Case will not help here Hotshot, you'd use Selest Case the same as switch or if and then.

He just needs to have some controling subs and a containing loop.
Shockwave ^ Codigos
Challenge Trophies Won:

Offline DrewPee

  • I Toast Therefore I am
  • Pentium
  • *****
  • Posts: 563
  • Karma: 25
  • Eat Cheese - It's good for you!
    • View Profile
    • Retro Computer Museum
Re: Goto routines . . .
« Reply #11 on: June 14, 2009 »
I am still 'cheating' a little - but it does work much better now . . . I have added some music, difficulty levels etc.

.exe attached
DrewPee
aka Falcon of The Lost Boyz (Amiga)
Ex-Amiga Coder and Graphic Designer
Administrator of > www.retrocomputermuseum.co.uk

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17414
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: Goto routines . . .
« Reply #12 on: June 14, 2009 »
Still has the slightly annoying movement delay when pressing a key (see above for the solution) but apart from that it's not bad at all.

It's great to see you doing some programming again Drewpee, you should do more but I guess that it's hard to get the time these days.
Shockwave ^ Codigos
Challenge Trophies Won:

Offline DrewPee

  • I Toast Therefore I am
  • Pentium
  • *****
  • Posts: 563
  • Karma: 25
  • Eat Cheese - It's good for you!
    • View Profile
    • Retro Computer Museum
Re: Goto routines . . .
« Reply #13 on: June 14, 2009 »
I am using the commands you mentioned, I think I just have them in the wrong place - need to move the code around a little me thinks! ;)
DrewPee
aka Falcon of The Lost Boyz (Amiga)
Ex-Amiga Coder and Graphic Designer
Administrator of > www.retrocomputermuseum.co.uk

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17414
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: Goto routines . . .
« Reply #14 on: June 14, 2009 »
You should use GETASYNCKEYSTATE
Like this:

IF GETASYNCKEYSTATE( VK_UP ) THEN

(without the =-32768 bit), that why the controls are slightly broken atm.
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Re: Goto routines . . .
« Reply #15 on: June 14, 2009 »
If you are going the Select Case route, if you use Select Case As Const Choice

Choice being a variable of your choice ( hehe, get it ) it's apparently better / faster.
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won: