Jump to content
  • Advertisement
Sign in to follow this  

Addressing Vectors

This topic is 3817 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

Im looking for a clean way of accessing the variables in a vector, by a specified name, such that accessing a particular element is easy even when the vector is of a large size. i was think of somthing like
static struct loc
int UI_AIM;
//etc etc

std::vector <UI_Element> UI_Elements;
UI_Element Aim_sight;
//set up Aim_sight ..........

loc.UI_AIM = (int)UI_Elements.size();

//so that now when the UI_Elements consists of 100's of variables, i can simply use .....

//access Aim_sight from vector

int data = UI_Elements[loc.UI_AIM].getData(); 

Share this post

Link to post
Share on other sites
I would probably use some sort of map for this (e.g. std::map from the SC++L). This would allow you to (among other things) access the elements using names in string form, e.g.:
// This is just an example. In practice you'd probably want to wrap this
// up so that the behavior is reasonable when there is no element with a
// key of 'aim'...
There are other issues at play here, such as how you want to store the elements, whether the objects are polymorphic, how the UI interacts with the rest of your application, etc. But as far as your particular question goes, I would say that a map or hash map of some sort would be a reasonable solution.

Share this post

Link to post
Share on other sites
Original post by gsg
Is there a reason you can't just use a structure?
What do you mean exactly? Can you give an example?

Share this post

Link to post
Share on other sites
Well, addressing a block of memory by unique symbolic identifiers is usually handled with structs. maya18222's code looks like structure lookup with offset calculation deferred to runtime - I was wondering what the reason is for not simply using a struct.

If the answer to that is "I need runtime flexibility", then use of symbolic identifiers is questionable. Surely the whole point of deferring this stuff to runtime is to allow adding an element by more flexible means than including a hardcoded list of symbols in your program?

Share this post

Link to post
Share on other sites
If your identifiers are hard coded constants then you'd be better off with something along these lines:
class UserInterface
UserInterface(); // Set up aim, health, ammo in constructor

UI_Element aim;
UI_Element health;
UI_Element ammo;

UserInterface ui;

int data = ui.aim.getData();

If your identifiers are not hard coded then using a map would be the better option:

std::map<std::string, UI_Element> uiElements;

....set up aim, health, ammo....

int data = uiElements["aim"].getData();

Whether or not you really want to store UI_Elements by value is something to consider, as jyk mentioned.

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!