Static members in class
I have this code which i want called in a function, in my class, but it wont't work as i need a seperate set of variables for each time i call the function.
here is the code
I want this code in my add function of my class C_Cvars..
static ConVar Cvar_m(pName, pStartVal, NULL, Description);
ConVar *pCvar_m = &Cvar_m;
but if i want to add 2 or more then i get a crash because i need a different static ConVar for each time i add.
So how can i make a function which will use different variable names each time.
I thought of an array or macro.
I would really appreciate some help on this, thanks.
I'm not shure that I understand what you're trying.
looks like you trying to get the address of a local variable, but since that doesn't work you decided to make the variable static.
But now all your registered pointers point to the same location. Right ?
If that is your problen then you have to dynamically create the pointer to a ConVar.
Trouble is that you should delete that variable at some time. You will have to find a way to delete it if you don't need it amymore.
ConVar *pCvar_m = new ConVar;
thanks for the help, i did also try using it as an object but decided against that due to the deletion of object.
this code bassically adds a variable into a computer game externally.
but my problem is in order to add 2 i have to do this
obviously this is messy, and i want to avoid it.
static ConVar Cvar_enabled("PH_Enabled", "0", 0, "Enable or Disable ");
static ConVar Cvar_menu("PH_Menu", "0", 0, "Enable or Disable the menu!");
ConVar *pCvar_enabled = &Cvar_enabled;
ConVar *pCvar_menu = &Cvar_menu;
so i have an add function which i pas this info for each i want to add:
("PH_Enabled", "0", 0, "Enable or Disable the hack!")
but i need to have a seperate Cvar_m amd pCvar_m for each.
so simply having the info added multiple times will not work.
i need to change the add function so Cvar_m actually will have a different name each time so i get a variable for each time i add, not just use the same variable inside the function to store the data.
If you never want to add more then 2 variables then your approach isn't that bad.
If you want to make it more general then I would dynamically create that ConVar's and store the pointers in some container to be able to release them whenever needed.
i will have lots, so i want a streamlined approach lol.
and again, i don't want to have to predefine the pointers.
i just want the one function to do it all, no matter how many entry's i put in.
well i really hope that can be done.
Thanks for the continuing help.
You say that you register variables for an external game, so there should also be a function to deregister this varables again. So your approach to use static variables is not right anyway because this way you would never deregister your variables.
So I think the only clean way would be to dynamically create this variables, put the pointers into some container and deregister the variables before you delete them.
I'm not entirely sure how the variables get deregistered, but all i know is, if i try any other method of registering them, it will just result in a crash
Are you trying to modify Valve Software's Source engine by any chance? if so, don't touch the new operator, the engine handles all its memory management. If you try to go above its head, then weird stuff happens! The new operator is overloaded, and behaves differently to the way you'd normally expect - The entire SDK only uses 'new' once among its lebenty-zillion lines of code and several hundred .cpp files (Presumably a call which interfaces directly with the engine)
IMHO, the only way to do what you're asking is to either type that console variable stuff out individually, or design some hideous, ugly macro to do it for you (The SDK is already littered with more than enough mind-bogglingly incomprehensable macros as it is..)
Aside from this, static variables are probably the only way to create ConVar objects that the engine recognises - like most of the entity factory objects which the SDK uses, the engine needs to know the address of the ConVar before the DLL loads, and for various reasons, this can only be done using static initialisation.
(If you dig deep enough around the LINK_ENTITY_TO_CLASS macro, you'll see templated code which creates a unique static 'Factory' variable for every single Class which derives from CBaseEntity , and records the class name to the 'entity dictionary' container )
thankyou so much for that info. I will have a look later today, i did try making a macro for it but it still resulted in a crash. I will have a look at theirs though.
my macro code still results in a crash
this is it.
and i call it in this function
#define MACRO_CVARS(pName, pStartVal, Description) \
static ConVar Cvar_##pName(#pName, #pStartVal, NULL, #Description); \
ConVar *pCvar_##pName = & Cvar_##pName; \
void C_Cvars::AddCvar(const char* pName, const char* pStartVal, const char* Description)
MACRO_CVARS(pName, pStartVal, Description);
Out of curiousity, why do you need anything after the static declaration? As far as I can tell from here http://developer.valvesoftware.com/w...onsole_Control - you only need to declare that variable, and the rest is done for you..
well normally yes...
but I'm not making a mod that is loaded by the source engine. In this case, all convars would be registered yes.
But I am making an external program which hooks in and needs to use a cvar class to register it myself. I probably couldv'e hooked into the rigth spot to have the source engine look up my convars.. but its easier just to register them the way i am doing it.