Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Discussion / Code for number input (slow and fast.) with one key.

Author
Message
Brazos
21
Years of Service
User Offline
Joined: 7th Apr 2003
Location:
Posted: 28th Jun 2009 16:32
This code works but I was wondering can this code be simplified?

Robert The Robot
17
Years of Service
User Offline
Joined: 8th Jan 2007
Location: Fireball XL5
Posted: 28th Jun 2009 17:37 Edited at: 28th Jun 2009 17:39
You might be able to combine your "If upkey()" statements together, and the same for the down key statements. Something like



Not sure if there's any way to condense this further, but it looks interesting (I haven't actually run the code yet, but I'll give it a try later).

Hope this helps (and that it still works, I spotted a couple more Upkey/Downkey statments at the end of your loop that I missed )

"I wish I was a spaceman, the fastest guy alive. I'd fly you round the universe, in Fireball XL5..."
Ashingda 27
16
Years of Service
User Offline
Joined: 15th Feb 2008
Location:
Posted: 28th Jun 2009 19:03 Edited at: 28th Jun 2009 19:04
Brazos
21
Years of Service
User Offline
Joined: 7th Apr 2003
Location:
Posted: 28th Jun 2009 20:49
Thanks for both of your responses. In each of the codes the slow single increment of the "t" is lost. Can you show how your code could still contain the slow single increment of "t"?
Ashingda 27
16
Years of Service
User Offline
Joined: 15th Feb 2008
Location:
Posted: 28th Jun 2009 23:03
Are you talking about the values of uinc and ddec? If you hold the up key when uflag > 30 then the uinc = 3, after you let go and press again the uinc = 1 due to uflag being < 30.
Brazos
21
Years of Service
User Offline
Joined: 7th Apr 2003
Location:
Posted: 28th Jun 2009 23:44 Edited at: 29th Jun 2009 01:18
@ Ashingda 27
No, I was talking about the variable "t". If you run my original code you'll see that the variable "t" can be slowly inc or dec a single value at a time until uflag or dflag is > 10 then the inc and dec speeds up.
edit:
That was the reason for the last two lines of code before the sync command.
Ashingda 27
16
Years of Service
User Offline
Joined: 15th Feb 2008
Location:
Posted: 29th Jun 2009 01:52 Edited at: 29th Jun 2009 01:52
Ohh I get what you're talking about now.

On this line:


It's checking if uflag has pass 30 and setting uinc to either 3 or 1.

The uflag is reset to 0 if upkey is not pressed.
Libervurto
17
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 29th Jun 2009 05:19 Edited at: 29th Jun 2009 05:46
You need to explain what your code is trying to achieve.
Do you want it to act like holding down a key in microsoft word?
also "t" isn't a very good variable name, is it "time" or what?
Sorry for being grouchy I am tired

I would do this...


TGC Forum - converting error messages into sarcasm since 2002.
Brazos
21
Years of Service
User Offline
Joined: 7th Apr 2003
Location:
Posted: 29th Jun 2009 05:46 Edited at: 29th Jun 2009 15:50
@ OBese87
My code achieves what I wanted it to. I was just wondering if the code could be streamlined. My original code will inc/dec a variable one number at a time and stop when the key is release (that's slow inc/dec). And if the key is held down for a short period of time the variable will inc/dec at a rapid pace (that's fast inc/dec). If you continue to hold the key down inc/dec gets even faster. Does that explain what my code achieves?
I realize "t" is not a very descriptive name for a variable. It's just a number that is being manipulated. I guess I could call it "TheNumber".
Libervurto
17
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 29th Jun 2009 06:06
did my code help?
i edited just as you posted

TGC Forum - converting error messages into sarcasm since 2002.
Brazos
21
Years of Service
User Offline
Joined: 7th Apr 2003
Location:
Posted: 29th Jun 2009 06:45
@ OBese87
No, your code did not help. Did you read my last post? If so, then I guess I have failed to clearly articulate my intended purpose for my code.
Silverman
17
Years of Service
User Offline
Joined: 18th Jan 2007
Location: France
Posted: 29th Jun 2009 12:39 Edited at: 29th Jun 2009 12:56
Code simplified :



AMD Athlon(tm)XP 3200+ (2.2GHz) / DDR pc3200 (1024Mo) / Nvidia 6800GT (driver 178.24 WHQL)/ XP Pro SP3 / DirectX 9.0c (march 2008)/ DBPro 7.1
Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 30th Jun 2009 17:47
let's crunch it some more - eliminate a few variables etc:



Enjoy your day.
Ashingda 27
16
Years of Service
User Offline
Joined: 15th Feb 2008
Location:
Posted: 30th Jun 2009 18:24
Wow Latch, that's much smaller and simpler.

Because abs() is a bit slow, it'll be faster to just multiply flag*dr.
Brazos
21
Years of Service
User Offline
Joined: 7th Apr 2003
Location:
Posted: 1st Jul 2009 00:23
To all fellow coders who responded to my post:

All the code from each of you is good and fine.....but....
all of you are overlooking the original purpose for the code.

Quote: "My code achieves what I wanted it to. I was just wondering if the code could be streamlined. My original code will inc/dec a variable one number at a time and stop when the key is release (that's slow inc/dec). And if the key is held down for a short period of time the variable will inc/dec at a rapid pace (that's fast inc/dec). If you continue to hold the key down inc/dec gets even faster. Does that explain what my code achieves?
I realize "t" is not a very descriptive name for a variable. It's just a number that is being manipulated. I guess I could call it "TheNumber". "


Read that slowly again a see what I mean.
Brazos
21
Years of Service
User Offline
Joined: 7th Apr 2003
Location:
Posted: 1st Jul 2009 04:11
You guys are just messing with my head....aren't you.
Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 1st Jul 2009 04:52
@Ashingda
Very clever! Because dr will be negative when flag is negative or flag will be zero so it resets therefore flag will always be able to be compared to +10 and +30. A neg*neg = positive. Now I'm jealous that I missed that!


@Brazos
I'm pretty sure my code functions like you want. If you hold down the up key, it won't increase t until flag passes 10 then it will increase at a rate of 1 unit. If you continue to hold the up key, once flag passes 30 the increase to t will be 3. And it's the opposite if you hold the Down key. Try it out. I'll slow the iterations so you can see what's happening - unless that's not what you want - a gradual increase in speed in either direction and then a reset when no key is pressed.

This is Ashingdas adaptation but I'm putting a pause in so you can see the numbers:



Enjoy your day.
Brazos
21
Years of Service
User Offline
Joined: 7th Apr 2003
Location:
Posted: 1st Jul 2009 15:50 Edited at: 1st Jul 2009 15:55
@Latch
I had no idea this would be so difficult to describe. Let me see if I can explain:

your code:
Press the up/down key and nothing happens until flag>10 then the inc/dec of the variable 't' just takes off fast

my code:
press up/down key and the variable 't' increases only one value until uflag/dflag>10. This gives the user time to press the up/down key, inc/dec variable and then release the up/down key before uflag/dflag>10.

The purpose of the original code was to be able to inc/dec a variable (press up/down key) only one value and then stop (release up/down key). But, if the up/down key is held down passed the flag count of '10' the variable (t) will be inc/dec at a fast pace and even faster pace if the up/down key is held down longer.

The single inc/dec of the variable allows the user to obtain a precise value choice. Without this single inc/dec and then stop ability of the code, the inc/dec flies past the desired variable value. The uk/dk flag in my original code is what allows the the single inc/dec and then stop ability.

See what I mean?
TheComet
16
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 1st Jul 2009 16:07
Do you mean something like this?



You could probably make this more efficient...

TheComet


Make the path of your enemies easier with Waypoint Pro!
Libervurto
17
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 1st Jul 2009 16:11
@Brazos
What you just described is what my code does, now I am really confused. Latch just has to add a +1 his will work like that too.
You only need one flag because you can't be going up and down at the same time.

TGC Forum - converting error messages into sarcasm since 2002.
Ashingda 27
16
Years of Service
User Offline
Joined: 15th Feb 2008
Location:
Posted: 1st Jul 2009 17:50
Based off Latch's version that does what you described(I think). press up/down, t is inc by 1 only after holding for 10 count t then continues to inc by 1, after 30 count t is inc by 3 points.

Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 1st Jul 2009 18:14 Edited at: 1st Jul 2009 18:24
@Brazos
So on an initial tap or short time press of up or down(before the flag reaches 10) t should increase or decrease only by 1. Right? I was thinking that nothing happened until the flag count at least passed 10. Should be an easy fix.

I simplified it a bit more - I got rid of the abs() and some of the multiplication and got rid of 1 conditional test. However, I had to introduce another conditional test. But I think it does what you are after. Now it's a challenge of scrunching it down even further...



Enjoy your day.
Ashingda 27
16
Years of Service
User Offline
Joined: 15th Feb 2008
Location:
Posted: 1st Jul 2009 18:30 Edited at: 1st Jul 2009 18:31
Yikes I always learn some super neat stuff from you Latch ^_^.

I never thought booleans could be that usefull, now thinking about it, there were alot of cases where I needed to or could used that in my codings.

Can you give a list of the operators and what they are please?
Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 1st Jul 2009 18:56 Edited at: 1st Jul 2009 20:05
Actually Ashingda, you've challenged me on this one! I've got to steal one more of your ideas to squeeze out a little more efficiency. if flag=0 ... That's the only thing I need to check for when up or down is pressed and released. New code:



@Ashingda

Operator list


I think that's all of them.

Enjoy your day.
Ashingda 27
16
Years of Service
User Offline
Joined: 15th Feb 2008
Location:
Posted: 1st Jul 2009 19:59
Wow thanks very much.
Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 1st Jul 2009 20:04 Edited at: 1st Jul 2009 20:06
Ok, I had to try and get rid of all of the conditionals (if thens). I think I got it! Haven't tested this for speed, but it's pretty condensed - there are 4 multiplys though...



Enjoy your day.
Brazos
21
Years of Service
User Offline
Joined: 7th Apr 2003
Location:
Posted: 2nd Jul 2009 05:43
[BIG sigh of relief].....YES! Latch you got it man. TheComet you got it also. OBese87 and Ashingda 27 do each of you see what the purpose of the original code is now? Latch, I don't understand how your code works but it does so beautifully. Thanks to all for your input. Latch a break down of what is going on in your code would be very instructional on my part. [Another BIG sigh of relief] It's so nice to be understood by someone.
TheComet
16
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 2nd Jul 2009 18:43
@ Latch

I didn't know you could use "inc t,((flag=0)*dr" or "inc flag,(dr ! 0)" or "uinc = (flag > 10)". What does that do?

TheComet


Make the path of your enemies easier with Waypoint Pro!
That1Smart Guy
15
Years of Service
User Offline
Joined: 26th Feb 2009
Location: Somewhere...... yep
Posted: 2nd Jul 2009 20:57
the first and 3rd are flag returns i think, they return 1 or 0 based on if the condition is true or false (then multiply that by dr in the 1st's case)

the middle one is a binary operator, not, but im not good at explaining those

Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 3rd Jul 2009 03:26
@Brazos and TheComet
Quote: "the middle one is a binary operator, not, but im not good at explaining those"

You do fine. But DBC is tricky, in other languages ! can be used as a binary NOT where it gives the opposite value of the expression or two expressions compared. In DBC it does a boolean comparison and just returns a true or a false: 1 or 0. True if the values don't match, and false if they do.

Just like That1Smart Guy wrote.

(flag=0), (dr!0), and (flag>10) are all boolean expressions. That means if their condition is met or not met, they return a 1 or a 0 . Then that value is added or multiplied to the other part of the expression.

Since dr = upkey()-downkey() , that means if upkey() is pressed then a 1 is returned by upkey() and if downkey() isn't pressed then 0 is returned by downkey(). So 1-0 is 1. And the opposite if the downkey is pressed : 0-1 = -1 . If both keys are or aren't pressed, then the value of dr is 0.

Multiply and add these values together and you have your results.

If dr is 0, then flag is reset to 0 at the end of each loop. So in the expression

inc t,((flag=0)*dr) + dr*uinc

The ((flag=0)*dr) will result in the value of the upkey()-downkey() expression ONLY when flag=0; since flag is reset if upkey or downkey aren't pressed, then that allows the user to tap the upkey or downkey and only increment t 1 value, either +1 or -1 .

uinc = (flag > 10) + 2*(flag > 30)

(flag > 10) will only be 1 if flag is > 10. Otherwise it's 0. The same with (flag > 30) except when flag > 30. That allows uinc to be 0, 1, or 1+(2*1) which is 3 . Bring uinc back to the previous line

((flag=0)*dr) + dr*uinc

and the expression can be 0,1,3,-1,-3 . Increment t by any of those values, and voila.

Can you figure out what

inc flag,(dr ! 0)

does?

Enjoy your day.
TheComet
16
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 3rd Jul 2009 14:16
Ouch... That is very complicated, but I think I get it...

Quote: "Can you figure out what

inc flag,(dr ! 0)

does?"


I guess it increments flag by the return value of dr ! 0. So if dr is not 0, flag will be incremented by 1, and if it is 0, it won't be incremented. Is that correct?

TheComet


Make the path of your enemies easier with Waypoint Pro!
Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 3rd Jul 2009 20:20
That is correct!

Enjoy your day.
TheComet
16
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 3rd Jul 2009 20:59
Cool! I think I get it!

But one more question:

Quote: "dr=upkey()-downkey()"


What if up and down are pressed at the same time? dr will equal 0, won't it? I would get around that like this:



Is that too slow?

TheComet


Make the path of your enemies easier with Waypoint Pro!
That1Smart Guy
15
Years of Service
User Offline
Joined: 26th Feb 2009
Location: Somewhere...... yep
Posted: 3rd Jul 2009 21:03
im not sure what the project is (im too lazy to scroll up) but if upkey and downkey are both presses shouldnt they cancel each other out?

TheComet
16
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 3rd Jul 2009 21:06 Edited at: 3rd Jul 2009 21:40
Yep, that's what they do. That's what I am trying to fix in the above post. The project is to increment a variable by 1, wait a bit, then increment it faster and faster and faster and faster...

TheComet

EDIT: Ah, wait... My code won't work, because it won't return a negative 1...


Make the path of your enemies easier with Waypoint Pro!
Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 3rd Jul 2009 22:04 Edited at: 3rd Jul 2009 22:08
Why try and work around it? I want it to be zero if up and down are pressed at the same time. The computer can't assume which direction key you're meaning to press, and since they are opposite, the result should be zero.

If you want to create a priority key, so that up for instance, takes priority over down, then add an OR comparison:



Enjoy your day.
TheComet
16
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 3rd Jul 2009 22:15
Very clever, Latch.

Is OR a binary operation too? | <-- This symbol

What does it do exactly?

TheComet


Make the path of your enemies easier with Waypoint Pro!
Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 3rd Jul 2009 22:19
look further up in the thread at the operators list

Enjoy your day.
TheComet
16
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 3rd Jul 2009 22:39
Ah, sorry. I didn't spot it before.

TheComet


Make the path of your enemies easier with Waypoint Pro!

Login to post a reply

Server time is: 2024-05-20 13:18:55
Your offset time is: 2024-05-20 13:18:55