Sign in to follow this  
Endar

Keeping track of variable types in a map

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
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 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
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
Oh, also, I'm going to need instructions like add, subtract, etc.

So, the way I'm implementing it is that in my instruction class, I have a 'char*' pointing to data and I'll probably have a list of offsets, so regardless if I have two numbers or if I have two strings that represent variables, I'll be able to copy them both into memory that the 'char*' points to and I'll be able to deal with it then.

So, if I had, say two vars: "var1" and "var2", then the contents of the memory that the 'char*' points to would be "var1\0var2\0". And the first offset would probably be 0 (I might just include it so I never forget about it) and the second offset would be 5, so I can read both bits.

My only problem is being able to tell what are the different types stored in the 'char*'. Because I only have a single Instruction class and an enum with the different instruction types, an instruction could take 0, 1, 2, or more values. Integer, float, string, whatever.

Does anyone have a couple of pointers? (and please don't think I'm trying to make a dodgy nerd joke :D)

Share this post


Link to post
Share on other sites

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