ACSUtils Wiki

An ACS library for ZDoom-based ports

User Tools

Site Tools


new:input

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
new:input [2018/02/16 23:12] korshunnew:input [2018/02/17 14:10] (current) korshun
Line 1: Line 1:
-====== Input functions ======+====== Input library ======
  
 ACSUtils provides a set of wrappers for [[zdoom>GetPlayerInput]] to make writing correct input code easier. ACSUtils provides a set of wrappers for [[zdoom>GetPlayerInput]] to make writing correct input code easier.
Line 5: Line 5:
 ===== Basic concepts ===== ===== Basic concepts =====
  
-Every key is either **up** or **down** at any time.+  * Every key is either **up** or **down** at any time
 +  * If the key was up the previous tic, but is now down, it is said to have been **pressed**. 
 +  * If the key was down the previous tic, but is now up, it is said to have been **released**. 
 +  * If the key has been pressed or released, it is said to have been **toggled**.
  
-If the key was **up** the previous tic, but is now **down**, it is said to have been **pressed**.+ACSUtils provide a way to easily check these all these states for every key without writing compliated ''GetPlayerInput'' expressions.
  
-If the key was **down** the previous tic, but is now **up**, it is said to have been **released**.+===== What'the point of "pressed" and "released"? =====
  
-If the key has been **pressed or released**it is said to have been **toggled**.+Sayyou have an on-screen button to sell items that can be [[mouse|clicked with mouse]]:
  
-ACSUtils provide a way to easily check these all these states for every key without writing compliated ''GetPlayerInput'' expressions.+<code> 
 +if (/* check that mouse cursor is inside the button */) 
 +    if (GetPlayerInput(-1, INPUT_BUTTONS) & BT_ATTACK)) // check left mouse button click 
 +        SellItem(); 
 +</code>
  
-===== What's the point of "pressed" and "released"? =====+**This code is wrong.** If the player clicks the button once, it will likely cause him to sell multiple items. That'because this code checks if the mouse button is **down**. But the mouse button may be down for multiple tics after a click, as it may not be immediately released
 + 
 +To avoid this bug, you need to check if the button was up the previous tic, but is now down. And that's exactly what the **pressed** state means. The ''KeyPressed'' function contains all the repetitive code to perform this check: 
 + 
 +<code> 
 +if (/* check that mouse cursor is inside the button */) 
 +    if (KeyPressed(BT_ATTACK)) // check left mouse button click 
 +        SellItem(); 
 +</code> 
 + 
 +Now the button works correctly. Performing an action on releasing the button can be done the same way using ''KeyReleased''.
  
 ===== Functions ===== ===== Functions =====
Line 32: Line 49:
  
 <code> <code>
-if (KeyPressed(BT_FORWARD)) +if (KeyPressed(BT_ATTACK)) 
-    print(s:"You just pressed W");+    print(s:"You clicked left mouse button");
 </code> </code>
  
Line 64: Line 81:
  
 <code> <code>
-if (KeyDown(BT_FORWARD | BT_BACK | BT_MOVELEFT | BT_MOVERIGHT))+if (KeyDownAny(BT_FORWARD | BT_BACK | BT_MOVELEFT | BT_MOVERIGHT))
     print(s:"You are trying to move.");     print(s:"You are trying to move.");
 </code> </code>
  
  
-To use these wrappers on players other than the activator, prefix any function with ''Player'' and add player number as the first argument:+To use these wrappers on players other than the activator, prefix any of the above functions with ''Player'' and add player number as the first argument:
  
   * ''bool PlayerKeyUp(int player, int key)''   * ''bool PlayerKeyUp(int player, int key)''
new/input.1518815534.txt.gz · Last modified: 2018/02/16 23:12 by korshun