Gamestudio Links
Zorro Links
Newest Posts
Blobsculptor tools and objects download here
by NeoDumont. 03/28/24 03:01
Issue with Multi-Core WFO Training
by aliswee. 03/24/24 20:20
Why Zorro supports up to 72 cores?
by Edgar_Herrera. 03/23/24 21:41
Zorro Trader GPT
by TipmyPip. 03/06/24 09:27
VSCode instead of SED
by 3run. 03/01/24 19:06
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
2 registered members (VoroneTZ, AndrewAMD), 833 guests, and 5 spiders.
Key: Admin, Global Mod, Mod
Newest Members
sakolin, rajesh7827, juergen_wue, NITRO_FOREVER, jack0roses
19043 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
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 Offline OP
Serious User
Reconnoiter  Offline 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?:

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


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 Offline
Senior Expert
3run  Offline
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:
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


Looking for free stuff?? Take a look here: http://badcom.at.ua
Support me on: https://boosty.to/3rung
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 Offline
Serious User
txesmi  Offline
Serious User

Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
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!



Re: hold key code returns very high value instead of 0 or 1 [Re: txesmi] #469973
12/17/17 13:30
12/17/17 13:30
Joined: Dec 2011
Posts: 1,823
Netherlands
Reconnoiter Offline OP
Serious User
Reconnoiter  Offline OP
Serious User

Joined: Dec 2011
Posts: 1,823
Netherlands
Thanks guys for your tips!

Re: hold key code returns very high value instead of 0 or 1 [Re: Reconnoiter] #469977
12/17/17 16:10
12/17/17 16:10
Joined: Dec 2011
Posts: 1,823
Netherlands
Reconnoiter Offline OP
Serious User
Reconnoiter  Offline OP
Serious User

Joined: Dec 2011
Posts: 1,823
Netherlands
@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();
                                   ...


Re: hold key code returns very high value instead of 0 or 1 [Re: Reconnoiter] #469980
12/17/17 18:29
12/17/17 18:29
Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
txesmi Offline
Serious User
txesmi  Offline
Serious User

Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
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.

Re: hold key code returns very high value instead of 0 or 1 [Re: txesmi] #469983
12/17/17 21:28
12/17/17 21:28
Joined: Dec 2011
Posts: 1,823
Netherlands
Reconnoiter Offline OP
Serious User
Reconnoiter  Offline OP
Serious User

Joined: Dec 2011
Posts: 1,823
Netherlands
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)

Re: hold key code returns very high value instead of 0 or 1 [Re: Reconnoiter] #470105
12/22/17 19:34
12/22/17 19:34
Joined: Dec 2011
Posts: 1,823
Netherlands
Reconnoiter Offline OP
Serious User
Reconnoiter  Offline OP
Serious User

Joined: Dec 2011
Posts: 1,823
Netherlands
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).


Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1