Help with your scripting

"and" not working for two keys down

"and" not working for two keys down

Postby CreativeOcclusion » 19 Apr 2016, 03:05

It seems that the "and" statement is not working for 2 keys down at the same time...Unless I have the syntax wrong or something...Thanks for any help....Here is a code sample that is not working...This is in a function onKeyDown(key)...

Code: Select all
   if (key == "shift" and key == "w") then
      sound.play2DSound( runsound_ID );
      anim.setSpeed(obj, "Walk", 2.8);
   end
Thanks, CreativeOcclusion
User avatar
CreativeOcclusion
Skyline Warrior
 
Posts: 366
Joined: 22 Jun 2015, 19:34
Location: Texas

Re: "and" not working for two keys down

Postby PaRaDoXoS » 19 Apr 2016, 07:02

onKeyDown(key) sends an event immediatelly when a key is pressed. This means "shift" and "w" are triggered seperately. Even if not your code requires "shift" == "w", which will obviously always return false.
Solution:
Code: Select all
if (key == "w" and input.keyDown("shift")==1)) then

if you want multiple keys with shift and only mondify e.g. speed by the shift-key this saves you some lines of code and code doubling (haven't gone into movement, so the example is repositioning)
Code: Select all
if (string.find("ijkluo",key) > 0 and not key == "") then
   sky.lprint("DEBUG: moveObj");
   sky.lprint("DEBUG: shift: "..input.keyDown("shift"));
   if (input.keyDown("shift") == 1) then
      d = d/20;
   end
   if (key == "i") then
      x,y,z = entity.getPosition(eid); -- eid=0 -> crash
      y = y+d;
      entity.setPosition(eid,x,y,z);
   elseif (key == "j") then
.....
   end
end


Unfortunately, all this will not work with numbers.
Code: Select all
if (key == "0") then
   if (input.keyDown("shift")==1) then
      sky.lprint("THIS IS UNREACHABLE");
   end
end

The reason can be seen with Debug-Messages:
Code: Select all
sky.lprint("DEBUG: keyPressed("..key..")");
shift+0 will NOT trigger key="0", but key="".

Unfortunately the following won't work, too. input.keyDown("0") does work, but only if pressed without a modifier key. First "0" then Shift has keyDown("0")==1, but obviously that triggers a 'plain' "0" first, which is unusable for hotkeys.
Code: Select all
if (key == "" and input.keyDown("shift")==1 and input.keyDown("0")==1) then


Hope this helps with your problem(s).
PhenomII 2,8GHz, GeForce9600GT, 16GB DDR3, SSD, old but still works
User avatar
PaRaDoXoS
Skyline Apprentice
 
Posts: 29
Joined: 17 Mar 2016, 07:54
Location: Germany

Re: "and" not working for two keys down

Postby SolarPortal » 19 Apr 2016, 11:15

As PaRaDoXos has mentioned, only 1 key is sent through at once. To get around this, when the shift key is pressed or released, store a variable for isShiftPressed.

e.g.
Code: Select all
isShiftPressed = false;
function onKeyDown( key )
     if(key == "shift")then isShiftPressed = true; end

     if(key == "w" and isShiftPressed) then
          -- set the run speed
     end
end

function onKeyUp( key )
     if(key == "shift")then isShiftPressed = false; end
end


well, thats how i would approach this, the only problem is that the w key will not be retriggered if it is held down and the shift key is released or pressed. Which is why movement we tend to handle in the update loop to adapt the speeds on the keys pressed.

All methods are valid i think :)
Skyline Game Engine - Lead Dev.
Please provide as much info as possible when asking for help.


Specs: OS: Win 10 64bit, CPU: Intel i7 4770 3.4ghz x 4 core(8 threads), GPU: Nvidia GTX 1060 6GB, Ram: 16gig DDR3, Windows on 250gb Samsung Evo 860

Twitter: @SolarPortal
Instagram: @SolarPortal
User avatar
SolarPortal
Skyline Founder
Skyline Founder
 
Posts: 3631
Joined: 29 Jul 2012, 15:56
Location: UK
Skill: 3D Modeller
Skill: 2D Artist
Skill: Programmer
Skill: Level Designer

Re: "and" not working for two keys down

Postby CreativeOcclusion » 19 Apr 2016, 18:56

Which is why movement we tend to handle in the update loop to adapt the speeds on the keys pressed.


i will just end up moving this to the onUpdate()....The main problem I'm having is that These adjustments are being made to the FPS Kit Player...The micro-script for this player contains none of the movement code or keys code...The shift and w key work fine together for the FPS Player...

I am just adding footsteps sounds for run and walk, and changing the animation speed for the walk animation...The actual movement is controlled from somewhere unseen...If this code was visible to me I could easily manipulate it to do what I want, without having to guess at it....I can find no code attached to the character controller...I may end up just having to code my own character controller....Thanks for the help, @PaRaDoXoS and @SolarPortal...
Thanks, CreativeOcclusion
User avatar
CreativeOcclusion
Skyline Warrior
 
Posts: 366
Joined: 22 Jun 2015, 19:34
Location: Texas

Re: "and" not working for two keys down

Postby SolarPortal » 19 Apr 2016, 19:21

i would wait until the new FPS player before writing a new one :)
Skyline Game Engine - Lead Dev.
Please provide as much info as possible when asking for help.


Specs: OS: Win 10 64bit, CPU: Intel i7 4770 3.4ghz x 4 core(8 threads), GPU: Nvidia GTX 1060 6GB, Ram: 16gig DDR3, Windows on 250gb Samsung Evo 860

Twitter: @SolarPortal
Instagram: @SolarPortal
User avatar
SolarPortal
Skyline Founder
Skyline Founder
 
Posts: 3631
Joined: 29 Jul 2012, 15:56
Location: UK
Skill: 3D Modeller
Skill: 2D Artist
Skill: Programmer
Skill: Level Designer


Return to Lua Scripting

Who is online

Users browsing this forum: No registered users and 3 guests