Sign in to follow this  
sakky

A registry system?

Recommended Posts

sakky    100
I thought about using a type of variable registry system. Each registry item has three fields, id, type and data. The id is just a unique identifier for an item. The type and data members are a little tricky. The type is an enumerated registry variable type. This is used to denote the type of data the registry contains so that any operations are carried out on the right memory. The data is one of many fields in a union. Let’s see, Boolean, integer, floating point and string are the types. So then a sample structure of what one of the registry items would look like would be this-> struct reg_item { int id; int type; union { bool b ; long l ; float f ; char* s ; } } ; Sorry for the short variables names, but you get the point. I find my self wondering how such a system would be any use. If I had it, what would I use it for and would it really be necessary. I figured it would be sort of a cool way to use global variables for such things like game status or flags. All one would need is the basic ids and types and they can use the data. Some extra things could be added to prevent write access to specific variables i.e. read-only. But I thought it would make a little cleaner code and easier to use this type or system to store many different variables that a game would use. Take for instance, some of the developer flags or commands used in Quake III and others like it. One in particular ‘sv_cheats’. Hehe... you could set these types of flags as read only and enable or disable them. This way you don’t have a lot of extra code in classes or what ever that get or set something. Next step, how would this system be created? I choose a set array size with an embed linked list. Later, maybe a std::map or something of my own invention could be used. But what do you think about this?

Share this post


Link to post
Share on other sites
Endar    668
If you're thinking mostly of a console like in Quake 3, there are better ways to do it than have a whole lot of global variables. Also, if you were to use a system like this, there shouldn't be global because most of whatever you're making will not have a use for them, so you should keep them all in a specific spot.

Share this post


Link to post
Share on other sites
Bezben    202
I used to have something similar to what you are suggesting:

#define tINT 1 // ... an integer
#define tFLOAT 2 // ... a float
#define tCHAR 3 // ... a charactor point
#define tFUNC 4 // ... a function
#define tALIAS 5 // ... an alias (list of commands seperated by semi-colon)
#define tPROC 6 // ... a function object

struct CmdInfo // Structure for a console command
{
void* var; // void pointer to the function/variable
int type; // the type of the pointer (see defines above)
char* szName; // the console name for the command
int params;
};

typedef std::map<std::string, CmdInfo*, std::less<std::string>, std::allocator<CmdInfo*> > STRING2CMDINFO; // defines a map of strings to cmdinfo pointers

class CConsole : public CBaseRenderer , public CBaseFuncProc, public CBaseConsole
{
public:

void RegCVar(char *sz, int *x); // Register an int with the console
void RegCVar(char *sz, float *x); // Register a float
void RegCVar(char *sz, char *x); // Register a char*
void RegCVar(char *sz, p_fnCMD x); // Register a function*
void RegCVar(char *sz, CBaseFuncProc *x, int num);

int QueryCVari(char* sz);
float QueryCVarf(char* sz);
char* QueryCVarc(char* sz);
void QueryCVar(char* sz, CText& ct);


private:

STRING2CMDINFO CVars; // All registered console commands

};

void CConsole::RegCVar(char *sz, int *x)
{
if (CVars[sz]) return;
CmdInfo *v = NEW CmdInfo;
v->var=(void *)x;
v->szName=sz;
v->type=tINT;
CVars[sz]=v;
}


That's more or less a copy paste from my old code, I've omitted a lot for the sake of clarity though. I hope you can see the idea though, I had a map of strings to commands, and anything that wants to expose a variable just called console->RegCvar. Processing input was a case of getting the pointer out of the map for the first word typed, and switching on the type variable. I gave all this up though for something more closely tide to my scripting system. That accesses internal variables via functions only, which is a lot safer.

Share this post


Link to post
Share on other sites
Stani R    1070
Quote:
Original post by Bezben I gave all this up though for something more closely tide to my scripting system. That accesses internal variables via functions only, which is a lot safer.


I was about to suggest using it for scripting like this too. Kind of how Neverwinter Nights does it, simulate a registry of variables on every scriptable object, and expose functions to store and read those variables.

Share this post


Link to post
Share on other sites
Basiror    241
i would store the whole registry entry content as an array if maximum n characters

a) to make it memory aligned
b) its just easier than double checking pointers everywhen you read it

and i would convert the data at runtime or build some special functions that return the converted data ...

and i would add a 32 bit flag for special purposes like modification rights and so on

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