[TI BASIC] Optimizing

Got questions? Got answers? Go here for both.

Moderator: MaxCoderz Staff

DarkerLine
Calc Wizard
Posts: 526
Joined: Tue 08 Mar, 2005 1:37 am
Location: who wants to know?
Contact:

Post by DarkerLine »

pacHa wrote:

Code: Select all

:Repeat Ans=21
:GetKey
:End
This might work, if my basic memories are good (assuming Ans != 21 before this)
Since it's a repeat loop, it doesn't matter if Ans is 21 before it. Not that it matters *points to kalan's post*

If you want to wait for more than one key,

Code: Select all

Repeat max(getKey={21,105
End
or if you want to know which key was pressed afterwards,

Code: Select all

Repeat max(Ans={21,105
getKey
End
and the keypress will be stored in Ans afterwards.
just try to be nice to people.
_________________
My TI Blog - http://mpl.unitedti.org/
User avatar
waeV
New Member
Posts: 74
Joined: Wed 23 Apr, 2008 12:52 am
Location: Maine

Re: [TI BASIC] Optimizing

Post by waeV »

I don't know if something like this has been posted, but I used this in my Tron Basic program and it sped up quite a lot:

instead of

Code: Select all

getkey -> g
if g = 24
then
...
end
if g = 25
then
...
end
I did this

Code: Select all

getkey -> g
if g != 0
if g = 24
then
...
end
if g = 25
then
...
end
end
The only problem is that the game slows down when you hold a key, as it goes through all the if statements.
My ethicator machine must have had a built-in moral compromise spectral release phantasmatron! I'm a genius!
Image
User avatar
tr1p1ea
Maxcoderz Staff
Posts: 4141
Joined: Thu 16 Dec, 2004 10:06 pm
Location: I cant seem to get out of this cryogenic chamber!
Contact:

Re: [TI BASIC] Optimizing

Post by tr1p1ea »

Some people optimise their keypress blocks depending on what you need to do. If you only need to increase/decrease an x/y you can just factor in key checks.

Code: Select all

getKey->K
X+(K=26)-(K=24)->X
Y+(K=34)-(K=25)->Y
"My world is Black & White. But if I blink fast enough, I see it in Grayscale."
Image
Image
User avatar
waeV
New Member
Posts: 74
Joined: Wed 23 Apr, 2008 12:52 am
Location: Maine

Re: [TI BASIC] Optimizing

Post by waeV »

Huh, I didn't know you could do that. How would you get it to continue in the direction of the keypress without needing to be held down?
My ethicator machine must have had a built-in moral compromise spectral release phantasmatron! I'm a genius!
Image
User avatar
tr1p1ea
Maxcoderz Staff
Posts: 4141
Joined: Thu 16 Dec, 2004 10:06 pm
Location: I cant seem to get out of this cryogenic chamber!
Contact:

Re: [TI BASIC] Optimizing

Post by tr1p1ea »

Guess you could:

Code: Select all

getKey
If Ans:Ans->K
X+(K=26)-(K=24)->X
Y+(K=34)-(K=25)->Y
"My world is Black & White. But if I blink fast enough, I see it in Grayscale."
Image
Image
User avatar
waeV
New Member
Posts: 74
Joined: Wed 23 Apr, 2008 12:52 am
Location: Maine

Re: [TI BASIC] Optimizing

Post by waeV »

Awesome, that works and keeps the speed fast even when a key is held.

However, I had no idea something like this could be done and still barely understand : does. Where can I read up on this sort of stuff?

I also wanted a variable F that switches between -1 and 1 when 2nd is pressed, an I came up with the following code:

Code: Select all

F * ( -2 * (K=21) + 1) -> F
Oh wait, when I do that it keeps putting 21 in as the last pressed key instead of the direction. Hmm...
My ethicator machine must have had a built-in moral compromise spectral release phantasmatron! I'm a genius!
Image
User avatar
tr1p1ea
Maxcoderz Staff
Posts: 4141
Joined: Thu 16 Dec, 2004 10:06 pm
Location: I cant seem to get out of this cryogenic chamber!
Contact:

Re: [TI BASIC] Optimizing

Post by tr1p1ea »

You could try:

Code: Select all

getKey
If Ans and Ans!=21:Ans->K
F*(-2*(Ans=21)+1)->F
X+(K=26)-(K=24)->X
Y+(K=34)-(K=25)->Y
"My world is Black & White. But if I blink fast enough, I see it in Grayscale."
Image
Image
User avatar
waeV
New Member
Posts: 74
Joined: Wed 23 Apr, 2008 12:52 am
Location: Maine

Re: [TI BASIC] Optimizing

Post by waeV »

Well, now it won't accept 2nd as input.

Does the ":" equate to a "then"?
My ethicator machine must have had a built-in moral compromise spectral release phantasmatron! I'm a genius!
Image
User avatar
tr1p1ea
Maxcoderz Staff
Posts: 4141
Joined: Thu 16 Dec, 2004 10:06 pm
Location: I cant seem to get out of this cryogenic chamber!
Contact:

Re: [TI BASIC] Optimizing

Post by tr1p1ea »

: is the character ':', accessed via Alpha+'.' also the " and " is accessed via 2nd+MATH>LOGIC.
"My world is Black & White. But if I blink fast enough, I see it in Grayscale."
Image
Image
User avatar
kalan_vod
Calc King
Posts: 2932
Joined: Sat 18 Dec, 2004 6:46 am
Contact:

Re: [TI BASIC] Optimizing

Post by kalan_vod »

The ":" is just as if you make a new line, but this just helps when you want to group routines in one area. It is not any faster/slower than creating a new line, it really is just a matter of opinion..I love to use it, as it makes things easier to read.

If you are wanting to know more about basic, I would suggest going to TI|BD.
User avatar
waeV
New Member
Posts: 74
Joined: Wed 23 Apr, 2008 12:52 am
Location: Maine

Re: [TI BASIC] Optimizing

Post by waeV »

Thanks for the link.

I am confused, though, I am used to the following syntax:

Code: Select all

if ans != 0 and ans !=21
then
ans -> k
end
If : is just the same as a new line, then what how come the following doesn't need a then, an end, or a logic operator for the first comparison?

Code: Select all

if ans and ans != 21:ans -> k
My ethicator machine must have had a built-in moral compromise spectral release phantasmatron! I'm a genius!
Image
User avatar
calc84maniac
Regular Member
Posts: 112
Joined: Wed 18 Oct, 2006 7:34 pm
Location: The ex-planet Pluto
Contact:

Re: [TI BASIC] Optimizing

Post by calc84maniac »

"If" only needs a "Then" and "End" if it needs more than one line of code to run if true (colon counts as a newline).
~calc84maniac has spoken.

Projects:
F-Zero 83+
Project M (Super Mario for 83+)
User avatar
waeV
New Member
Posts: 74
Joined: Wed 23 Apr, 2008 12:52 am
Location: Maine

Re: [TI BASIC] Optimizing

Post by waeV »

Well that make for much cleaner code, thanks. I remember you could do something like that in QBASIC but not in Ti-Basic.

So are the following equivalent?

Code: Select all

if ans

Code: Select all

if ans != 0
My ethicator machine must have had a built-in moral compromise spectral release phantasmatron! I'm a genius!
Image
User avatar
benryves
Maxcoderz Staff
Posts: 3089
Joined: Thu 16 Dec, 2004 10:06 pm
Location: Croydon, England
Contact:

Re: [TI BASIC] Optimizing

Post by benryves »

Non-zero is implicitly true.
User avatar
waeV
New Member
Posts: 74
Joined: Wed 23 Apr, 2008 12:52 am
Location: Maine

Re: [TI BASIC] Optimizing

Post by waeV »

Ah, I get it.

Sweet, this will help a ton.
My ethicator machine must have had a built-in moral compromise spectral release phantasmatron! I'm a genius!
Image
Post Reply