Hi,
I'm really new to SFML and it's been years since I've coded in C++ so excluse my code if its terrible.
What I want is to make a Key Binding Menu where the player can change the keys used to move and such things.
Here is my problem, I have a class that takes care of all the settings (resolution, volume, that kind of stuff), so I want to pass the key press to that class.
My approach is to handle the event in the main class where the game loop is.
Here is some minimal code.
GameSystem.h
enum GameStates
{
PAUSE,
RUN,
EXIT
};
enum GameEvents
{
MENU,
QUIT,
KEY
};
class GameSystem
{
public:
GameSystem();
~GameSystem();
void initilize();
bool isRunning();
void update();
private:
Settings _settings;
thor::ActionMap<GameEvents> _events;
thor::ActionMap<GameEvents>::CallbackSystem _systemEvents;
sf::RenderWindow _window;
GameStates _state;
void onKeyPress(thor::ActionContext<GameEvents> context);
}
Now the problematic part.
GameSystem.cpp:
void GameSystem::initilize()
{
_settings.loadSettings();
_window.create(sf::VideoMode(_settings.get_width(), _settings.get_height()), "Title");
_state = RUN;
_events[QUIT] = thor::Action(sf::Event::Closed);
_events[MENU] = thor::Action(_settings.get_menu(), thor::Action::PressOnce);
_events[KEY] = thor::Action(sf::Event::KeyReleased);
_systemEvents.connect(KEY, &GameSystem::onKeyPress);
}
void GameSystem::onKeyPress(thor::ActionContext<GameEvents> context)
{
sf::Event event = *context.event;
_settings.set_menu(event.key.code);
}
In the example I don't show neither the loop nor the events trigger, but that's because the events fire just fine when I don't try to use the callback. If I do the event callback from outside a class and just pass a function (as in the example) everything works correctly, but then I don't have access to the _settings instance.
I'm pretty sure this is a very C++ fundamental lack of knowledge on my part but I've searched for a couple days now and I just don't find how to do this or if this is a terrible idea alltoghether.
The compiler error is:
Severity Code Description Project File Line Suppression State
Error C2664 'thor::Connection thor::EventSystem<thor::ActionContext<ActionId>,ActionId>::connect(const EventId &,std::function<void (const Event &)>)': cannot convert argument 2 from 'void (__cdecl GameSystem::* )(thor::ActionContext<ActionId>)' to 'std::function<void (const Event &)>' Project2 d:\programacion\sfml\project2\gamesystem.cpp 25
Excuse my grammar, english is not my mother languaje