ParaToxic Posted February 8, 2013 Share Posted February 8, 2013 Hello guys, I have a little problem here.I'm making a keybinding system for my game and need a map with a int reference as the first key and a normal int as the second. Well that's not supported by the standart STL map. Then I saw in the boost lib the wrapper for references. Now my map looks like that: [/size] std::map<boost::reference_wrapper<int>,int> m_keybinds; But now I don't realy know how to insert items in the map, because [/size] m_keybinds.insert(std::make_pair<boost::reference_wrapper<int>,int>(var,key)) don't work. Maybe somebody has experience with that wrapper Quote Link to comment Share on other sites More sharing options...
ParaToxic Posted February 8, 2013 Author Share Posted February 8, 2013 Okey I got it You have to use boost::ref for that.Here a little example: #include <boost\ref.h> #include <map> std::map<boost::reference_wrapper<int>,int> m; int a; int& b = a; m.insert(std::make_pair(boost::ref(B),a)); // Set b as a reference to the map Quote Link to comment Share on other sites More sharing options...
Furbolg Posted February 8, 2013 Share Posted February 8, 2013 Makes sense One question: I guess the second parameter is the key from keyhit/press (escape, left, right etc) ? But what is the first parameter (ref int?)? // Edit: Correct me if im wrong. First parameter is the key that you want to bind, the second parameter is the "action"key (jump, shoot...) ? Quote Link to comment Share on other sites More sharing options...
ParaToxic Posted February 8, 2013 Author Share Posted February 8, 2013 Yes , I have some interfaces: Each Actor has a IActorInterface which contains some variables like m_move,m_strafe.... Then I have with polymorhpism two other classes : IHumanInterface and IAIInterface. So you can create a user (human) interface with the type IActorInterface and pass it or an AI interface. Well the human interface has a functions that loads keybinds from a ini file. So I pass in my m_move as a reference in the map and the key which affects that value.Then in a Updatefunction I have a for loop which goes through all members in the map and check with if(KeyDown(itr->second)) if the key is pressed and when it is the first value ( with is a reference to my other variable ) should get an other value. Thats the idea behind it Quote Link to comment Share on other sites More sharing options...
ParaToxic Posted February 8, 2013 Author Share Posted February 8, 2013 Hmmm now I have an other problem. When I insert 4 items I have the size of 1 and just one key bind ?! How is that possible ? Look at the code: [/size] SetKeyBind(m_forward,ReadINI_Int(filename,"Keys","move_forward")); SetKeyBind(m_backward,ReadINI_Int(filename,"Keys","move_backward")); SetKeyBind(m_strafe_left,ReadINI_Int(filename,"Keys","strafe_left")); SetKeyBind(m_strafe_right,ReadINI_Int(filename,"Keys","strafe_right")); IConsole::inst()->Debug("SIZE 1 : %i",m_keybinds.size()); And the SetKeyBind Function: void IHumanInterface::SetKeyBind(int &var,int key) { m_keybinds.insert(std::make_pair(boost::ref(var),key)); } Working now, had to swap the keys of the map Quote Link to comment Share on other sites More sharing options...
Furbolg Posted February 8, 2013 Share Posted February 8, 2013 Ok, thanks for explanation. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.