I was reading through an open-sourced sky-diving script when I came across this:
if Key:byte() == 119 or Key:byte() == 17 then -- W
TiltFB = -1
GlideFB = 2
Drag = DragRatio*(0.997+DragDiminish)
if IsFalling.Value == true then
I’ve never seen anything related to “Byte” before in scripting, what is it? What are the numbers? Do they detect certain keypresses? If so how? Is it only exclusive to KeyDown? So many questions, so little answers.
From what I can remember, a byte is a succession of (typically) 8 bits used to display a value. Bits are single binary digits, either a 1 or a 0.
A byte can portray 256 unique values (2 to the power 8 or 2^8).
Also, this may not be true, but a friend once told me: Lua strings are explained on a per-byte level, meaning you can make use of the full 0-255 range, and a Lua string will keep that information and read it back as singular bytes.
Yep, they are numbers that in this case represent the 01001 that the keyboard sends to the computer to represent characters like “w” and yeah when this key is equal to 119 that represents “W” from the Keycode Enum documentation.
The KeyCode Enum contains a list of Byte keycodes that represent the button used in a user input.
Note, KeyCode keyboard values refer to the physical position of buttons on a standard QWERTY keyboard. This means that, provided the user’s system is configured correctly, the location of keys (such as WASD) will remain the same on other keyboard types, such as Dvorak keyboards (where it would map to ,AOE).
Also it’s not exclusive to keydown, you can also use the newer super UserInputServices to obtain the keycode through the input object property returned by the InputBegan and InputEnded.
An expression like o:foo(x) is just another way to write o.foo(o, x) , that is, to call o.foo adding o as a first extra argument. In Chapter 16 we will discuss such calls (and object-oriented programming) in more detail.
local s = "ABCDE"
s:byte(3,4) -- can apply directly to string variable
For the question of why not just use an Enum? I believe it may be due to historical reasons before UserInputServices existed but yeah it’s easier and more readable hence efficient to put down the Enum code and it should work for that specific letter.