|
Post by TinSoldier on Jul 15, 2016 22:17:22 GMT
A simple oneshot keypress.
bool MClick=false; float[] Timer(3);// create an array of 3 timers, numbered 0 to 2. // you can substitute // float timer = 0.0f; // if only one timer is needed.
void Main(){ // start timer when any key is pressed, reset to 0.0f when key is released. if(iKeyDown(-1))Timer[0]+=0.016667f; else Timer[0]=0.0f;
// check for the [F3}key pressed, process MClick only once after timer starts, // because Timer[0] is less than 0.03f. if(iKeyDown(iKeyCode("DIK_F3")) && Timer[0]<0.03f)MClick=!MClick; }
|
|
|
Post by Karvy on Jul 17, 2016 21:09:52 GMT
Why did you used 0.016667? very useful script thanks!
|
|
|
Post by TinSoldier on Jul 18, 2016 5:22:18 GMT
0.016667 is (1 second / 60), because 3drad's engine process's each game cycle 60 times a second.
so 60 times 0.016667 is very close to 1 second or 60 engine cycles. that's an easy way to time things.
You could also use the clock script command for timing, but that's a little more involved.
int iSystemTime(int); Return system clock data. int = identifier of the information to return, as follows: 0 - Year 1 - Month 2 - DayOfWeek 3 - Day 4 - Hour 5 - Minute 6 - Second 7 - Milliseconds For example, iSystemTime(4) will return current hour.
|
|
|
Post by NicusorN5 on Jul 18, 2016 6:14:24 GMT
Uhh ...
So use this ratio : 1/75 = 0.01(3) ≈ 0.13334 ...
|
|
|
Post by F1Andre on Jul 18, 2016 11:15:50 GMT
Awesome, I use the exact same concept in Unity for just about anything that it can be used on.
|
|
|
Post by Red Moon Entertainment on Jul 18, 2016 13:09:03 GMT
Sorry, what's oneshot keypress?
|
|
|
Post by NicusorN5 on Jul 18, 2016 13:20:52 GMT
It is a timer activated with a key press ...
|
|
|
Post by TinSoldier on Jul 18, 2016 15:39:18 GMT
um Uhh ... Because, when the project is launched, the script is executed 75 times per second, what you see on your screen is a twinkling sprite.
So use this ratio : 1/75 = 0.01(3) ≈ 0.13334 ...
Read more: http://3dradboards.org/post/new/730#ixzz4Em7FlZK9
I believe if you do the timing in 3drad, compare it with your system clock, you'll see it's actually 60 cycles per second, that referance to 75 came from the documentation and is a mistake that was never corrected. I can script this to prove it if you like, but i'd rather not cause i've done it before, it's your turn
|
|
|
Post by TinSoldier on Jul 18, 2016 15:46:03 GMT
A oneshot keypress referrer's to a key press that only registers only once when you press the key, until you release ( one shot)
In 3drad you'll find a few key input situations where you'll get a repeating input when you press a key.. not always, but often, it will drive you nuts without a oneshot script process.
Sorry, what's oneshot keypress?
|
|
|
Post by Power Supersport on Jul 24, 2016 12:55:30 GMT
I use another method that does the same thing... You don't need that complicated numbers to actually register a key press once...
Take a look at this script...
int time;
void Main () {
if (iKeyDown(iKeyCode("DIK_SPACE"))) { if (time < 3) time += 1; if (time == 1) { // your stuff here... } } else time = 0;
}
The line "time < 3" just ensures that the time will not increment above 3... We don't need higher values... It's just a very little performance optimization... But it's not that necessary... I just feel more convenient when I limit the timer...
|
|
|
Post by cypermethrin on Jul 24, 2016 17:36:17 GMT
Or to technically reduce the memory overheads more (although you'll never notice) take all the timers out...
Bool KeyLock;
void Main () {
if (iKeyDown(iKeyCode("DIK_SPACE")) && KeyLock == false) {
KeyLock = true; // your stuff here...
} if (iKeyDown(iKeyCode("DIK_SPACE") == false) Keylock = false; }
|
|
|
Post by Power Supersport on Jul 24, 2016 21:15:28 GMT
Yes, your method seems the simpliest and most efficient...
|
|
|
Post by TinSoldier on Jul 25, 2016 1:15:45 GMT
I see 2 ways to do the same thing ( powers is the same as mine, just using a simpler time process ). But of course, mine is the best way cause i say it is
|
|