Jump to content
  • Advertisement
Sign in to follow this  
Endar

Keeping track of variable types in a map

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

I'm messing around with creating a scripting language and I've seen a good tut on gamedev and worked through just about all of it. Now, the tut I saw used integers as variables (eg. variable[0] or variable[6] ), and I thought that a good way to use actual strings as variable is using a map<char*, int>, but I've run into a little problem because I realised, what if I want to have a float a string as data? Then I came up with having a map<char*, void*>, but this comes with a problem of having to keep track of what kind of variable each one is. That is, unless a map can have a key with two values corresponding to that key, but so far I don't think it can. Can anyone think of a better idea that having two maps? (one for <char*,void*> other for <char*,char*>, the second char* representing "float" or "int". Appreciated :D

Share this post


Link to post
Share on other sites
Advertisement
If I understood you correctly, you want to store arbitary variable types in an map and keep track of their type, right?
Instead of storing the variables directly as values, why not wrap them up in struct that also contains the type?


struct ScriptVariable {
enum eVariableType {
SVT_INT,
SVT_FLOAT,
SVT_STRING
};
eVariableType typeId;
std::string strValue;
float floatValue;
int intValue;

ScriptVariable() : typeId(SVT_INT),intValue(0), floatValue(0)
{}
};

std::map<const char*, ScriptVariable> variableMap;


Using such a construct you can also easily mix types by converting them as required, e.g. in a script '1' * 1.5 becomes
1.5 with the result type set to float after converting '1' from string to float first, because there is no way to multiply strings.

I hope this helps you,
Pat.

Share this post


Link to post
Share on other sites
Quote:
Original post by iMalc
Or you could just have a map of VARIANTs...

It's been a while since I used that - isn't that a COM-centric data type?

Share this post


Link to post
Share on other sites
I don't think you're supposed to use a raw pointer as a key in a map because of some sorting issues (maybe somebody can explain why cause I'm not sure); however it is safe to use std::string instead.

Share this post


Link to post
Share on other sites
Quote:
Original post by fallenang3l
I don't think you're supposed to use a raw pointer as a key in a map because of some sorting issues (maybe somebody can explain why cause I'm not sure); however it is safe to use std::string instead.

Actually you are right. String compares are best done using actual strings, not pointers [smile]. For a scripting language I'd suggest converting all keys to lower-case before inserting and querying (case sensitive identifiers in scripts are quite annoying, IMHO[wink]).

Share this post


Link to post
Share on other sites
Quote:
Original post by Enigma
std::map<std::string, boost::any>

Enigma

The cleanest solution. I totally forgot about boost...

Share this post


Link to post
Share on other sites
Thanks everyone :D

I'll probably be going with a variation of darookie's idea, but I hear some people talking about boost.

What is it? Because I've heard a couple of people talking about it, but I haven't had time to go and look for info about it.

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!