|
How can I reference a public variable in a class NOT using dot syntax, but some way where I can use a dynamic variable name? For example, if I have an object like this: I know I can reference speed like this: but I want to reference it like this:
(comments are locked)
|
|
You could use a HashTable, like an array where the index can be a string. Or look into 'eval' on the MSDN
(comments are locked)
|
|
You could easily do what you want using reflection, but I think it would introduce a performance hit and increase the size of your executable. I believe it's possible to override the [] operator in C# (if you must use that exact syntax) but I'm not sure. Just out of curiosity... Why do you ask? Is it because you're trying to translate and/or work with php code? It's definitely possible to override the [] operator in C#, but yeah, the question still remains- why?
Nov 09 '11 at 10:28 PM
syclamoth
@syclamoth: It's not really possible to overload the [] operator in C# but you can define an indexer which looks quite similar. That's what the Hashtable / Dictionary uses.
Nov 09 '11 at 10:51 PM
Bunny83
I've been working in Flash so I'm used to throwing just about anything into arrays and being able to use one of set of functions to find things such as all the visible display objects, all data objects containing certain values, etc.
Nov 09 '11 at 11:03 PM
davedev
I won't claim to be Mister Optimization, but I do know you can expect a performance hit if you're constantly using string values to reference variables. I suspect that doing what you want to do might go against the whole point of using a game engine: code optimized for graphics. I would think it might be pretty easy to implement some of these handy functions yourself (in a way that fits Unity). I think php array/hashtables are awesome... But they're for php projects.
Nov 09 '11 at 11:20 PM
jahroy
(comments are locked)
|
