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

Author Topic: Handling input in a state based approach  (Read 2478 times)

0 Members and 1 Guest are viewing this topic.

Cameleon

  • Newbie
  • *
  • Posts: 1
    • View Profile
Handling input in a state based approach
« on: December 02, 2010, 08:19:09 pm »
Hi. I'm writing a puzzle game using SFML2 for windowing and input, however my game's main loop looks like this

Code: [Select]

//at init
this->input = window->GetInput(); //oinoput of type const sf::Input&

 while(//running) //while the current state is still running
{
   stateManager->stateLoop(input);
   this->window->Display();
}


Code: [Select]

stateManager::stateLoop(sf::Input& input)
{
   //check various state flags
   currentState->HandleInput(input); //<-Need access to sf::Input& here
}


I'm unsure how to hand down input handling to each state. I have tried passing an instance of sf::Input by reference down through the state manager to the state but I was getting compiler errors about the class being non copyable. Any help would be great

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
Handling input in a state based approach
« Reply #1 on: December 02, 2010, 08:34:52 pm »
You could have a loop which pulls event from the window and then pass the event down each state. Though a pretty ruff method but it works. You could also check my signal system in the wiki: http://sfml-dev.org/wiki/en/sources/signal_system
Though I'm uncertain but I think I found bugs in it that I haven't corrected.

And why you are getting a compiler error is because I think the "input" member variable you try to assign the input to is not a reference variable and thus must copy the input object in order to assign it into the "input" variable.
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

Drektar

  • Jr. Member
  • **
  • Posts: 77
    • View Profile
Handling input in a state based approach
« Reply #2 on: December 02, 2010, 10:41:14 pm »
You have to use constant pointer or reference with sf::Input...
Code: [Select]
sf::Input* Input =  &App.GetInput() ; // Not correct
const sf::Input* Input =  &App.GetInput() ; //Correct
//or
sf::Input& Input = App.GetInput(); //Not correct
const sf::Input& Input = App.GetInput(); //Correct

Maybe it's not the problem but here :
Code: [Select]
stateManager::stateLoop(sf::Input& input)
You have to pass a constant reference:
Code: [Select]
stateManager::stateLoop(const sf::Input& input)