Jump to content
  • entries
    15
  • comments
    50
  • views
    24,960

Key Configs and Tedium


Einlander

3,805 views

 Share

This post was inspired by this forum thread http://www.leadwerks.com/werkspace/topic/8942-keys-customisation-shortcut/ by YouGroove

 

A little background of how a concocted this solution. I run a bunch of Left 4 Dead 2 servers and I make mods for them using sourcemod. I find myself having to make special keyconfigs when administrating or adding stuff to the levels. In the process you find out that all actions in the source engine are abstracted. So I adapted that to Leadwerks.

 

When most people program they hard code values for input for thier games. This leads to easy development but later they come up with odd solutions to solve the lack of configuration flexibility.

 

[Abstraction]

Some game engines have most everything abstracted away. They refer to animations by name instead of frame range (playercrouch vs playanimtionframes[1,200]) , game events by name instead of number ("hunterpouncebegin" vs action 12345) and player input by name instead of of keycode (player_jump vs Key.Space).

 

The developer defines all the possible input the player can take and gives them a name. Then they apply a keycode to the name. This method allows them to change the actions value at anytime they choose.

 

Psudeocode:

Dim Player_Action_Jump as integer 'Create action
Player_Action_Jump = Keys.Space 'assign keycode to action
If ME.Keydown = Player_Action_Jump then 'detect if the action was taken
Jump() ' do something
End If

Please excuse the vb.net code thats what i'm most use to

 

So now Applying it to Leadwerks:

 

[Application]

We will be using the Fpsplayer.lua script and another script that we create.

 

First we Figure out what key inputs the script takes, and that would be:

 

Forward
Backwards
Strafe Left
Strafe Right
Jump
Flashlight
Use
Reload
Crouch -- this one is comented out but I included it anyway
Run

 

Taking this list of actions we create another script that will hold all the actions and key configs.

 

Script.L_Forward
Script.L_Backwards
Script.L_StrafeLeft
Script.L_StrafeRight
Script.L_Jump
Script.L_Flashlight
Script.L_Use
Script.L_Reload
Script.L_Crouch
Script.L_Run

 

The first thing I did was make them all string inputs. But soon I discovered that you couldn't write a shift in an input box, and space would confuse someone. So I made them each a drop down box where you can select the key.

 

Script.L_Forward =22 --choice "Forward" "UNASSIGNED,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,NUMPAD0,NUMPAD1,NUMPAD2,NUMPAD3,NUMPAD4,NUMPAD5,NUMPAD6,NUMPAD7,NUMPAD8,NUMPAD9,NUMPADPERIOD,NUMPADDIVIDE,NUMPADMULTIPLY,NUMPADSUBTRACT,NUMPADADDITION,BACKSPACE,CAPSLOCK,LCONTROL,DELETE,DOWN,END,ENTER,HOME,INSERT,LEFT,NUMLOCK,PAGEDOWN,PAGEUP,RCONTROL,RIGHT,SHIFT,SPACE,SUBTRACT,TAB,UP,EQUALS,OPENBRACKET,BACKSLASH,SEMICOLON,QUOTES,COMMA,SLASH,PERIOD"

 

I had to look all the keys up dry.png and then write them all down and make sure i didn't misspell any of them. Also I had to remove some keys from the list -- tilda[~] escape [esc] and the windows key --.

 

blogentry-7960-0-99500100-1394761483.jpg

 

Now that you have the selectors for the keys you need to assign the keycode to a global action variable.

 

 

NOTE: lua does not have select case switches sad.png

 


function Script:Start()
-- Assign key values to standard movement globals
--LW_ stands for Leadwerks  or local world
LW_Forward =self:AssignKeyFromChoice(self.L_Forward)
LW_Backwards =self:AssignKeyFromChoice(self.L_Backwards)
LW_StrafeLeft =self:AssignKeyFromChoice(self.L_StrafeLeft)
LW_StrafeRight =self:AssignKeyFromChoice(self.L_StrafeRight)
LW_Jump =self:AssignKeyFromChoice(self.L_Jump)
LW_Flashlight =self:AssignKeyFromChoice(self.L_Flashlight)
LW_Use =self:AssignKeyFromChoice(self.L_Use)
LW_Reload =self:AssignKeyFromChoice(self.L_Reload)
LW_Crouch =self:AssignKeyFromChoice(self.L_Crouch)
LW_Run =self:AssignKeyFromChoice(self.L_Run)
-- More standard key binds will be added later
end

function Script:AssignKeyFromChoice(KeyAsChoice)
-- Looked it up, Lua DOES NOT have select case 
if KeyAsChoice ==0 then return 0 end
if KeyAsChoice ==1 then return Key.A end
if KeyAsChoice ==2 then return Key.B end
if KeyAsChoice ==3 then return Key.C end
if KeyAsChoice ==4 then return Key.D end
if KeyAsChoice ==5 then return Key.E end
if KeyAsChoice ==6 then return Key.F end
if KeyAsChoice ==7 then return Key.G end
if KeyAsChoice ==8 then return Key.H end
if KeyAsChoice ==9 then return Key.I end
:
:Rest of code
:
end

 

That's the code that handles user input. Make a pivot in the level then attach the script to it.

 

Now this part is simple

 

Go through the fps player script and change all the keycodes to your actions

 

if self.weapon~=nil then
if window:KeyHit(Key.R) then
self.weapon:Reload()
end
end
BECOMES:
if self.weapon~=nil then
if window:KeyHit(LW_Reload) then
self.weapon:Reload()
end
end

 

And so on.

 

Now you can change any of the keys to what to what you want at anytime

 

The only shortcomings is this does not support mouse commands. In a future version I might be able to write an input function that reads both the mouse and keyboard commands.

 

Edit:

Here Is this idea in action. The Fps player is modified to look for a variable containing the keycodes, and the other script holds the keycodes and lets you change them

 

http://www.leadwerks.com/werkspace/files/file/515-key-config-host/

  • Upvote 1
 Share

2 Comments


Recommended Comments

Great.

But like Rick said for keycodes, perhaps you can directly use :

if window:KeyHit(variableKeyStored) then ...

 

If this is possible it should even more shorten your code

without having ot call the function AssignKeyFromChoice, something to test.

Link to comment

That is what I did, but i added all the keycodes so i would never have to look them up again.

 

So I have a variable, and a function that will look up the keycode for me. and store it in that variable. With the script that I made, I will never have to manually look up the keycodes. I just take the script, assign it to a pivot and everything is finished. If i want to jump i just use window:KeyHit(LW_Jump) and if i want to change the key, i just use the box and change it.

 

I recommend taking a look at the linked scripts, they are an implementation of the ideas in that thread.

Link to comment
Guest
Add a comment...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...