Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: User input problem  (Read 1992 times)

0 Members and 1 Guest are viewing this topic.

Marty

  • Newbie
  • *
  • Posts: 6
    • View Profile
User input problem
« on: May 18, 2011, 11:50:30 pm »
Hey Everyone (and thanks for bearing over with my high amount of questions)

I have some code that i simply cannot get to work the way i intend it to, and i hope some of you might shead some light on how i could fix it.

Basically i have a setup close to this (though alot has been removed for simplicity's sake):

Code: [Select]

bool WaitForInput = false;
while(Window.IsOpened()){
  sf::Event Event;
  while(App.GetEvent(Event))
  {
  if(handler == special situation)
  bool WaitForInput = true;
  while(WaitForInput == true)
  {
   If(Event.Type == sf::Event::KeyPressed)
   {
    WaitForInput = false;
    cout << "Key has been pressed" << endl;
    }
   }
  }
}


So basically what i inteded with it was that if the loop enters a specific condition (the if sentance) - It should promt the user (not included in code) to press a key, and stay there until the user responds by hitting a key.
However all i've been able to achieve so far is that it gets stuck in the loop forever - despite me desperately smashing my keyboard buttons.

So is there a way to add a "wait for key input" kinda thing in the middle of the code?

(the structure of the code was kinda borrowed from this topic.


edit: Just altered it a bit.
Moved the While Get Event part down to where my primary key input part was, as it was running the rest of the code twice whenever it recieved a valid input. (which makes no sense at all).

Also thought of a 2nd question which could be related to the first one:

As my project is "turnbased", is there a way to stop it from rushing through the while loop like a madman? As it is now it makes my cpu go from "power-saving happy" to "I diddent know you where playing 3 instances of Crysis on the highest settings" mode.

So kinda make it rest until key input (which is basically the same as i wanted in question one - except in that case it should pause-mid code).

Thanks in advance for any help! :D

OniLinkPlus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
User input problem
« Reply #1 on: May 19, 2011, 02:45:27 am »
Can you provide your source code with anything irrelevant removed? We need complete and minimal code to help you.
I use the latest build of SFML2

Marty

  • Newbie
  • *
  • Posts: 6
    • View Profile
User input problem
« Reply #2 on: May 19, 2011, 02:39:37 pm »
I hope this is sufficient:
Source Code

It's my "main gameloop" function, most of the functions called are just passing back and forth information after calculating the results of the inputs.
It's not really optimized yet (specially not the sfml window/input/gfx parts, still working on fully understand it all with class implementations - but thats a whole other story that i'll take my time with).

I've made a little:
---------------
This is the spot
---------------
comment for where i'd like to insert the "pause and wait for key to continue" event.

The couts are mainly for bug testing purposes and will be removed once my problems are solved :)

(The reason i've only included that gameloop function is because thats where the entire SFML part of the program is - only there).

OniLinkPlus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
User input problem
« Reply #3 on: May 20, 2011, 01:57:09 am »
Quote from: "Marty"
I hope this is sufficient:
Source Code

It's my "main gameloop" function, most of the functions called are just passing back and forth information after calculating the results of the inputs.
It's not really optimized yet (specially not the sfml window/input/gfx parts, still working on fully understand it all with class implementations - but thats a whole other story that i'll take my time with).

I've made a little:
---------------
This is the spot
---------------
comment for where i'd like to insert the "pause and wait for key to continue" event.

The couts are mainly for bug testing purposes and will be removed once my problems are solved :)

(The reason i've only included that gameloop function is because thats where the entire SFML part of the program is - only there).
This is neither complete nor minimal.
Complete means it can be compiled by itself.
Minimal means it only contains code related to the problem.
I found your problem. The while loop is "while (wait = true)." This will never exit. When it checks if wait is equal to true, you tell it to set wait to true. You have to use ==, not =.
I use the latest build of SFML2

Marty

  • Newbie
  • *
  • Posts: 6
    • View Profile
User input problem
« Reply #4 on: May 20, 2011, 09:17:32 am »
Thanks for the help OniLink10, that solved the issue. I must have stared myself blind at the issue.

Sorry about the the whole thing regarding how much to attach, I completely misunderstood what you where reffering to. But at least, should I need more assistance in the future, I'll be aware of what to attach properly next time.

Again, Thanks a bunch for the help!