123
-=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- (c) WidthPadding Industries 1987 0|644|0 -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=-
Socoder -> Blitz -> Stupid question....

Sun, 16 Mar 2008, 20:28
Orion Pax
Ok.... I am having trouble in blitz3d. This is a really ignorant question and cant believe I cant think of the solution. Its got me so perplexed that I have a headache now.

I am trying to set the key 'C' to toggle an item on and off. I can get it to turn it on...just not off!

I have tried this


I have tried putting flushkeys in there in several places.

I have tried various other forms of the above code.

And I am really not going to be able to sleep tonight because of this now. I know my knowledge of blitz is better than this. Heck my knowledge of basic period is better than this but my head is hurting too bad to think any more. Maybe thats my problem right now...
Sun, 16 Mar 2008, 20:39
JL235
Works fine for me:

Sun, 16 Mar 2008, 22:09
oscar
the reason it won't work is that it will execute both ifs
so:

it will set cc=1
THen because key 46 is hit and cc now =1 it will set it back to 0

you probably want to do:
If (KeyHit(46) And cc=0) Then cc=1

elseif (KeyHit(46) And cc=1) Then cc=0

so that it will only execute one or the other instead of both

Sun, 16 Mar 2008, 22:29
HoboBen
I think the problem is calling KeyHit twice...

If it's hit the first time, then there won't be time for it to be hit the second time.

Either use the "on = not on" method as demonstrated above, or store the result of KeyHit...



-=-=-
blog | work | code | more code
Sun, 16 Mar 2008, 23:29
JL235
HoboBen, yours is even worse. By storing the result it does mean that if keyhit is true and cc is equal to 0 then both if statements will be run, as Oscar pointed out above.

An alternative to my one liner above is to use a nested if statement with else:

I'd also recommend you to use true and false instead of 1 and 0 when you are doing flags. It's more descriptive to the reader because it clearly tells them that there will only be two states, true and false. You also don't need to check if cc is equal to 1 in the if statement because true and 1 in Blitz are exactly the same. If your checking if it's false, it's also usually more common to write 'not cc' instead of 'cc = false' in most languages.
Sun, 16 Mar 2008, 23:33
HoboBen
Sorry, yes. It's too early to think!

Checking keyhit twice is still something to avoid though.

-=-=-
blog | work | code | more code
Mon, 17 Mar 2008, 07:08
spinal
I would...


then you only check it once.

-=-=-
Check out my excellent homepage!
Mon, 17 Mar 2008, 08:29
mike_g
Yet another way:


Tue, 18 Mar 2008, 09:39
mindstorm8191
Keyhit would be fine for this, but it sometimes gets confused by the flushkeys. If you want to use keydown, you'll have to remember if it was being held down at an earlier time, or not. Something along the lines of:

(urg, I hope that makes some sense) Of course, this will only be needed if flushkeys is causing problems.

-=-=-
Vesuvius web game
Tue, 18 Mar 2008, 11:17
Paul
keyhit() works something like this:

function keyhit(x)
if keydown(x) and keysdown(x)=0
keysdown(x)=1
return 1
endif
return 0
end function

And blitz has some internal workings doing something like this:

for i=1 to 255;or is it 0 to 255?
if not keydown(i) then keysdown(x)=0
next

back on topic, this is how i would have solved the initial problem

if keyhit(46) then cc=not cc