dennis Posted May 9, 2012 Share Posted May 9, 2012 Hello, I wanted to toggle a screen when pressing on escape or TAB. for showing a quest screen. or something like that. but my code didn't work... atleast it partly works. shows for like 1 ms the code I'm using is: if KeyHit(KEY_ESCAPE) fw:Render() SetBlend(1) DrawText("if you see this it works.",100,100) SetBlend(0) Flip() end What am I doing here wrong? I also use the while do... but that worked for some few seconds. it showed when not pressing the escape key. and it didn't go off when pressing the escape key. so not worked at all. Quote Link to comment Share on other sites More sharing options...
Furbolg Posted May 9, 2012 Share Posted May 9, 2012 Of course the code dont work, take a few moments and think about what you have done. if ESCAPE is Hit then Show.... but Hit lasts only 1 Frame. What you have to do is set up an variable, the easiest way to do this is bool questionscreen = false; if KeyHit(KEY_ESCAPE) questionscreen = !questionscreen; end This is your toggle, the longer version is bool questionscreen = false; if KeyHit(KEY_ESCAPE) if (questionscreen) questionscreen = false; else questionscreen = true; end Now you can check if your variable is true and show your options/quests etc. if (questionscreen) fw:Render() SetBlend(1) DrawText("if you see this it works.",100,100) SetBlend(0) Flip() end 1 Quote Link to comment Share on other sites More sharing options...
Mumbles Posted May 9, 2012 Share Posted May 9, 2012 I was going to write that too, but since it's already been done, I'll click that facebook tick thing instead... Quote LE Version: 2.50 (Eventually) Link to comment Share on other sites More sharing options...
dennis Posted May 9, 2012 Author Share Posted May 9, 2012 Of course the code dont work, take a few moments and think about what you have done. if ESCAPE is Hit then Show.... but Hit lasts only 1 Frame. What you have to do is set up an variable, the easiest way to do this is bool questionscreen = false; if KeyHit(KEY_ESCAPE) questionscreen = !questionscreen; end This is your toggle, the longer version is bool questionscreen = false; if KeyHit(KEY_ESCAPE) if (questionscreen) questionscreen = false; else questionscreen = true; end Now you can check if your variable is true and show your options/quests etc. if (questionscreen) fw:Render() SetBlend(1) DrawText("if you see this it works.",100,100) SetBlend(0) Flip() end It won't work for me though that is normally more like C, C++, VB like code. also couldn't remember since LUA accepted bool. as in: bool questionscreen = false; Quote Link to comment Share on other sites More sharing options...
Furbolg Posted May 9, 2012 Share Posted May 9, 2012 Im sorry im not familiar with Lua... should be the same like c++ Quote Link to comment Share on other sites More sharing options...
Rick Posted May 9, 2012 Share Posted May 9, 2012 you can use bool true/false in Lua you don't define it as bool. just do: questionScreen = false; Quote Link to comment Share on other sites More sharing options...
dennis Posted May 9, 2012 Author Share Posted May 9, 2012 you can use bool true/false in Lua you don't define it as bool. just do: questionScreen = false; Thanks, and you too Furbolg. But it still doesn't toggle. it shows it for 1 frame. when I spam the button it kinda shows it. I used questionscreen = false; if KeyHit(KEY_ESCAPE) then if (questionscreent) then questionscreen = false; else questionscreen = true; end end if questionscreen then fw:Render() SetBlend(1) DrawText("if you see this it works.",100,100) SetBlend(0) Flip() end Quote Link to comment Share on other sites More sharing options...
Pixel Perfect Posted May 9, 2012 Share Posted May 9, 2012 Are you assigning the initial questionscreen = false in the game loop? It needs to be outside of the game loop or it will reset it every iteration of the loop (i.e. every frame). So: Initialise the variable Game loop: Test for Esc Key and set variable appropriately Test variable and act appropriately End of loop. Quote Intel Core i5 2.66 GHz, Asus P7P55D, 8Gb DDR3 RAM, GTX460 1Gb DDR5, Windows 7 (x64), LE Editor, GMax, 3DWS, UU3D Pro, Texture Maker Pro, Shader Map Pro. Development language: C/C++ Link to comment Share on other sites More sharing options...
Guest Red Ocktober Posted May 9, 2012 Share Posted May 9, 2012 i dunno... but it appears as if the solutions above have a built in logic fault to em... you many think that a quick keypress is enough to toggle the questionScreen variable once, but in reality the toggle is being toggled at around 60 times each second... even the fastest keypress won't guarantee the desired results... you've gotta make the toggle happen once and prevent the toggle from happening further during the key being pressed in order for the toggle to work properly... --Mike Quote Link to comment Share on other sites More sharing options...
Pixel Perfect Posted May 9, 2012 Share Posted May 9, 2012 Mike is right, you probably also need to check for the key release before toggling the variable Quote Intel Core i5 2.66 GHz, Asus P7P55D, 8Gb DDR3 RAM, GTX460 1Gb DDR5, Windows 7 (x64), LE Editor, GMax, 3DWS, UU3D Pro, Texture Maker Pro, Shader Map Pro. Development language: C/C++ Link to comment Share on other sites More sharing options...
dennis Posted May 9, 2012 Author Share Posted May 9, 2012 hmm, strange, now it's flickering at game start Quote Link to comment Share on other sites More sharing options...
Mumbles Posted May 9, 2012 Share Posted May 9, 2012 you've gotta make the toggle happen once and prevent the toggle from happening further during the key being pressed in order for the toggle to work properly... --Mike The solutions provided already take this into account. They use KeyHit which (in Blitz) returns the number of times the specified key has been pressed since it was last checked. I think in LE 2 it's been reworked to simply be 1 if the key has been released and pressed again since the last check, and 0 if it's either not pressed, or still held down from the last check... However your point would be true if KeyDown was used since that simply returns 1 if the key is currently pressed, regardless of whether it is still being held down from last time, or a totally unique keypress. The flickering is more likely due to declaration and initialisation inside the main game loop (which was identified by Pixel Perfect), when it should be outside the game loop, but that wasn't explicitly stated when the solution was given. Quote LE Version: 2.50 (Eventually) Link to comment Share on other sites More sharing options...
Pixel Perfect Posted May 9, 2012 Share Posted May 9, 2012 Here is some toggle code I did for mouse buttons ( I knew I had some somewhere but it was a long time back): void updateMouseCommands(void) { bLeftMouseButtonClicked = false; bRightMouseButtonClicked = false; if(MouseDown(1)) { bLeftMouseButtonDown = true; bLeftMouseButtonDownToggle = true; } else { bLeftMouseButtonDown = false; } if(MouseDown(2)) { bRightMouseButtonDown = true; bRightMouseButtonDownToggle = true; } else { bRightMouseButtonDown = false; } if(bLeftMouseButtonDownToggle && !bLeftMouseButtonDown) { bLeftMouseButtonClicked = true; bLeftMouseButtonDownToggle = false; } if(bRightMouseButtonDownToggle && !bRightMouseButtonDown) { bRightMouseButtonClicked = true; bRightMouseButtonDownToggle = false; } } This allows you to then conditionally switch based on the values of bRightMouseButtonClicked and bLeftMouseButtonClicked Quote Intel Core i5 2.66 GHz, Asus P7P55D, 8Gb DDR3 RAM, GTX460 1Gb DDR5, Windows 7 (x64), LE Editor, GMax, 3DWS, UU3D Pro, Texture Maker Pro, Shader Map Pro. Development language: C/C++ Link to comment Share on other sites More sharing options...
dennis Posted May 9, 2012 Author Share Posted May 9, 2012 But I also need it to use for a pause game. so when the escape button is clicked the game is pause and draws some text on screen which says pause. so when clicking the escape again the game will start. a normal logic would be... 1 click is one second click is off. as with a light. when I push that little button it will go on, the second click turns it off. Quote Link to comment Share on other sites More sharing options...
Furbolg Posted May 9, 2012 Share Posted May 9, 2012 You just need to "toggle" a variable from true to false, this variable has to be define outside the mainloop. If you use then KeyHit (Not KeyPress, KeyDown, KeyUp etc.) then you can toggle (switch) the variable. The simplest solutin i know is : variable = !variable; (or in basic: variable = not variable) Then you check it and can do what you want, to do a "pause" you can start a new loop and end it after another KeyHit. Quote Link to comment Share on other sites More sharing options...
Guest Red Ocktober Posted May 9, 2012 Share Posted May 9, 2012 @Mumbles... The solutions provided already take this into account. They use KeyHit which (in Blitz) returns the number of times the specified key has been pressed since it was last checked. yes, you're right about KeyHit() in BlitzMax... but since i didn't see any BlitzMax code above, i suggested that none of the suggested solutions were valid... the logic is faulted... it wouldn't work properly in c or lua, which the above code fragments looked like they were written in... in lua, a simple and reliable toggle that he could use ( 1 key as the toggle switch) would look like this if KeyDown(KEY_1)==1 and toggleSwitch==0 then toggleSwitch=1 questionScreen=questionScreen*-1 end if KeyDown(KEY_1)==0 then toggleSwitch=0 end the questionScreen variable is initialized to -1 (OFF) outside the main loop... and switches to 1 (ON) or -1 (OFF) inside the main loop when the toggle event is triggered (1 key is pressed)... this would work with either KeyHit() or KeyDown() functions in lua... the same logic should also work in c, blitzMax, vb, c# (or any programming language) regardless of whether KeyHit(), KeyDown(), KeyPressed(), MouseButtonDown(), or whatever input is used as the toggle... the logic is bullet proof and not prone to variations in any specific language... --Mike Quote Link to comment Share on other sites More sharing options...
Mumbles Posted May 10, 2012 Share Posted May 10, 2012 KeyHit and KeyDown are both available in the C DLL and they behave exactly as they would in Blitz Max (KeyPressed isn't available and MouseButtonDown is now MouseDown(int button)). It would make sense if Josh's TKeyHit(int key) command was just a single line wrapper function return KeyHit(key) so, if lua has access to the same commands, then it should work, then again, disclaimer -- I don't use lua, which is why I'm sticking with your method of of NOT-ing a variable... (which I'd never actually thought of before). if KeyHit(KEY_1)==1 then questionScreen=questionScreen*-1 end I myself, don't use Blitz (or lua), but I learned the distinction between hit and down whilst in my earlier years using Blitz 3D, and indeed Dark Basic had it too, except back in my DB times, I couldn't fathom the difference between "object hit" and "object collision", which worked in much the same way and KeyHit and KeyDown respectively, even though it was there in plain English in the help page. Quote LE Version: 2.50 (Eventually) Link to comment Share on other sites More sharing options...
dennis Posted May 10, 2012 Author Share Posted May 10, 2012 Thanks guys, will try it as soon as possible. I'm exhausted, didn't sleep for 40 hours now. Quote Link to comment Share on other sites More sharing options...
Guest Red Ocktober Posted May 10, 2012 Share Posted May 10, 2012 the logic is the primary thing in programming... it's irrelevant what programming language you use... almost as irrelevant is the code, seeing as the code is merely the expression of the logic you want to implement... if the logic is faulted or incomplete, then the product of that logic is also faulted or incomplete... and prone to error... also, solid logic usually produces solid code... and is easily transportable to different languages... seeing that i switch back and forth between c++, lua, blitzmax, and several others, for me, it's as simple as that... whatever the prevailing consensus, we've given Dennis a few options from which to choose... --Mike 1 Quote Link to comment Share on other sites More sharing options...
dennis Posted July 13, 2012 Author Share Posted July 13, 2012 Well thank you Mike. I'm more familiar with the LUA code now. so I understand some of those now. I really appreciate your help and also the Helping hand from the other community people. really great. Thanks. 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.