Jump to content

toggle value?


dennis
 Share

Recommended Posts

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.

Link to comment
Share on other sites

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

  • Upvote 1
Link to comment
Share on other sites

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;

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

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

Guest Red Ocktober

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

Link to comment
Share on other sites

Mike is right, you probably also need to check for the key release before toggling the variable

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

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.

LE Version: 2.50 (Eventually)

Link to comment
Share on other sites

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

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

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Guest Red Ocktober

@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

Link to comment
Share on other sites

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.

LE Version: 2.50 (Eventually)

Link to comment
Share on other sites

Guest Red Ocktober

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

  • Upvote 1
Link to comment
Share on other sites

  • 2 months later...

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