Jump to content

window:KeyHit strange problem


tipforeveryone
 Share

Recommended Posts

Today I found a strange problem with window:KeyHit

I put this code inside an if statement

 

if self.player_Inside_Room == true then
if window:KeyHit(Key.H) then
System:Print("Yay!")
end
end

 

I create a CSG box with collision type = Trigger, it will set self.player_Inside_Room = true when player collides, after that, I can press H to display "Yay!"

 

Strange thing is: I press and release H before entering the trigger but when I enter that box, the debug console displays "Yay!". It doesn't matter how long I have pressed H key before

 

How can I solve this? I just want to execute something only when I get inside the trigger and hit a key

Link to comment
Share on other sites

Sound like your keys are stored untill usage. You probably just have to use flush keys:

http://www.leadwerks.com/werkspace/page/api-reference/_/window/windowflushkeys-r903

 

I thought about this function once, but I wonder will it flush every keyboard interaction, include KeyDown if I put this in UpdateWorld ?

 

....

Oh I have tried FlushKeys(), put it in UpdateWorld() then I can not use my AWSD key anymore :"D

 

can you suggest me how to use this to solve my KeyHit problem ?

Link to comment
Share on other sites

I'm not sure why you need the self.player_Inside_Room variable without seeing the script but I'd probably do something like:

 

function Script:Collision(entity, position, normal, speed)

if entity:GetKeyValue("type") == "player" then

self.player_Inside_Room = true

else

self.player_Inside_Room = false

end

if self.player_Inside_Room == true then

if window:KeyHit(Key.H) then

System:Print("Yay!")

end

end

end

 

The variable seems redundant so I'd probably do:

 

function Script:Collision(entity, position, normal, speed)

if entity:GetKeyValue("type") == "player" then

if window:KeyHit(Key.H) then

System:Print("Yay!")

end

end

end

---

Scott

 

Using Windows 7 Ultimate 64 bit/Core I7-2700K @ 4312mhz/24G RAM/Nvidia GTX 1060

Link to comment
Share on other sites

*sigh. stupid formatting is gone again.

 

Found the solution:

 

You can test it with this script.

  • Make a new scene.
  • Add this script to a directional light or object.
  • Press H, nothing happens.
  • Press Q, and you will see that the system print from the H if statement is triggered too.
  • Now add the flushkeys on main.lua
  • Now run again
  • press Q, only the message from the Q if statement is shown
  • Press H and only now the message from H is shown

Script.enabled = false
function Script:UpdateWorld()
local window = Window:GetCurrent()
if window:KeyHit(Key.Q) then
System:Print("h key testing enabled")
self.enabled = true
end
if self.enabled then
if window:KeyHit(Key.H) then
System:Print("hello")
end
end
end

 

And the flushkeys needs to be between the main loop world update en world render.

 

world:Update()
window:FlushKeys()
world:Render()

Link to comment
Share on other sites

@Aggror: Yes this will solve the problem of window:KeyHit but affect window:KeyDown too, I use ASWD for character movement and they are flushed too, make my character unmovable. So that I think it is not good to put window:Keyflush() anywhere.

Is there any way to flush only one key ?

 

@Rick

I avoid the Hit functions generally and use the Down functions with my own Boolean for that specific case instead. The Hit functions have a few "quarks" that I don't like and often burn me.

I have a same thinking now sad.png I may combine window:KeyDown with some bool variable

 

@SGB: I want a key to execute an unique function only in specified condition(this case is player is inside a room), when player is outside the room, that key will do another task when hit.

 

Ex:

Press H in room 1 ~> display "Rick"

Press H in room 2 ~> display "Josh"

Press H in room 3 ~> display "SGB"

etc...

 

one key can execute diffirent tasks in a specified condition, so that I need to put window:KeyHit under an if statement

If I put if statement inside if window:KeyHit(). It does not work

 

this code does not work

if window:KeyHit(Key.H) then
if self.room == "Room1" then ... end
if self.room == "Room2" then ... end
if self.room == "Room3" then ... end
end

 

this does but if I use this, I will have problem which I describe in the first entry of this topic

if self.room == "Room1" then
if window:KeyHit(Key.H) then *dosomething* end
end
if self.room == "Room2" then
if window:KeyHit(Key.H) then *dosomething* end
end
if self.room == "Room3" then
if window:KeyHit(Key.H) then *dosomething* end
end

Link to comment
Share on other sites

this code does not work

if window:KeyHit(Key.H) then
if self.room == "Room1" then ... end
if self.room == "Room2" then ... end
if self.room == "Room3" then ... end
end

This is the correct way. You check for key exactly one time every update and only then check other conditions. If this is not working then the mistake is in other part of your code which we don't see.

Maybe you don't check for trigger collision if key isn't hit? Those two checks should be independent.

 

If you need to flush one key you just get it's state:


window:KeyHit(Key.H)

H key is flushed until next update.

  • Upvote 2
Link to comment
Share on other sites

Was going to comment essentially the same as Genebris. The shown code that you say does not work should work as it does for me. If it's not, then the problem is elsewhere. You should avoid checking for a particular Key's action more than once or across multiple scripts in an update. If you are doing this, it may cause issues.

 

My suggestion is that you post an example project that encapsulates the problem so others can try to duplicate exactly the issue.

 

On a side note, you can check for different actions for a particular Key in an update.

 

 

Edit -- the only thing I can think of is how maybe you are setting the value for self.room? How is that variable being set via the different rooms' triggers? If you have multiple triggers with scripts, are all the triggers' scripts checking the state of H's KeyHit? If so, that may cause problems.

Win7 64bit / Intel i7-2600 CPU @ 3.9 GHz / 16 GB DDR3 / NVIDIA GeForce GTX 590

LE / 3DWS / BMX / Hexagon

macklebee's channel

Link to comment
Share on other sites

the only thing I can think of is how maybe you are setting the value for self.room? How is that variable being set via the different rooms' triggers? If you have multiple triggers with scripts, are all the triggers' scripts checking the state of H's KeyHit? If so, that may cause problems.

 

You are right, my "if window:KeyHit" was placed in many script, I will make more research to solve this :) thanks for your advices

Link to comment
Share on other sites

My test of your issue I had the KeyHit check in the player's script:


Script.Message = ""

...

...

if window:KeyHit(Key.H) then -- in UpdateWorld function

System:Print(self.Message)

end

 

And in the trigger's script, I had:

function Script:Collision(entity, position, normal, speed)

entity.script.Message = self.entity:GetKeyValue("name")

end

 

Simplified in the KeyHit code, but your nested check of the room's name will work inside like you posted above.

Win7 64bit / Intel i7-2600 CPU @ 3.9 GHz / 16 GB DDR3 / NVIDIA GeForce GTX 590

LE / 3DWS / BMX / Hexagon

macklebee's channel

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   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.

 Share

×
×
  • Create New...