Jump to content

std::map with key reference -> boost::reference_wrapper


Recommended Posts

Posted

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 :D

Posted

Okey I got it :D 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

Posted

Makes sense wink.png

 

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...) ?

Posted

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 :D

Posted

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 :)

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...