News:

Function Finder  Find all the functions within source code files

Main Menu

Defining Labels?

Started by Mick Berg, February 01, 2007, 10:58:06 PM

Previous topic - Next topic

Mick Berg

I'm trying to use a Label (with GOTO) and I get an error "Label is not Defined." How do I define a label?
I get this with or without "Explicit" being active.
Thanks,
Mick Berg.

Ian Price

#1

here_is_a_label:



here_is_another:


Just use a word or phrase (no spaces) then add a colon to the end. This will then show up in the Source Inspector under labels.

Is that what you are after? Read the help on Gosub to get a better understanding :)
I came. I saw. I played some Nintendo.

kevin

#2
  Labels can not be indented.  

 In terms of layout PB is a little  more strict than most basics by design.   See help   -> About -> ProgramLayout for how PB expects code should be structured



  ->Read PlayBASIC Online HELP Files - > Program Layout




Mick Berg

Well, what I have is;
StartAgain:
DO
etc,etc,
LOOP

and in a PSUB later on;

IF SPACEKEY()
GOTO StartAgain
ENDIF


And I get the error "Label is not Defined." I've read all the help and I think I'm doing it right.
Thanks,
Mick Berg.

empty

You can't jump out of the current scope. And you should never ever do that anyway, even if you could ;).
Problem is (with almost all languages), if you're in a function/Psub (or jumped somewhere with Gosub for that matter), the return address has to be stored somewhere- most of the time on the stack- so when it hits return or endfunction or endpsub, it knows where to go back to. Now if you jump out of such a routine and never get back, the return address will never be popped off the stack and then, if you do it more often, sooner or later you will get a stack overflow.

So when you're inside a psub or function you can only jump to labels defined inside that psub or function.

kevin

#5
   You can only access labels within the same scope.  Your example actually demonstrates the reason, which is that eventually that program will crash from stack overflow.   (so the EndPSUB hasn't cleaned up the stack)


PlayBASIC Code: [Select]
Psub DoingSomethign()

Goto HeresALocalLabel


; When you're inside a SUB/Function the only labels PB can see
; are those within the same scope.
GOTO DoSomethingElse ; << so this Won't work

HeresALocalLabel:

EndPsub


; This label is visible to the Main Scope
DoSomethingElse:


Gosub HeresALocalLabel ; So, you can't do this either !




EDIT - I was too slow :)


Here's a bare bones framework for game (it's not commented though.. )

Simple Game framework

Mick Berg

#6
Understood! You should maybe include "You can only access labels within the same scope" in the help for GOTO. It is no doubt obvious to you geniuses, but may not be to noobs such as I. And the error message didn't help much either!
Thanks for the game example.
Mick Berg.

kevin


  I'd assumed it did,  it didn't so i've updated GOTO/GOSUB now.

Mick Berg

I wish I'd seen your recent extensive discussion on GOTO's etc in the Game Design forum. I will visit that department much more often in future.
Mick.

kevin


    Well, if you find yourself using Goto a lot, then more often than not your probably replicating some other construct.    It still has it's place however, but it's not the only tool we have..  :)


Kman1011

OOOOO!! :P

Those nasty stack overflows. I remember those back in the days of the ADAM Colecovision

I think then command used was 'POP' to get free of those but never used it. Program structure used 'RETURN'

Quick question...
When would you use 'Gosub' as opposed to Psub?
Ahh... Another visitor. Stay awhile....STAY FOREVER!!!...MWA-HA-HA-HA