I'm about to start a big project, a game, and in it I'm planning on using normal while loops and For's where applicable. In the program I'll need to exit loops and go to specific parts of the program. For example, the user is playing the main part of the game and they hit 2nd to open their inventory so a goto finds Lbl IN and leaves the big loop that it was just in causing a memory leak. The wiki only gave small scale examples and I didn't find them very useful. Is there another way to get the program to jump around it's own lines and not cause a memory leak, or a way to fix a memory leak after it has happened? The only thing that comes to mind is a garbage collect and it wouldn't be very intuitive if the game had to stop every 3 minutes and garbage collect.
if your while is condition based, you can force a number well beyond the condition and test for it outside the loop followed by the lbl
The Silver Phantom welcomes you
and test for it outside the loop followed by the lbl
How would I go about doing that?
while x<10 (some number you have for your condition)
....
if stuff stuff needs happening
100→x
....
end
if x=100
lbl N
The Silver Phantom welcomes you
So, an if statement followed by a lbl will run the contents of the lbl if the if statement results to true?
I tried this, which should be the same principle, but it didn't work.
0→A
If A=1
Lbl A
Disp "Word"
Even though A is 0 it still runs the code in Lbl A and displays "Word"
sorry, not lbl, it should be goto
The Silver Phantom welcomes you
So would this have memory leaks?
lbl 1
disp "HI"
2→A
3→B
While x<5
getkey→Z
If Z=105:Then
3→A
End
If Z≠0 and Z≠105:Then
100→X
End
Disp A
End
goto 1
Nope, you are good. Your "Lbl 1" and your "Goto 1" are at the same level - they are both outside of all if..then..end blocks.
Also, it's good for you to know that the following code will not cause a memory leak. Even though the Lbl and the Goto are enclosed in an if..then..end block, they are both at the same level. By level, I mean the amount of indentation, as shown:
If (condition):Then
Lbl 1
//code
Goto 1
End
Here is a bad example that WILL cause a memory leak, because they are not at the same level of indentation. The goto is making the program jump outside of the current block, which is the whole idea of memory leaks:
Lbl 1
If (condition):Then
Goto 1
End
Also, I noticed in your code:
If Z=105:Then
3→A
End
If Z≠0 and Z≠105:Then
100→X
End
You can remove the Then's and the End's here, because you don't need them when you are only running 1 line of code under the If. You only need the Then and End when you are running more than 1 line under the if statement. This saves some space in memory in this case:
If Z=105
3→A
If Z≠0 and Z≠105
100→X
If Z≠0 and Z≠105
//*** Can be***//
If Z and Z≠105
//***The code Below is 27 Bytes according to TokenIDE***//
If Z=105
3→A
If Z≠0 and Z≠105
100→X
//***The Code Below is 25 bytes according to TokenIDE***//
If Z=105
Then
3→A
Else
If Z
100→X
End
lbl 1
Disp "HI"
2→A:3→B
While x<5
getkey→Z
If Z=105
Then
3→A
Else
If Z
100→X
End
Disp A
End
Goto 1
You can make the inventory screen a subprogram.
Timothy Foster - @tfAuroratide
Auroratide.com - Go here if you're nerdy like me
My face when the first line of the subprograms page is the example I gave at the top… So, when a subprogram is launched from the middle of a loop, it exits the current program. Does it free up all memory space currently being used by the open program i.e. the memory for the End of the loop which causes the memory leak?
The calculator keeps track of which loop it is in using a stack. Each control statement it runs into puts memory onto this stack, and when it encounters an End, it removes the memory from the stack's top. A subprogram called within a loop should re-enter the loop when it finishes, and therefore the normal End statement for the loop will be encountered. Therefore, the memory should be popped like expected, resulting in no memory leak.
Now, I haven't done calculator programming for a good many years now, so this is a bit fuzzy for me. Thankfully, you can try this yourself. See if the program below procurs a memory error.
prgmTESTLEAK
:While 1
:prgmEXITLOOP
:End
pgrmEXITLOOP
:Disp "HI"
:Return
Timothy Foster - @tfAuroratide
Auroratide.com - Go here if you're nerdy like me
Instead of having EXITLOOP display "hi" I made it increment a variable by 1 each time it is called and display it so I can look at it and actually see if it's starting to slow down. It's counted from 0 to 20k and it doesn't seem any slower. Awesome, subprograms should work. Thanks man!