hold key code returns very high value instead of 0 or 1

Posted By: Reconnoiter

hold key code returns very high value instead of 0 or 1 - 12/16/17 14:20

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?:

Code:
#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();
    ....
  }
}

Posted By: 3run

Re: hold key code returns very high value instead of 0 or 1 - 12/16/17 15:08

Couldn't get your example working either, so I've done it a bit different way:
Code:
#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. grin
Posted By: txesmi

Re: hold key code returns very high value instead of 0 or 1 - 12/17/17 07:48

Originally Posted By: Reconnoiter
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.

Code:
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!


Posted By: Reconnoiter

Re: hold key code returns very high value instead of 0 or 1 - 12/17/17 13:30

Thanks guys for your tips!
Posted By: Reconnoiter

Re: hold key code returns very high value instead of 0 or 1 - 12/17/17 16:10

@txesmi, for some reason for me only the first value keyType is read correctly (while(&keyType == -1)), am I doing something wrong in my code below?:

Code:
var keyType = -1;
				get_keyType(&keyType);
				while(&keyType == -1) wait(1); //wait for value
				debug_var1 = &keyType;
				if (&keyType == KEYTYPE_HOLD) {
                                   beep();
                                   ...

Posted By: txesmi

Re: hold key code returns very high value instead of 0 or 1 - 12/17/17 18:29

The referencing operator '&' is only needed when passing the address of the local variable of the previous function to the last function. The local variable continues been a normal local variable inside the origin function.
Posted By: Reconnoiter

Re: hold key code returns very high value instead of 0 or 1 - 12/17/17 21:28

What weird is however, is than when I remove the & symbols (except for get_keyType(&keyType); ofcourse), not even the while(keyType == -1) wait(1); works anymore. (keyType instead becomes some high number again in the origin function)
Posted By: Reconnoiter

Re: hold key code returns very high value instead of 0 or 1 - 12/22/17 19:34

I couldn't solve the above problem so I use a global variable now (I guess in my case its good enough since the function will probably be used relative rarely instead of multiple calls within the same time window).
© 2024 lite-C Forums