Archived

This topic is now archived and is closed to further replies.

Accessing variables from console

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

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_fps
29.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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites