Omnimaga
Calculator Community => TI Calculators => TI-BASIC => Topic started 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 :(
-
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.
-
Instead of something like
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
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
-
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
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!
-
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
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.
-
Yeah, so try not to use Gotos inside parts which are executed very often (such as key detects, grayscale,...).
-
Instead of something like
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
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.
-
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
-
Yeah that's why I always try to have only 1 program.
-
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.
-
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.
-
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...
-
I used it with CAD, because there were a LOT of different If: then statements necessary and they were conveniently grouped by function.
-
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. ;)
-
Doesn't that cause an ERR:SYNTAX the first time it goes through Lbl 1, though? ??? (assuming that's at the program top)
-
nah, I generally make it goto 0 first and then start at thw hile or whatever ele you wanted to start at. ;)
-
Ah right, I see :D
-
wow, I misspelled a lot of stuff. :P
-
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.
-
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.
-
: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.
-
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. ;)
-
Ah alright
thx for explaining