Author Topic: Programming help  (Read 15651 times)

0 Members and 1 Guest are viewing this topic.

Offline indubitably

  • LV2 Member (Next: 40)
  • **
  • Posts: 35
  • Rating: +1/-0
    • View Profile
Programming help
« on: September 30, 2009, 11:54:15 pm »
I was bored in class so I decided to try and make a ddr type game.
I did so and i like it, but it slows down over time and I cannot figure out how to prevent this.
Is this a problem with my code or is it the calculator?
I didn't think it was the calc because there are much bigger games that keep a constant speed.
Also I tried the program on an emulator and it still slowed down. I cannot find any reason why this would happen in the code.

Thanks for any help in advance!

Link:
http://www.ticalc.org/archives/files/fileinfo/421/42167.html


Code: [Select]
:ClrHome
:0→S
:9→O
:9→P
:9→Q
:9→R
:Goto Z
:Lbl A
:0→X
:0→A
:While X=0
:(A+1)→A
:If A=10
:Goto B
:getKey→X
:End
:Goto B
:Lbl B
:If X=24
:Goto 1
:If X=25
:Goto 3
:If X=26
:Goto 4
:If X=34
:Goto 2
:If O=2
:(S-1)→S
:If P=2
:(S-1)→S
:If Q=2
:(S-1)→S
:If R=2
:(S-1)→S
:Goto X
:Lbl 1
:If O=2
:Then
:(S+1)→S
:9→O
:Goto X
:Else
:(S-1)→S
:Goto X
:End
:Lbl 2
:If P=2
:Then
:(S+1)→S
:9→P
:Goto X
:Else
:(S-1)→S
:Goto X
:End
:Lbl 3
:If Q=2
:Then
:(S+1)→S
:9→Q
:Goto X
:Else
:(S-1)→S
:Goto X
:End
:Lbl 4
:If R=2
:Then
:(S+1)→S
:9→R
:Goto X
:Else
:(S-1)→S
:Goto X
:End
:Lbl Z
:randInt(1,4)→M
:1→N
:Goto X
:Lbl X
:If N=0
:Goto Z
:(N-1)→N
:If M=1
:Then
:If O=9
:(O-1)→O
:End
:If M=2
:Then
:If P=9
:(P-1)→P
:End
:If M=3
:Then
:If Q=9
:(Q-1)→Q
:End
:If M=4
:Then
:If R=9
:(R-1)→R
:End
:0→M
:Goto Y
:Lbl Y
:ClrHome
:If O≤8
:Then
:Output(O,7,"<")
:(O-1)→O
:End
:If P≤8
:Then
:Output(P,8,"V")
:(P-1)→P
:End
:If Q≤8
:Then
:Output(Q,9,"^")
:(Q-1)→Q
:End
:If R≤8
:Then
:Output(R,10,">")
:(R-1)→R
:End
:Output(1,1,S)
:Output(3,6,"-"
:Output(3,11,"-"
:If O=0
:9→O
:If P=0
:9→P
:If Q=0
:9→Q
:If R=0
:9→R
:Goto A
« Last Edit: October 01, 2009, 01:00:06 am by indubitably »

Offline cooliojazz

  • Support Staff
  • LV7 Elite (Next: 700)
  • *******
  • Posts: 619
  • Rating: +66/-9
  • I omnoms on your soul
    • View Profile
    • Unreal Phantasies
Re: Programming help
« Reply #1 on: October 01, 2009, 12:12:20 am »
It would help anyone wnting to help if you posted the code too, maybe with comments. :P
Spoiler For Random signess:
You can not beat my skills.
Trust me.
So don't even try.
And remember never to trust someone who says, "Trust me."

TI File Editor Progress: Remade in java like a boss. 50% we'll call it? IDK =P
Java Libraries: JIRC - 90% JTIF - 5%
TI Projects: Unreal Notator - -5000%
Nomcraft, a Bukkit mod
Some of the music I write can be found here | The Rest Should Be Here (Bandcamp)

Offline indubitably

  • LV2 Member (Next: 40)
  • **
  • Posts: 35
  • Rating: +1/-0
    • View Profile
Re: Programming help
« Reply #2 on: October 01, 2009, 12:18:03 am »
Is there a way I can copy it right from the program?

Offline Builderboy

  • Physics Guru
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5673
  • Rating: +613/-9
  • Would you kindly?
    • View Profile
Re: Programming help
« Reply #3 on: October 01, 2009, 12:20:14 am »
Hi Indubitably (coolest word ever ^^) and welcome to the site!

The problem you are describing is called a memory leak.  when you enter a while loop, the calculator needs to remember that it has entered the while loop, so it will know what to do when it gets to the End.  So what it does is set aside a small pice of memory coresponding to that while loop.  When the program exits the while loop, the calculator is able to get rid of that memory, and everything works great.

The problem arises when you have code like this

Code: [Select]
While x=0
If A
Goto B
End

Lbl B

When you Goto out of the loop, the calculator is never able to reach the End it needs to in order to get rid of that memory.  As a result, you have this chunk of memory that is never going to be free.  If you continue doing this over and over again, eventually the program is going to get very slow, and eventually crash when the calculator runs out of memory.

I see that you use a lot of Goto's in your program, and while that is not a bad thing, it is very important that you don't Goto out of a Loop, as this causes memory errors, slowdowns, and crashes :(


By the way, if you want to convert your program to text, you can use this web program called sourcoder :)
http://www.cemetech.net/projects/basicelite/sourcecoder2.php
It will take in and display your program, and even try to optimize it in some cases.

Offline indubitably

  • LV2 Member (Next: 40)
  • **
  • Posts: 35
  • Rating: +1/-0
    • View Profile
Re: Programming help
« Reply #4 on: October 01, 2009, 12:29:05 am »
Thanks for the reply, the reason I did this is because I really do not know another
way to only allow a certain amount of time for a getkey command.

I am thinking that I should change the code to
Code: [Select]
While x=0
If A
9>x
end

This would end the while loop. I would just have to make sure that doesn't mess with
scoring or even button recognition.

Offline jsj795

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1105
  • Rating: +84/-3
    • View Profile
Re: Programming help
« Reply #5 on: October 01, 2009, 12:43:07 am »
yeah playing around with goto/lbl is not a good idea because it is highly likely to give you stack mems...


Spoiler For funny life mathematics:
1. ROMANCE MATHEMATICS
Smart man + smart woman = romance
Smart man + dumb woman = affair
Dumb man + smart woman = marriage
Dumb man + dumb woman = pregnancy
2. OFFICE ARITHMETIC
Smart boss + smart employee = profit
Smart boss + dumb employee = production
Dumb boss + smart employee = promotion
Dumb boss + dumb employee = overtime
3. SHOPPING MATH
A man will pay $2 for a $1 item he needs.
A woman will pay $1 for a $2 item that she doesn't need.
4. GENERAL EQUATIONS & STATISTICS
A woman worries about the future until she gets a husband.
A man never worries about the future until he gets a wife.
A successful man is one who makes more money than his wife can spend.
A successful woman is one who can find such a man.
5. HAPPINESS
To be happy with a man, you must understand him a lot and love him a little.
To be happy with a woman, you must love her a lot and not try to understand her at all.
6. LONGEVITY
Married men live longer than single men do, but married men are a lot more willing to die.
7. PROPENSITY TO CHANGE
A woman marries a man expecting he will change, but he doesn't.
A man marries a woman expecting that she won't change, and she does.
8. DISCUSSION TECHNIQUE
A woman has the last word in any argument.
Anything a man says after that is the beginning of a new argument.

Girls = Time * Money (Girls are a combination of time and money)
Time = Money (Time is money)
Girls = Money squared (So, girls are money squared)
Money = sqrt(Evil) (Money is also the root of all evil)
Girls = sqrt(Evil) squared (So, girls are the root of all evil squared)
Girls = Evil (Thus, girls are evil)
*Girls=Evil credit goes to Compynerd255*

Offline TsukasaZX

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 415
  • Rating: +46/-3
  • Never Gonna Give You Up!
    • View Profile
Re: Programming help
« Reply #6 on: October 01, 2009, 01:02:20 am »
Why not do:
Code: [Select]
:While X=0 and A<10
:A+1→A
:getKey→X
:End
:Goto B

Also, there's some optimizations you could make if you want to make your game smaller and faster.
IRC EFNet | Nick Su-Hime | Chan #omnimaga, #unss, #cemetech
Yumé - 100% Complete!
Yumé 2: Rika's Story - 10%
 - Hiatus until summertime
Yumé: The Eternal Dreams - 50%
 - World and events
TIBiC/GO C Library - N%

"Alenda lux ubi orta libertas" - Let learning be cherished where liberty has arisen.

  ▲
▲ ▲ Triforce!

Offline indubitably

  • LV2 Member (Next: 40)
  • **
  • Posts: 35
  • Rating: +1/-0
    • View Profile
Re: Programming help
« Reply #7 on: October 01, 2009, 01:11:55 am »
I was not aware that there could be 2 conditions for a while loop.
I tried that and it worked really nice.

Offline TsukasaZX

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 415
  • Rating: +46/-3
  • Never Gonna Give You Up!
    • View Profile
Re: Programming help
« Reply #8 on: October 01, 2009, 01:13:48 am »
Yup, you can have pretty much as many conditions as you want in an If or While. :)
Glad I helped some ;D
IRC EFNet | Nick Su-Hime | Chan #omnimaga, #unss, #cemetech
Yumé - 100% Complete!
Yumé 2: Rika's Story - 10%
 - Hiatus until summertime
Yumé: The Eternal Dreams - 50%
 - World and events
TIBiC/GO C Library - N%

"Alenda lux ubi orta libertas" - Let learning be cherished where liberty has arisen.

  ▲
▲ ▲ Triforce!

Offline megasasquatch

  • LV3 Member (Next: 100)
  • ***
  • Posts: 54
  • Rating: +0/-0
    • View Profile
    • Pixel Productions
Re: Programming help
« Reply #9 on: October 01, 2009, 02:28:30 am »
Ahh... Good ole programming during class. :)
I'm a PC, but I don't use Windows.

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: Programming help
« Reply #10 on: October 01, 2009, 02:28:03 pm »
Hi and welcome here :)

Another thing about Goto/Lbls:

The farther the Lbl in your program, the longer it will take to load until it reaches it. Because of this, it is recommended to immediately start your program with Goto 0 and have Lbl 0 with the intro screen, initializing and menu/options (if any) located at the end of your game program and have the game main loop (where arrows rises) at the top. However, if you use While/Repeat for the main game loop, this might not do such a difference. However, in case you use a lot of Goto, it might be best. Your program would look like this:

PRGM:PRGMNAME
Goto 0
Lbl 1
<game code>
Lbl 0
<initializing/menu code>
Goto 1

The issue is that when you use Goto, the calculator will search from the beginning of the program code and go down until it reaches the first Lbl of the same name. Because of this, if someone uses a lot of Lbl and Gotos, and his game main code is very very far in the program (especially for very large games), it will run considerably slower

Offline indubitably

  • LV2 Member (Next: 40)
  • **
  • Posts: 35
  • Rating: +1/-0
    • View Profile
Re: Programming help
« Reply #11 on: October 01, 2009, 08:13:39 pm »
wow that explains a lot considering my most commonly referenced
label is label x, which is located all the way at the bottom.
There are also some unnecessarily placed gotos and lbls that can
be removed as well.

Offline Ti-newb

  • LV4 Regular (Next: 200)
  • ****
  • Posts: 180
  • Rating: +3/-0
    • View Profile
Re: Programming help
« Reply #12 on: November 15, 2009, 04:42:36 pm »
stay away from Lbls and Goto's =DDD.

Another reason to stay away from Lbls and Goto's.. When you try to edit ur prgm, you will be confused, you'll have to 'track/folow' your prgm to see what it does again. and when u finally think u remember whatu were thinking, you'll forget. That and its hard for people to help you... im not sure i wanna read through that prgm =P no offence lol.

*EDIT:

Just wondering.. are u gonna make a new DDR game? or have it Edited? i wanna try it out.

Offline Galandros

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1140
  • Rating: +42/-10
    • View Profile
Re: Programming help
« Reply #13 on: November 15, 2009, 04:49:18 pm »
Goto and Lbl in TI-BASIC are slow (DJ shows why). Also you can do everything in program flow without them.

That said there is no need to use them. The exception is the Menu( command... Lbl's with Menu( isn't a that bad idea depending on what you do.
Hobbing in calculator projects.

Offline Ti-newb

  • LV4 Regular (Next: 200)
  • ****
  • Posts: 180
  • Rating: +3/-0
    • View Profile
Re: Programming help
« Reply #14 on: November 16, 2009, 09:13:56 pm »
Hah, exactly why i made a Menu Subprgm, =D it doesnt use Label =DDD