0 registered members (),
1,089
guests, and 10
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
hold key code returns very high value instead of 0 or 1
#469959
12/16/17 14:20
12/16/17 14:20
|
Joined: Dec 2011
Posts: 1,823 Netherlands
Reconnoiter
OP
Serious User
|
OP
Serious User
Joined: Dec 2011
Posts: 1,823
Netherlands
|
Hi, Below is some code I wrote to check if the player holds a key for a 0.5 sec (instead of a normal/quick key press or left mouse button or such), and if so let the player do some other action. However the function returns some weird high value (whether holding the key or not) and doesn't beep, but I am not entirely sure why, maybe something with waiting before returning the value?:
#define KEYTYPE_HOLD 1
//Returns whether key is hold or not
function get_keyType() {
var _keyType = 0;
var _holdDur = 0.5; //sec
var _timer = 0; //sec
while (_timer < _holdDur) { //mouse_left &&
_timer += time_step/16;
wait(1);
}
if (_timer >= _holdDur) _keyType = KEYTYPE_HOLD;
return _keyType;
}
....
....
void ... () {
var _keyType = -1;
_keyType = get_keyType();
while(_keyType == -1) wait(1); //wait for return value
debug_var1 = _keyType; //used as DEBUG_VAR(debug_var1, 430);
if (_keyType == KEYTYPE_HOLD) {
beep();
....
}
}
|
|
|
Re: hold key code returns very high value instead of 0 or 1
[Re: Reconnoiter]
#469962
12/16/17 15:08
12/16/17 15:08
|
Joined: May 2009
Posts: 5,370 Caucasus
3run
Senior Expert
|
Senior Expert
Joined: May 2009
Posts: 5,370
Caucasus
|
Couldn't get your example working either, so I've done it a bit different way:
#define KEY_TYPE_NONE 0
#define KEY_TYPE_HOLD 1
var key_type = 0;
function get_key_hold(var *input, var *key){
var counter = 0, def_time = 0.5;
while(counter <= def_time){
if(*key == 0){ counter = 0; break; }
counter += time_frame / 16;
wait(1);
}
if(counter >= def_time){ *input = KEY_TYPE_HOLD; }
return(*input);
}
function test(){
get_key_hold(&key_type, &mouse_left);
}
function main() {
warn_level = 6;
fps_max = 60;
fps_min = 30;
time_smooth = 0.9;
on_mouse_left = test;
while(1){
DEBUG_VAR(key_type, 10);
if(key_type == KEY_TYPE_HOLD){
beep();
key_type = KEY_TYPE_NONE;
}
wait(1);
}
}
Also 'time_frame' is better for counters/timers (thanks to Superku). I found out that you example get's messed up cause of the while loop, if I removed it, I could get -1... This is weird to me, probably cause of my lack of knowledge I guess.
|
|
|
Re: hold key code returns very high value instead of 0 or 1
[Re: Reconnoiter]
#469970
12/17/17 07:48
12/17/17 07:48
|
Joined: Jun 2007
Posts: 1,337 Hiporope and its pain
txesmi
Serious User
|
Serious User
Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
|
maybe something with waiting before returning the value? yes, that is the reason. You can't return a value when there is a wait inside the function. You might pass a pointer as parameter of the function.
function get_keyType(var* _keyType) {
var _startTime = 0;
while (mouse_left) {
if (total_ticks - _startTime > 8) { // 8 ticks == 0.5 seconds
*_keyType = KEYTYPE_HOLD;
break;
}
wait(1);
}
}
...
get_keyType(&_keyType);
Salud!
|
|
|
Moderated by mk_1, Perro, rayp, Realspawn, Rei_Ayanami, rvL_eXile, Spirit, Superku, Tobias, TSG_Torsten, VeT
|