Sign in to follow this  

C# Dictionary Question

This topic is 2043 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:

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

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
[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.
[/quote]

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]
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.

[quote]
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='Telastyn' timestamp='1336858800' post='4939636']
[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.
[/quote]

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

This topic is 2043 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this