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.

GetPlayerInput: Preventing holding down a certain key?

Postby Nash » Mon May 09, 2011 5:19 am

I have a custom player movement, like so:

Code: Select allExpand view
// 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 allExpand view
// 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 allExpand view
         // now press use to pick up item!
         if ((buttons == BT_USE) && !pressonce[PlayerNumber()])


to...

Code: Select allExpand view
         // 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
Nash
Twitter/Facebook/Youtube: nashmuhandes
 
 
 
Joined: 27 Oct 2003
Location: Kuala Lumpur, Malaysia
Twitch ID: nashmuhandes
Github ID: nashmuhandes

Re: GetPlayerInput: Preventing holding down a certain key?

Postby AFADoomer » Mon May 09, 2011 6:07 am

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
AFADoomer
 
Joined: 15 Jul 2003

Re: GetPlayerInput: Preventing holding down a certain key?

Postby Nash » Mon May 09, 2011 6:17 am

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
Twitter/Facebook/Youtube: nashmuhandes
 
 
 
Joined: 27 Oct 2003
Location: Kuala Lumpur, Malaysia
Twitch ID: nashmuhandes
Github ID: nashmuhandes

Re: GetPlayerInput: Preventing holding down a certain key?

Postby Nash » Mon May 09, 2011 11:03 am

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
Nash
Twitter/Facebook/Youtube: nashmuhandes
 
 
 
Joined: 27 Oct 2003
Location: Kuala Lumpur, Malaysia
Twitch ID: nashmuhandes
Github ID: nashmuhandes

Re: GetPlayerInput: Preventing holding down a certain key?

Postby Isle » Mon May 09, 2011 1:48 pm

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 allExpand view
         // 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 allExpand view
      // reset pressonce if not pressing use
      if ((buttons != BT_USE))
      {
         pressonce[PlayerNumber()] = 0;
      }
User avatar
Isle
WadAuthor 4 L1F3
 
Joined: 21 Nov 2003
Location: Arizona, USA

Re: GetPlayerInput: Preventing holding down a certain key?

Postby Nash » Mon May 09, 2011 2:00 pm

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
Nash
Twitter/Facebook/Youtube: nashmuhandes
 
 
 
Joined: 27 Oct 2003
Location: Kuala Lumpur, Malaysia
Twitch ID: nashmuhandes
Github ID: nashmuhandes

Re: GetPlayerInput: Preventing holding down a certain key?

Postby Zippy » Mon May 09, 2011 4:29 pm

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.
User avatar
Zippy
Your Golden Boy
 
Joined: 23 Mar 2005
Location: New Jersey
Discord: Zurock#4834


Return to Editing (Archive)

Who is online

Users browsing this forum: No registered users and 2 guests