tipforeveryone Posted November 4, 2016 Share Posted November 4, 2016 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 Quote Link to comment Share on other sites More sharing options...
AggrorJorn Posted November 4, 2016 Share Posted November 4, 2016 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 Quote Link to comment Share on other sites More sharing options...
tipforeveryone Posted November 4, 2016 Author Share Posted November 4, 2016 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 ? Quote Link to comment Share on other sites More sharing options...
AggrorJorn Posted November 4, 2016 Share Posted November 4, 2016 I would put it in the main.lua loop and not in an entity script. Somewhere at the end of the loop so you know that all updateworld and updatephysics already have taken place. Quote Link to comment Share on other sites More sharing options...
tipforeveryone Posted November 4, 2016 Author Share Posted November 4, 2016 I would put it in the main.lua loop and not in an entity script. Somewhere at the end of the loop so you know that all updateworld and updatephysics already have taken place. Same resuit I put window:FlushKeys() at the bottom of loop and still can't move. You should give it a try, please Quote Link to comment Share on other sites More sharing options...
AggrorJorn Posted November 4, 2016 Share Posted November 4, 2016 I'll have a have a look this evening. Quote Link to comment Share on other sites More sharing options...
Genebris Posted November 4, 2016 Share Posted November 4, 2016 Just make sure you check for key press every update and use this information only when needed. Don't put it inside other if statement. Quote Link to comment Share on other sites More sharing options...
AggrorJorn Posted November 4, 2016 Share Posted November 4, 2016 Yeah thats another caveat, once you used the keyfunction in 1 script, you can't use it again in another script in the same update. But that is currently not the issue I believe. Quote Link to comment Share on other sites More sharing options...
Genebris Posted November 4, 2016 Share Posted November 4, 2016 Just do this local hHit = window:KeyHit(Key.H) if self.player_Inside_Room and hHit then System:Print("Yay!") end Quote Link to comment Share on other sites More sharing options...
tipforeveryone Posted November 4, 2016 Author Share Posted November 4, 2016 Just do this local hHit = window:KeyHit(Key.H) if self.player_Inside_Room and hHit then System:Print("Yay!") end This won't work I tried. Quote Link to comment Share on other sites More sharing options...
Jazz Posted November 4, 2016 Share Posted November 4, 2016 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 Quote --- Scott Using Windows 7 Ultimate 64 bit/Core I7-2700K @ 4312mhz/24G RAM/Nvidia GTX 1060 Link to comment Share on other sites More sharing options...
Genebris Posted November 4, 2016 Share Posted November 4, 2016 This won't work I tried. It does for me, your mistake is somewhere else. 1 Quote Link to comment Share on other sites More sharing options...
Rick Posted November 4, 2016 Share Posted November 4, 2016 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. 2 Quote Link to comment Share on other sites More sharing options...
AggrorJorn Posted November 4, 2016 Share Posted November 4, 2016 *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() Quote Link to comment Share on other sites More sharing options...
tipforeveryone Posted November 5, 2016 Author Share Posted November 5, 2016 @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 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 Quote Link to comment Share on other sites More sharing options...
Genebris Posted November 5, 2016 Share Posted November 5, 2016 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. 2 Quote Link to comment Share on other sites More sharing options...
macklebee Posted November 5, 2016 Share Posted November 5, 2016 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. Quote 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 More sharing options...
tipforeveryone Posted November 5, 2016 Author Share Posted November 5, 2016 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 Quote Link to comment Share on other sites More sharing options...
macklebee Posted November 5, 2016 Share Posted November 5, 2016 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. Quote 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 More sharing options...
tipforeveryone Posted November 7, 2016 Author Share Posted November 7, 2016 Thank you guys. Finally solved this problem, I neutralized all window:KeyKit in all script files then rewrite functions to execute my needs There should be only one window:KeyHit in a whole game and placed in character controller script. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.