Omnimaga

Calculator Community => TI Calculators => TI-BASIC => Topic started by: Yeong on October 15, 2010, 07:38:32 am

Title: Jump without Lbl-Goto
Post by: Yeong on October 15, 2010, 07:38:32 am
Is there any ways to jump to other part of program without using Lbl-Goto?
I heard that the Lbl-Goto slows down a game a lot :(
Title: Re: Jump without Lbl-Goto
Post by: JosJuice on October 15, 2010, 11:09:02 am
You could make a second program that your main program calls when you want to use a specific piece of code, but that would require the user to have both of those programs on their calc when playing. I think it's faster than Goto, but I'm not sure.

EDIT: Oh, it seems like you already did use this. I don't know any other methods than Goto and subprograms.
Title: Re: Jump without Lbl-Goto
Post by: DJ Omnimaga on October 15, 2010, 11:25:04 am
Instead of something like

Code: [Select]
Lbl 0
If something
Goto 1
If somethingelse
Goto 2
Goto 0
Lbl 1
Do stuff
Goto 0
Lbl 2
Do some other stuff
Goto 0

You would use

Code: [Select]
While 1
0->A
If something
1->A
If something else
2->A
If A=1
Do stuff
If A=2
Do some other stuff
End
Title: Re: Jump without Lbl-Goto
Post by: Builderboy on October 15, 2010, 03:40:18 pm
Note that goto's themselves do not slow a game down, only is they are misused.  That and the fact that they take a bit of time to execute (since the OS searches your whole program for the Label to jump to) and could make parts of your game slower.

Goto can also make your game run slower if they are used incorrectly, try running this program

Code: [Select]
0->A
Lbl T
A+1->A
If getKey=0:Then
Output(1,1,A
Goto T
End

You will notice as the program counts, the numbers seem to increase slower and slower.  This is because you have a Goto inside of an If:Then block.  When the calculator enters the If:Then block, it sets aside a bit or memory so that it knows what to do when it gets to the End that closes the block.  However, you Goto out of this block and up into T.  The TiOS doesn't realize this, and so it still has that chunk of memory set aside.  Enter the If Block again and another chunk of memory is set aside.  Do this enough and eventually your program gets the dreaded Error:Memory!
Title: Re: Jump without Lbl-Goto
Post by: DJ Omnimaga on October 15, 2010, 03:49:04 pm
Note that goto's themselves do not slow a game down, only is they are misused.  That and the fact that they take a bit of time to execute (since the OS searches your whole program for the Label to jump to) and could make parts of your game slower.

Goto can also make your game run slower if they are used incorrectly, try running this program

Code: [Select]
0->A
Lbl T
A+1->A
If getKey=0:Then
Output(1,1,A
Goto T
End

You will notice as the program counts, the numbers seem to increase slower and slower.  This is because you have a Goto inside of an If:Then block.  When the calculator enters the If:Then block, it sets aside a bit or memory so that it knows what to do when it gets to the End that closes the block.  However, you Goto out of this block and up into T.  The TiOS doesn't realize this, and so it still has that chunk of memory set aside.  Enter the If Block again and another chunk of memory is set aside.  Do this enough and eventually your program gets the dreaded Error:Memory!
They can slow things down if the label is at the bottom of a 10 KB large program, though. In some cases, there's a 1 second loading time until the TI-OS reaches the appropriate lbl.
Title: Re: Jump without Lbl-Goto
Post by: Silver Shadow on October 15, 2010, 03:53:40 pm
Yeah, so try not to use Gotos inside parts which are executed very often (such as key detects, grayscale,...).
Title: Re: Jump without Lbl-Goto
Post by: AngelFish on October 15, 2010, 03:55:15 pm
Instead of something like

Code: [Select]
Lbl 0
If something
Goto 1
If somethingelse
Goto 2
Goto 0
Lbl 1
Do stuff
Goto 0
Lbl 2
Do some other stuff
Goto 0

You would use

Code: [Select]
While 1
0->A
If something
1->A
If something else
2->A
If A=1
Do stuff
If A=2
Do some other stuff
End


I use both methods when I do BASIC. I use If: Then commands to control small, fast routines and Lbl/Goto commands to implement subroutines. Subprograms take a bit of time to load, so I try to reserve them for loading entirely new areas of a program, such as a second menu.
Title: Re: Jump without Lbl-Goto
Post by: DJ Omnimaga on October 15, 2010, 03:56:16 pm
Yeah, I noticed sub-programs seems to take a bit, especially when you have many or run low on memory. In Metroid II Evolution the speed drop is insane x.x
Title: Re: Jump without Lbl-Goto
Post by: Silver Shadow on October 15, 2010, 03:57:20 pm
Yeah that's why I always try to have only 1 program.
Title: Re: Jump without Lbl-Goto
Post by: DJ Omnimaga on October 15, 2010, 03:59:10 pm
THe issue, though, is when the sub program is extremly large, skipping the massive if condition seems to take a while too. Notice how long the pause is when an event is triggered in Reuben Quest. So in some cases, I am worried there may not be much of a difference.
Title: Re: Jump without Lbl-Goto
Post by: AngelFish on October 15, 2010, 04:07:09 pm
I actually have a method to speed up those If:Then lags. It eats up memory, but that's usually not a problem if you're working in BASIC ;)

Basically, you group all of the similar If: Then commands into one larger If: Then command. Then, when the program looks at the block, it evaluates only one If statement and it can simply skip computing the rest of the block. It won't make your program blazing fast, but it will make reasonably quick from painfully slow.
Title: Re: Jump without Lbl-Goto
Post by: DJ Omnimaga on October 15, 2010, 04:23:37 pm
Yeah that's what I do with Illusiat 13. It speeds things up considerably. I think in Reuben, they're one after each others. I wonder if Reuben could have them grouped in a way that makes the game faster...
Title: Re: Jump without Lbl-Goto
Post by: AngelFish on October 15, 2010, 04:50:05 pm
I used it with CAD, because there were a LOT of different If: then statements necessary and they were conveniently grouped by function.
Title: Re: Jump without Lbl-Goto
Post by: yunhua98 on October 15, 2010, 04:50:40 pm
I don't know if this is what you want but hers a piece of code using gotos but not that slow:

:Lbl 1
:stuff
:End
:While 1
:stuff
:Goto 1

and you can break out of it by changing the while condition.  and the goto isn't slow if the label is at the top.  ;)
Title: Re: Jump without Lbl-Goto
Post by: DJ Omnimaga on October 15, 2010, 05:55:31 pm
Doesn't that cause an ERR:SYNTAX the first time it goes through Lbl 1, though? ??? (assuming that's at the program top)
Title: Re: Jump without Lbl-Goto
Post by: yunhua98 on October 15, 2010, 05:56:51 pm
nah, I generally make it goto 0 first and then start at thw hile or whatever ele you wanted to start at.  ;)
Title: Re: Jump without Lbl-Goto
Post by: DJ Omnimaga on October 15, 2010, 05:58:34 pm
Ah right, I see :D
Title: Re: Jump without Lbl-Goto
Post by: yunhua98 on October 15, 2010, 05:59:31 pm
wow, I misspelled a lot of stuff.  :P
Title: Re: Jump without Lbl-Goto
Post by: meishe91 on October 16, 2010, 01:39:30 am
I believe another way to use Lbl/Goto more efficiently would be to put all your sub-routines at the top of your program. Then you put your main code in a label too and put a Goto at the top that goes to that label. That way when a sub-routine is called it won't have to scan far to call the routine. It can be tricky to code that though, sometimes.

I didn't see anyone else suggest that so ya. Hopefully that makes sense.
Title: Re: Jump without Lbl-Goto
Post by: DJ Omnimaga on October 16, 2010, 02:14:02 am
Yeah in some old games, what I did is to put menus and other stuff that aren't related to the main engine completly at the bottom of my programs and the main engine at the top. It was much faster.
Title: Re: Jump without Lbl-Goto
Post by: shrear on October 16, 2010, 04:47:25 pm
:Lbl 1
:stuff
:End
:While 1
:stuff
:Goto 1

I suppose you mean something like
Lbl a
stuff
While something
stuff
EndWhile
stuff
If something
Goto a

Just say yes or no because I honestly don't understand what you mean else.
Title: Re: Jump without Lbl-Goto
Post by: yunhua98 on October 16, 2010, 04:50:34 pm
no, theres no endwhile, the goto makes it jump to the lbl, and then when it reaches the "End", it thinks the End belongs to the While, because the TIOS stores a bit of memory to look for the end.  it never sees an End til the end after the lbl, so it thinks that end belongs with the While, so it jumps back to where the While is. and jumping back from a loop is really fast.  ;)
Title: Re: Jump without Lbl-Goto
Post by: shrear on October 16, 2010, 05:10:00 pm
Ah alright
thx for explaining