GetPlayerInput: Preventing holding down a certain key?

Archive of the old editing forum
Forum rules
Before asking on how to use a ZDoom feature, read the ZDoom wiki first. This forum is archived - please use this set of forums to ask new questions.
Locked
User avatar
Nash
 
 
Posts: 17439
Joined: Mon Oct 27, 2003 12:07 am
Location: Kuala Lumpur, Malaysia
Contact:

GetPlayerInput: Preventing holding down a certain key?

Post by Nash »

I have a custom player movement, like so:

Code: Select all

// custom player movement
script 501 enter
{
   int angle, pitch, velx, vely, velz;
   int velocity;

   while(1)
   {

      angle = GetActorAngle(0);
      pitch = GetActorPitch(0);

      int buttons = GetPlayerInput(-1, INPUT_BUTTONS);
	  
	  // on ground movement

      if(buttons & (BT_FORWARD + BT_BACK + BT_MOVELEFT + BT_MOVERIGHT))
		Thing_Stop(0);

      if((buttons & (BT_FORWARD + BT_BACK)) && (buttons & (BT_MOVELEFT + BT_MOVERIGHT)))
         velocity = sin(0.125) + playerspeedmult;
      else
         velocity = 1.0 + playerspeedmult;

      // forward
      if (buttons & BT_FORWARD)
      {
		  SetActorVelocity(0, FixedMul(velocity, cos(angle)), FixedMul(velocity, sin(angle)), 0, TRUE, FALSE);
      }

      // backwards
      if (buttons & BT_BACK)
      {
		  SetActorVelocity(0, -FixedMul(velocity, cos(angle)), -FixedMul(velocity, sin(angle)), 0, TRUE, FALSE);
      }

      // left
      if (buttons & BT_MOVELEFT)
      {
		  SetActorVelocity(0, FixedMul(velocity, cos(angle + 0.25)), FixedMul(velocity, sin(angle + 0.25)), 0, TRUE, FALSE);
      }

      // right
      if (buttons & BT_MOVERIGHT)
      {
		  SetActorVelocity(0, FixedMul(velocity, cos(angle - 0.25)), FixedMul(velocity, sin(angle - 0.25)), 0, TRUE, FALSE);
      }

   delay(1);
   }
}
... and then, I have the following script that monitors for the player to press the +use key, which will then execute the item pickup code. This is similar to the system used in Oblivion where, you point your crosshair on to the item in the game world, then pressing +use will add the item into your inventory.

Code: Select all

// check if player hits use key and has valid itemtopickup
script 667 enter
{
	while(1)
	{
		// retrieve button input
		int buttons = GetPlayerInput(-1, INPUT_BUTTONS);

		// first, make sure there is an item under the crosshair and the item still exists
		if (itemtopickup[PlayerNumber()] && ThingCount(T_NONE, itemtopickuptid[PlayerNumber()]))
		{
			// now press use to pick up item!
			if ((buttons == BT_USE) && !pressonce[PlayerNumber()])
			{
				log(s: "You take the ", s: itemnames[itemtopickup[PlayerNumber()]]);

				// now remove item from game world
				Thing_Destroy(itemtopickuptid[PlayerNumber()]);

				// prevent holding down the use key
				pressonce[PlayerNumber()] = 1;
			}

		}

		// reset pressonce if not pressing use
		if ((buttons != BT_USE))
		{
			pressonce[PlayerNumber()] = 0;
		}

	delay(1);
	}
}
The problem now is... if I am moving and trying to pick up an item at the same time (that means I have movement keys held down while pressing the +use key), the item pickup won't work. I have to release all movement buttons first before picking up an item.

I tried changing this line...

Code: Select all

			// now press use to pick up item!
			if ((buttons == BT_USE) && !pressonce[PlayerNumber()])
to...

Code: Select all

			// now press use to pick up item!
			if ((buttons & BT_USE) && !pressonce[PlayerNumber()])
... but it doesn't work as expected: I can finally move and pick up items at the same time, BUT it negates the code to prevent the key from being held down, meaning: I can just hold down the +use key and move around, the player will automatically pick up anything that goes under the crosshair, sweeping the area very easily. :P

How do I solve this problem?
User avatar
AFADoomer
Posts: 1327
Joined: Tue Jul 15, 2003 4:18 pm
Contact:

Re: GetPlayerInput: Preventing holding down a certain key?

Post by AFADoomer »

I looks like you have your pressonce variable being reset in the same tic as it's being checked... So you pick up an item, pressonce is set, then you check and reset it, then you loop... So it behaves the same as if it wasn't even set. You probably need to move that check somewhere else, or add a longer delay before it's reset.
User avatar
Nash
 
 
Posts: 17439
Joined: Mon Oct 27, 2003 12:07 am
Location: Kuala Lumpur, Malaysia
Contact:

Re: GetPlayerInput: Preventing holding down a certain key?

Post by Nash »

Adding a delay before the reset stuff doesn't really solve it... the use key will still be retriggered if I start moving - and then releasing the movement keys - while the use key was held down...
User avatar
Nash
 
 
Posts: 17439
Joined: Mon Oct 27, 2003 12:07 am
Location: Kuala Lumpur, Malaysia
Contact:

Re: GetPlayerInput: Preventing holding down a certain key?

Post by Nash »

In case no one understands my problem, let me try and simplify it:

Basically I have 2 sections of code involving GetPlayerInput, one involves movement keys, the other involves the +use key.

I want something to happen when the +use key isn't pressed.

Problem is, releasing the movement keys triggers the above.

How do I totally isolate the code involving the +use key?
User avatar
Isle
Posts: 687
Joined: Fri Nov 21, 2003 1:30 am
Location: Arizona, USA

Re: GetPlayerInput: Preventing holding down a certain key?

Post by Isle »

you don't need to worry about what another script is doing with getplayerinput.
a) never do (buttons == BT_USE), (buttons & BT_USE) will check only that button and ignore the state of the other ones
b) press and release is what INPUT_OLDBUTTONS is for. it returns the buttons held a tic ago.

Code: Select all

         // now press use to pick up item!
         if((GetPlayerInput(0, INPUT_BUTTONS) & BT_USE) && !(GetPlayerInput(0, INPUT_OLDBUTTONS) & BT_USE))
         {
            log(s: "You take the ", s: itemnames[itemtopickup[PlayerNumber()]]);

            // now remove item from game world
            Thing_Destroy(itemtopickuptid[PlayerNumber()]);
         }
then take out

Code: Select all

      // reset pressonce if not pressing use
      if ((buttons != BT_USE))
      {
         pressonce[PlayerNumber()] = 0;
      }
User avatar
Nash
 
 
Posts: 17439
Joined: Mon Oct 27, 2003 12:07 am
Location: Kuala Lumpur, Malaysia
Contact:

Re: GetPlayerInput: Preventing holding down a certain key?

Post by Nash »

Again, many thanks. To be honest, the entire "bits" business utilized by GetPlayerInput is just really confusing for me ATM. :S

I was thinking a set of wrapper functions for commonly used applications from player input would help save a lot of time and headache... :D
User avatar
Zippy
Posts: 3302
Joined: Wed Mar 23, 2005 5:31 pm
Location: New Jersey

Re: GetPlayerInput: Preventing holding down a certain key?

Post by Zippy »

Nash wrote:Again, many thanks. To be honest, the entire "bits" business utilized by GetPlayerInput is just really confusing for me ATM. :S

I was thinking a set of wrapper functions for commonly used applications from player input would help save a lot of time and headache... :D
I remember I wrote up something on it awhile ago...

Here it is.
Locked

Return to “Editing (Archive)”