|
readkey + bool = Opening inventary |
jaime barrachina
Member #6,780
January 2006
|
void teclado() bool pillartecla() if (modo == 1) Looks fine and nice, suposedly, theres a function something like if (pillartecla == true) What all thaht is suposed to do is open the inventary when the player presses sapce_bar and closes it when he prees it again. As im only groping arround in the dark here, i'd apreciate any and all help you can give me, as it's not working. Especially if someone could tell me how yo use the readkey() properly and were to call the teclado(). I get a feeling inneed to call it in a while or a for but im not shure. Cheers "Under the sword lifted high There is hell making you tremble: But go ahead, And you have the land of bliss. - Miyamoto Musahshi" |
ReyBrujo
Moderator
January 2001
|
First, use code tags, putting <code> and </code> surrounding your code so that it is "codified". Second, you don't need to use END_OF_FUNCTION in all functions, only in those functions that are going to be called from inside a timer. Third, the game should look something like this (this may not compile!): #include <allegro.h> int main() { int quit = 0; allegro_init(); install_keyboard(); while (!quit) { teclado(); dibujo(); } allegro_exit(); } END_OF_MAIN() You call teclado to poll the keyboard, and then call dibujo to draw on screen. -- |
GullRaDriel
Member #3,861
September 2003
|
And using global variables isn't that good. "Code is like shit - it only smells if it is not yours" |
Ceagon Xylas
Member #5,495
February 2005
|
I know that using globals isn't a good thing, and I dont use them because people have told me to make functions with arguments... But why exactly is it bad? |
HoHo
Member #4,534
April 2004
|
Quote: But why exactly is it bad?
Couple of reasons can be red from here: The thing about multithreading will become more and more important as multicore CPU's are becoming more widespread __________ |
Rick
Member #3,572
June 2003
|
Quote: What all thaht is suposed to do is open the inventary when the player presses sapce_bar and closes it when he prees it again. This is how I usually handle a situation like that.
======================================================== |
jaime barrachina
Member #6,780
January 2006
|
1000 thanks to everyone who's replied and excuse me for my lousy gramar, please, i was half asleep when i wrote the first post. Thanks again everyone. "Under the sword lifted high There is hell making you tremble: But go ahead, And you have the land of bliss. - Miyamoto Musahshi" |
Jeff Bernard
Member #6,698
December 2005
|
Hopefully this'll clear it up for you:
-- |
Rick
Member #3,572
June 2003
|
Basically if you just use key[KEY_SPACE] to open the inventory it will run the code inside it on every loop, since if you hold it down it evaluates to true. Since we want a toggle of sorts, that is why we add the boolean value. So I push the space bar, it goes in and opens the inventory, now if I hold the space bar it won't keep going in and opening the inventory. But actually thinking about it, you might not want what I posted. With that code if you let go on space the inventory closes. If you want to toggle it with the space bar you will have to put something inside the first if statement to see if it's open or closed and act on it that way.
That will toggle inventory based on open or closed. ======================================================== |
Indeterminatus
Member #737
November 2000
|
Goin' a little off-topic. In C++ (don't know if that'd work in C as well), a way to easily remove the global variable would be the following:
_______________________________ |
Rick
Member #3,572
June 2003
|
Good point, although if I was doing this, input() would be in a Game class and key_space would be a member variable. ======================================================== |
Jeff Bernard
Member #6,698
December 2005
|
Rick, I believe the second code you posted would do just what the code I posted would do. I changed your original just a bit: That way you don't need another function to determine if inventory is open, you can just use the boolean. Or, since the input() function will most likely be looped, to make sure it doesn't run closeInventory() right away, you could just add this in: while (key[KEY_SPACE]) { rest(1); } EDIT-- I'm bored, have another version of the code. This one's the best (and conforms to not using a global):
-- |
Rick
Member #3,572
June 2003
|
That would actually freeze the entire game if I held the space bar. I don't like that solution. ======================================================== |
Tobias Dammers
Member #2,604
August 2002
|
The core of the problem is that you need to convert states into state changes over time. To do this, you need to keep a copy of the previous state around, and compare the current one. I prefer doing this for the entire key array, so I only have to implement it once:
Call tick_keyboard() once at the start of the input function. If delta_key[KEY_SPACE] is 1, toggle the inventory state (inv_open = !inv_open). --- |
jaime barrachina
Member #6,780
January 2006
|
Txs everyone for their help, really apreciate it people Problem is more or less solved now. Cheers "Under the sword lifted high There is hell making you tremble: But go ahead, And you have the land of bliss. - Miyamoto Musahshi" |
|