goto (runs and hides)

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
Da_Elf
Posts: 311
Joined: Sun Apr 27, 2014 2:45 am

goto (runs and hides)

Post by Da_Elf » Tue Aug 12, 2014 11:26 pm

I know using a goto command in some languages just shows poor code layout. i remember it as far back as commodore 64 and 128 days. Does this exist in livecode?

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10333
Joined: Wed May 06, 2009 2:28 pm

Re: goto (runs and hides)

Post by dunbarx » Wed Aug 13, 2014 12:11 am

In basic, "goTo" allows a subroutine to be executed. The calling "handler" pauses, and the designated routine starts. Control reverts to the calling handler upon completion.

In LC, the same concept exists, though mightily enhanced, and the methods to call the "subroutine" are varied. Knowing the hierarchy, you can:

Code: Select all

yourCode
yourCode
subRoutine --This is the, er, "subroutine"
yourCode
yourCode
or you could:

Code: Select all

yourCode
yourCode
send "subroutine" to yourDestinationObject --This has additional power
yourCode
yourCode
or:

Code: Select all

yourCode
yourCode
do "subRoutine" --This has considerable additional power
yourCode
yourCode
or:

Code: Select all

yourCode
yourCode
send "subRoutine" && paramLlist  to anywhereAtAllOnThePlanet --This has even more and better and just darned extra considerable additional power
yourCode
yourCode
or:

or:

or:

LC wins hands down, in flexibility, power and fun.

Craig Newman

Da_Elf
Posts: 311
Joined: Sun Apr 27, 2014 2:45 am

Re: goto (runs and hides)

Post by Da_Elf » Wed Aug 13, 2014 12:23 am

i cant find anything on subroutine in the dictionary and from what i see by googling it it has something to do with variables and from the way your describing it it doings like a "Command" or "function". i was thinking of using a goto command like being in an if and then jumping a section of code by lets say going to line 80 of the code. on the commodore 64 it allowed you to jump forward or backwards in your code

WaltBrown
Posts: 466
Joined: Mon May 11, 2009 9:12 pm

Re: goto (runs and hides)

Post by WaltBrown » Wed Aug 13, 2014 1:09 am

Nope. I used to use those for clean up and error recovery in earlier HLLs. You want to create a label somewhere in the routine and jump to it from various other places, which LC can't do. Expanding on Craig's answer, LC has lots of flexibility for this, and one mechanism would be to create a separate recovery subroutine that your main routine can start up and exit (in pseudocode, off the top of my head):

on mySub
doStuff
if exceptionDiscovered then
send "myCleanUp" && someErrorInformation to me in 0 milliseconds
exit to top
end if
doSomeMoreStuff
if exceptionDiscovered then
send "myCleanUp" && someErrorInformation to me in 0 milliseconds
exit to top
end if
end mySub

on myCleanUp pTheErrorCode
doErrorLoggingStuff
undoStuff
end myCleanUp
Walt Brown
Omnis traductor traditor

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10333
Joined: Wed May 06, 2009 2:28 pm

Re: goto (runs and hides)

Post by dunbarx » Wed Aug 13, 2014 5:48 am

Livecode uses discrete, named handler and function routines exclusively, These are accessed by, and one might consider their "address" to be referenced by, those names . This is basic stuff, and part of what you need to know at the foundation of the language. Consider this, which only touches the surface of LC capabilities. In a new card with one button and one field, in the button script:

Code: Select all

on mouseUp
  put random(9) into field 1
  checkTheField
end mouseUp

on checkTheField
  if fld 1 > 5 then answer "Big Number in Field"
end checkTheField
Now this:

Code: Select all

on mouseUp
  put random(9) into field 1
 answer checkTheField(field 1)
end mouseUp

function checkTheField theValue
  if theValue > 5 then return "Big Number in Field"
end checkTheField
Why call other handlers or functions at all? Couldn't the action of each of those "mouseUp" handlers be implemented directly? This is the stuff of the first 50 hours of getting to know the structure of LC. If you are familiar with the "goto" command, you will pick up the nature and value of the message paradigm right away, and be blown away. Go through the tutorials, practice, and write back when you are stuck. But you must, simply must get stuck first.

Craig Newman

Da_Elf
Posts: 311
Joined: Sun Apr 27, 2014 2:45 am

Re: goto (runs and hides)

Post by Da_Elf » Wed Aug 13, 2014 11:28 pm

instead of
on checkTheField
is usually write
command checkTheField

which i think is the exact same thing just that "on" is shorter to type than "command"... then again if you use "on" can you pass it variables like command can do?

i wasnt really asking about goto as a serious thing i wanted to use. I found an old notebook i had written some C64 game code in and goto was one of the commands i used and i dont think ive seen it anywhere else since then, i think PHP has it but ive never used it there and i saw people saying bad things about it

Image

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10333
Joined: Wed May 06, 2009 2:28 pm

Re: goto (runs and hides)

Post by dunbarx » Thu Aug 14, 2014 3:38 am

The two are synonyms. I use "on" because I always have.

They are control structures that define a message handler, and are the basic building block of LC. Parameters may be attached as required. These are evaluated before they are passed, and may be by value or by reference. Very powerful tools.

on mouseUp
doSomething the long date,myVariable,random(99),theSkyIsTheLimit
end mouseUp

Wikipedia has a great discussion of "goto"

Craig

Post Reply