# Unity Accessing arbitrary variables at runtime

This topic is 4397 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, This is my first post and first of all let me say thanks for all the help the gamedev community has provided regarding programming (especially OpenGL). On to my question: I have a C++ program and I would like to be able to get and set variables while my app is running. What is a good way to provide access to program variables? For the sake of context, this is an OpenGL app that has enough variables that binding keys, etc is impractical. I've written a little console (like a simple version of what you get in Quake, etc when you press tilde). I can enter commands to check and change variables in the console. However, the way I've written it I have to code a separate function for each variable I want to get/set. When I parse/interpret the command line i resolve a function pointer, pass it the params and execture it. Is there some better (more general way) to do this? I'm sure others have this type of functionality in games or other programs. What approach have you used / do you suggest? Has anyone gone so far as to create a simple debugging environment (like a lightweight gdb) or something like an interpretive language interface, where you can call your functions directly? Thanks in advance, Frank.

##### Share on other sites
You could do it in a general way using .Net reflection, but that obviously only works if you're using a .Net language.

You could also do it in a general way if you expose things through a scripting language and have your console execute that scripting language.

I don't have any experience exposing my code through a scripting language, though, so I don't know if there's any languages or tools that would let you automatically expose everything.

You could also store every variable you want to be able to modify in a hash table indexed by a string and have your console automatically look up the variable in the hash table.

##### Share on other sites
For your console class I could imagine something like

Console.BindInt( "PlayerX", &player->x );

And then from the console, while in the game, you could type

SET PlayerX 400

##### Share on other sites
It's pretty simple to do a basic command line in the program, you could (for example) have something like std::map<std::string, boost::function<void (const std::string&)> >, then tokenize each command line by spaces and pass to the function described by the map at the first string in the command.

##### Share on other sites
Hi,

Thanks for the help. .Net reflection looks pretty powerful. Alas, I'm not using .Net. I implemented a map for functions (get, set, etc) and a function to bind variables, as recommended. It's definitely better than a single function for each variable!

I'm now trying to template it to avoid creating BindFloat, BindString, etc..., and perhaps to avoid using switches in the my get/set/print... functions (and to learn about templates). I've created a function:

template <typename T>
Console.Bind(string & const name, T *ptr, boundVar_t type)

But it seems silly that I need the boundVar_t tag. Is there a better (implicit) way to keep track of the type so I can do the appropriate thing with the data later? Can T be saved directly somehow?

Thanks again,
Frank

Here's are some snippets from my current implementation:

// type tag
typedef enum {
intt, floatt, stringt // must specify every possible type :(
} type_t;

typedef struct {
string name;
type_t type;
void *ptr;
} boundVar_t;

//: Bind variable
void Console::Bind(string const &name, T *ptr, boundVar_t type)
{
boundVar_t newBind;
newBind.name = name;
newBind.type = type;
newBind.ptr = (void*) ptr;
variableList.push_back(newBind);
}

// ... somewhere in Console.Set

switch (variableList[hash].type) {
case intt:
*((int*)variableList[hash].ptr) = (int)valueParsedFromCommandline; break;
case floatt:
*((float*)variableList[hash].ptr) = (float)valueParsedFromCommandline; break;

// ...

##### Share on other sites
Im not sure what you were looking to do, How about you wrap the variables themselves in templates, some sort of convinance class.

template<class T>class BindVar{    BindVar(std::string, *T);    Get();    Set(T);    T *item}// Usageint *variable_to_bind = myengine::getvar();BindVar<int> bv = new BindVar<int>("Master", variable_to_bind);Console::Get().AddToHash(bv)

##### Share on other sites
I suggest investigating boost libraries, in particular Boost.Variant.

##### Share on other sites
Hi,

When etothex originally mentioned boost, I didin't look at it very carefully. I've now read the tutorials. It looks very useful, thanks snk_kid. I'll try it your way first, Paul, then I'll try boost.

Thanks again,
Frank

##### Share on other sites
I'd use a vector of variants, and a map of value names to variant indicies.

##### Share on other sites
Quote:
 Original post by snk_kidI suggest investigating boost libraries, in particular Boost.Variant.

Oh, hell yes!

I like to think that I have a bit of experience in this as I've spent the last month and a half writing my console, and I'm just finishing it up for functions.

For variables, I'm using boost::variant, and I'm storing them in a std::map<std::string, boost::variant> object. The variant can be a pointer, and I've created the visitors, so when it is a pointer, it's de-referenced and value it points to is get or set.

• 23
• 15
• 23
• 10
• 19
• ### Similar Content

• Ok, firstly, Hi.

This is my first post on this forum. I am an Indie Dev making my first game so bear with me when I say dumb stuff, I'm on a huge learning curve.

My first question is about inventory systems for unity. I am trying to make a survival type game with crafting. I have purchased Inventory manager pro by devdog from the unity asset store and it seems like a pretty powerful assett but for an intermediate coder its a little tough to use.  I'm beginning to wonder if it was the right purchase.
So my question is.... does anyone have any experience of inventory plugins / systems for unity and can anyone reccomend a system to me?
It needs to have the following: Loot system, crafting system, character sheet, blueprint system,  character stats system. Ideally with as little coding as possible.

Thanks

• I've got a bug with my brick breaker style game. The bricks move down one line at a time ever 1.5 seconds. What appears to be happening is occasionally the ball will be just about to hit the brick when the brick moves down a line, and now the ball is behind it. I'm not sure how to fix this. I have two ideas but I'm not sure of implementation. 1 solution would be to check where they were and where they are going to be before rendering the frame. Then if they crossed paths, then register the brick as hit. Solution 2 would be change how the bricks move. I could maybe slide them down line by line, instead of a jump down. I'm not sure of this will fix the issue or not. Any ideas?
• By Pixeye
I wrote an extension for unity inspector that allows to group/fold variables.
Available on github  , cheers!

• I'M interested in programming tools (For animation, UI, etc). Can anyone suggest me the resources where I can start learning or which technologies I need achive it.

Thanks,
Rakshit
• By Paszq
Group photo of some of the characters and creatures currently living in Arpago