Accessing variables from console
I wanted to incorperate a Quake type console into my game to allow me to debug stuff and what not. I have everything working in the console itself. It draws properly on the screen, handles input, scrolls text and can have messages sent to it.
The next part is ovbiously to allow me to set different variables inside the game to different values while in the game. I have pondered this for a while and haven''t come up with anything except hard-coding everything in. This is time-consuming and less then easy on readability.
My question is this. How can I handle such console commands as "set Fps_Counter 1", "set Sound_Output 0" and the like. Is there any way that I can have one function that can deteremine if the Fps_Counter is a valid variable inside the game, and then set it to the passed value? I will only be working int''s and bool''s atm. Any ideas, or I am a sol?
Any help would greatly be appreciated.
------------------------------
"I''m a decorated astronaut, I don''t make those kind of mistakes."
"Oh now wait a minute. Look I''ll show ya. I''ll enter the same calculations using what we like to call ''The Right Way''."
-Rem
Shit, I triple posted. Sorry
------------------------------
"I''m a decorated astronaut, I don''t make those kind of mistakes."
"Oh now wait a minute. Look I''ll show ya. I''ll enter the same calculations using what we like to call ''The Right Way''."
-Rem
------------------------------
"I''m a decorated astronaut, I don''t make those kind of mistakes."
"Oh now wait a minute. Look I''ll show ya. I''ll enter the same calculations using what we like to call ''The Right Way''."
-Rem
I recently implemented a Quake-style console for our current game. Functions and variables are registered with the console at application initialization. Note that I have no hard-coded functions or variables in the console at all. Everything is registered via two API functions.
To register a variable, eg. frame rate, I use this
Variables are stored in a structure similar to the following.
In the console at runtime, to see the value of a variable, I simply type the name of the variable, eg.
To change the value of a variable, simply append the new value. This example shows the profile graphs on screen.
I also have the ability to add a variable watch to the screen. This shows the value of the variable in a list at the top-right of the screen at all times.
To display the value of the variables, I use a function that checks the type of the registered variable and formats the output appropriately.
To simulate the ''set'' command, I would register a function with the console.
and then implement the function as follows.
I hope that helps.
Steve ''Sly'' Williams Code Monkey Krome Studios
typedef void (*ConFunc)(int argc, char *argv[]);CON_RegisterCmd(char *name, ConFunc func);CON_RegisterVar(char *name, void *data, int type);
To register a variable, eg. frame rate, I use this
CON_Register("r_fps", &frameRate, CONTYPE_FLOAT);
Variables are stored in a structure similar to the following.
typedef struct tagConVar{ char *name; void *data; int type;} ConVar;
In the console at runtime, to see the value of a variable, I simply type the name of the variable, eg.
]r_fps29.88
To change the value of a variable, simply append the new value. This example shows the profile graphs on screen.
]r_profile 1
I also have the ability to add a variable watch to the screen. This shows the value of the variable in a list at the top-right of the screen at all times.
]addwatch r_fps
To display the value of the variables, I use a function that checks the type of the registered variable and formats the output appropriately.
switch (type) { case CONTYPE_INT: itoa(*(int *)data, buffer, 10); break; case CONTYPE_FLOAT: sprintf(buffer, "%3.2f", *(float *)data); break; case CONTYPE_BOOL: strcpy(buffer, *(bool *)data ? "true" : "false"); break; case CONTYPE_STRING: strncpy(buffer, (char *)data, size); break; case CONTYPE_FUNC: strncpy(buffer, "(function)", size); break; } return buffer;
To simulate the ''set'' command, I would register a function with the console.
CON_RegisterCmd("set", CmdSet);
and then implement the function as follows.
void CmdSet(int argc, char *argv[]){ if (argc > 2) CON_SetVarAsString(argv[1], argv[2]); else CON_Print(CONMSG_ERROR_TOOFEWARGUMENTS);}
I hope that helps.
Steve ''Sly'' Williams Code Monkey Krome Studios
Ok. That gives me a better idea of how to approach this problem.
I appreciate it.
------------------------------
"I''m a decorated astronaut, I don''t make those kind of mistakes."
"Oh now wait a minute. Look I''ll show ya. I''ll enter the same calculations using what we like to call ''The Right Way''."
-Rem
I appreciate it.
------------------------------
"I''m a decorated astronaut, I don''t make those kind of mistakes."
"Oh now wait a minute. Look I''ll show ya. I''ll enter the same calculations using what we like to call ''The Right Way''."
-Rem
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement