Jump to content
  • Advertisement
Sign in to follow this  
jdub

C# Dictionary Question

This topic is 2346 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So I need a way to be able to relate my game actors with the physics body that represents them. To do that I use the following code:


private Dictionary<ActorId, int> bodyIdLookup; //matches a game actor ID to corrosponding physics body ID


However the problem is that I want to be able to do two things: 1) look up the game actor Id given the physics body Id 2) look up the physics body Id given the game actor Id. As I see it, the dictionary class only allows you to look up a value given it's corresponding key and not the other way around. Is this true? Can I implement this functionality without having another dictionary with the physics Id as a key and ActorId as a value?

Share this post


Link to post
Share on other sites
Advertisement

However the problem is that I want to be able to do two things: 1) look up the game actor Id given the physics body Id 2) look up the physics body Id given the game actor Id.


This is usually a sign that you're doing something wrong. If you have a 1 to 1 relationship where both sides will want to know about the other... you might as well just make them one object.


As I see it, the dictionary class only allows you to look up a value given it's corresponding key and not the other way around. Is this true?
[/quote]

Essentially. To do the reverse lookup, you'll need to iterate through the dictionary, which is not speedy. It also doesn't guarantee a single key for the value you're searching for.


Can I implement this functionality without having another dictionary with the physics Id as a key and ActorId as a value?
[/quote]

Sure, though having the second dictionary is the most straight-forward without simply putting references in the object itself.

Share this post


Link to post
Share on other sites

[quote name='jdub' timestamp='1336858117' post='4939634']
However the problem is that I want to be able to do two things: 1) look up the game actor Id given the physics body Id 2) look up the physics body Id given the game actor Id.


This is usually a sign that you're doing something wrong. If you have a 1 to 1 relationship where both sides will want to know about the other... you might as well just make them one object.
[/quote]
Or use composition. Most physics engines let you store an arbitrary pointer/object reference with a physics object. Then your object would store a reference to the physics object. Thus establishing the 1-1 relationship.

Share this post


Link to post
Share on other sites
No sure why you would want a Dictionary to do this. The "easy" way would be to put a reference in the objects to each other so that if you are using one you can quickly get to the other one. Now, this does tightly couple your code between the two, but in this case I don't see that as a bad thing.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!