Page 1 of 1

Commander.lua (Merge of command and commando)

Posted: Sun Jun 28, 2020 10:52 pm
by Yoshi021
Hello! Commander.lua is a library aim at helping you read inputs and organize the information easily and conveniently to use!

Documentation
As of now, commander can read inputs from player inputs and keyboard inputs.

Creating a command key
To create a command key, you simply use the commmander.register function. You must supply a table like this:

Code: Select all

local command = require("command")

-- Register your keys
command.register{
  [1] = { -- This represents the command keys for input 1
    up ={ID = 1, name = "up"},  -- You can now access this input by doing commander[1].up
    down = {ID = 1, name = "down"},    
    left = {ID = 1, name = "left"},
    right = {ID = 1, name = "right"},
    jump = {ID = 1, name = "jump"},  
    dash = {ID = 1, name = "run"},   -- You can now access this input by doing commander[1].dash. Note how its inputs are based off the run key.
    pause = 32,  -- 32 is the vk code for the space key
    },
    [2] = {  -- This represents the command keys for input 2
      up = {ID = 2, name = "up"},  -- You can now access this input by doing commander[2].up
      down = {ID = 2, name = "down"},    
      left = {ID = 2, name = "left"},
      right = {ID = 2, name = "right"},
      jump = {ID = 2, name = "jump"},
      dash = {ID = 2, name = "run"},
      pause = 20,  -- 20 is the vk code for the caps lock key    
    },
}
How to register a player key

Code: Select all

  -- name has to be "up", "down", "left", "right", "run", "altRun", "jump", "altJump", "dropItem", "pause"
  myKey = {name = "up"}  -- This is the bare minimum for a player key. It will assume player 1 inputs
  myKey = {name = "up, ID = 1}  -- Use the ID field to specify which player you want the inputs from. Use 1 for players, and 2 for player 2
  myKey = {name = "up", ID = 1, mode = "keys"} -- By default mode is "rawKeys". Set the mode to "keys if you want to. The difference is that keys is affected when disabling player inputs and note that run is activated most of the time when pressing altRun.
  myKey = {name = "up", ID = 1, mode = "keys", type = commander.TYPE_PLAYERKEY} -- You can use type to specify that its a player me, but by default its assumed to be.
How to register a keyboard key

Code: Select all

  myKey = 32  -- Just putting a number will make the script assume its a keyboard key
  myKey = {ID= 32, type= TYPE_KEYBOARD}  -- You can also specify a keyboard key like this
Using a commander key
A commander key has four different keys you can access.
state To be used with KEYS_PRESSED, KEYS_RELEASED, KEYS_DOWN, KEYS_UP.
time Measures how long a key has been held or released. Counts up when pressed, and counts down when released. Gets set to 1 or -1 when a key is pressed or released respectively. Example:

Code: Select all

  local v = commander[1].myCustomKey.time
  v == 30  -- if the key is held for exactly 30 frames
  v > 30  -- if the key is held for longer 30 frames
  v == -30  -- if the key is not held for exactly 30 frames
  v < -30  -- if the key is not held for longer 30 frames
  v == 1   -- Equivalent to checking if a key is KEYS_PRESSED
  v == -1  -- Equivalent to checking if a key is KEYS_RELEASED
prevstate The state of the key in the previous frame
time The time in the previous frame



Download
Commander.lua



TL;DR this library allows you to get standard SMBX inputs, give it a nickname and get the state, and how long the key was held and released.
Please report any bugs you find and feel free to leave suggestions!

Re: Command.lua and Commando.lua

Posted: Mon Jun 29, 2020 3:03 am
by arhi998
Nice! Could be very useful for beginners too. (like me)

Re: Command.lua and Commando.lua

Posted: Mon Jun 29, 2020 7:19 am
by Taycamgame
These are both certainly going to be useful, though I wouldn't be surprised if people typically make more use of Commando due to the added functionality of allowing any key to be an input!

Re: Command.lua and Commando.lua

Posted: Mon Jun 29, 2020 10:55 am
by Novarender
So did you fix the issue of not being able to detect key releases from non-player inputs?

Re: Command.lua and Commando.lua

Posted: Mon Jun 29, 2020 12:16 pm
by Yoshi021
Yes!

Re: Commander.lua (Merge of command and commando)

Posted: Thu Jul 02, 2020 10:16 am
by Yoshi021
I merged both libraries into one. Now you don't have to pick one over the other!