x


Converting String to Variable Name

I want to have the user specify a variable name as a string, and then have him change it's value. This is my current code which fails to work, something expected due to my zero experience with hash tables. What exactly does this one need to work properly? I may note that this is a potiful attempt at it and should not be used as a base to improve, i fail to understand what Hashtables are.

 var Hash : Hashtable;
 Hash = new Hashtable();
 
 if (consoleInterpretation[0] == "ModVal") {
     for (var gameObj : GameObject in GameObject.FindObjectsOfType(GameObject)) {
         if (gameObj.name == consoleInterpretation[1]) {
 
               gameObj.GetComponent(consoleInterpretation[2])
                      .Hash[consoleInterpretation[3]] = 
                       int.Parse(consoleInterpretation[4]);
 
         }
     }
 }
more ▼

asked Jan 15, 2012 at 07:45 PM

yeoldesnake 1 gravatar image

yeoldesnake 1
619 40 35 46

Well, i mainly want it so that i can modify a variable's value through a console similar to the one in the TES games.

Jan 16, 2012 at 01:17 PM yeoldesnake 1

May I ask what you're using this for? It's not something that is commonly of any use in most systems and situations. This feels like a "What's the real question?" situation, where your own efforts to solve a problem have led to a rather strange set of follow-up questions.

Jan 16, 2012 at 05:53 AM MarkFinn
(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

Dynamic code management is a pretty obscure area. You'd be far better off creating an indexed array (Dictionary in C#, Hashtable in Java) of values and allowing the user change values in that table rather than using "real" variable names.

more ▼

answered Jan 16, 2012 at 05:52 AM

MarkFinn gravatar image

MarkFinn
1.5k 4 8 19

However, i have no knowledge of such methods.

Jan 16, 2012 at 01:16 PM yeoldesnake 1
(comments are locked)
10|3000 characters needed characters left

why would someone wanna do this? i can think of thousands of reasons. ive been struggling with this myself for some time. i am building a very advanced inventory system from scratch and it would make my life a lot easier if there was a format command to turn string into variable name. its not some mysterious thing its very basic....

more ▼

answered Aug 15, 2012 at 02:43 AM

gooncorp gravatar image

gooncorp
36 8 10 12

(comments are locked)
10|3000 characters needed characters left

Answered here: http://answers.unity3d.com/questions/37010/getting-variable-name-from-a-string.html

I would avoid it as it could lead to all kinds of unwanted hacking and intrusions.

more ▼

answered Aug 15, 2012 at 03:13 AM

DaveA gravatar image

DaveA
33.6k 419 346 487

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1403
x687
x230
x78
x30

asked: Jan 15, 2012 at 07:45 PM

Seen: 5328 times

Last Updated: Aug 15, 2012 at 03:13 AM