Holding arrow key down

Posted By: Ruben

Holding arrow key down - 02/10/18 15:43

I have an inventory transfer system in place, in case the player wants to loot a dead body, or search a treasure chest, both the player's and the other entity's inventory bags display on the screen, and the player can click and drag items from one inventory bag to the other. When this happens, the game will ask the player how much of the item in question the player wants to transfer, and it will show the maximum amount of that item that can be transferred at that time. The player can then press the down arrow to decrease the amount, or the up arrow to increase the amount.

However, lets say there is a lot of the item in question to transfer. Lets say there is 100 of them. As my game program is right now, the player would have to push the up or down arrow key one button push at a time each time to make the counter increment or decrement by one. I really want to make it so that the player can hold down the down or up arrow key to change the amount automatically and faster.

This is what I have so far:

Code:
void increaseQntTransfer()
{
   itm_qnt_ent_from += 1;
	
   if(itm_qnt_ent_from > maxItmQntEntFrom) // KEEPS ITEMS BEING
      //    TRANSFERRED FROM GOING OVER MAXIMUM AMOUNT.
   {
      itm_qnt_ent_from -= 1;
   }
}

void decreaseQntTransfer()
{
   itm_qnt_ent_from -= 1;
	
   if(itm_qnt_ent_from < 0) // KEEPS ITEMS BEING TRANSFERRED FROM
      //    GOING BELOW ZERO.
   {
      itm_qnt_ent_from += 1;
   }
}		

void transferProcess()
{	
   ...

   on_cuu = increaseQntTransfer;
   on_cud = decreaseQntTransfer;

   ...
}



I tried putting a while(on_cud) loop around the...
Code:
itm_qnt_ent_from -= 1;


...in the decreaseQntTransfer() function, with a wait(1) at the end, but when I pressed the down arrow to reduce the amount to transfer, it just kept reducing and would not stop, even when I took my finger off the down arrow key.

I am afraid of creating an infinite loop. Can anyone give me advice on how to make it so that the player can press and hold down the up or down arrow key to make the amount to transfer change faster, and then if the player takes the finger off of these keys, the amount to transfer stops changing, and remains where the number has left off?
Posted By: DriftWood

Re: Holding arrow key down - 02/10/18 21:48

Look at Keys.c and ask yourself how many times the on_cud is starting a new instance of that function. In one second it could fire 60 instances.
Simply use a time to limit it to once every 1/2 a second.
Look at examples from the past in the forum about keys and time limit. --Hint combo keys post.
Look at proc_kill.
As a final step some one will most likely fix it minutes for money.
Posted By: Ruben

Re: Holding arrow key down - 02/11/18 05:49

Thank you DriftWood. Thanks to you I learned the difference between on_cud and key_cud, and I solved my issue. I really appreciate it.
© 2024 lite-C Forums