Turn Strings[] into vars[]?
Asked Answered
M

1

0

Okay, I guess I'll make it sounds way more complicated than it actually is but here it goes:

I have a built-in array of Strings and then want to retrieve and manipulate the according variables on the script. ie:

var myVar1:int;
var myVar2:String;
var myVar3:float;
var varNames:String[] = new String[3];
//I go in Inspector and set varNames to ("myVar1","myVar2","myVar3"), then:
for(i=0;i<varNames.length;i++){
    print(varNames <em>+ " = " + **varNames_.ToVar()**);_</em>
_*}*_
_*```*_
_*<p>Of course .ToVar() do not exist, this is what I need to find.*_
_*While I'm at it: .ToType() is something I could definitly use, too (same pseudo-code).</p>*_
_*<p>Once again: I'm trying to access a variable having only it name as a String.</p>*_
Manifold answered 3/7 at 8:51 Comment(2)

You almost certainly don't actually want to do that. (Though technically it's possible using reflection or eval().) Generally these sort of situations should use arrays or enums.

Hagood

woot! eval(myString) is exactly what I was looking for. Many thanks!

Manifold
S
0

There is no "easy" way of doing that and it's not recommended to do such things. In most cases there are better solutions. I don't know what you want to achieve in the end but one way would be to use .NET/MONO Reflection. If you really want to use reflection i would recommend C# and visual studio just because of autocompletion and intellisense.

But as i said that's not the way you should go. Reflection is quite slow and if you don't know what you're doing you can even crash your game. Maybe tell us what exactly you want to achieve and we find the best solution.


Edit

In this example I use 4 scripts (for my predefined property types). You can drag&drop one or more properties to a gameobject and give them a name. I use the component system of unity to search for them. Thanks to the base class “IProperty” you can set the value with get() and set() as string.

// IProperty.js  - should not be added to any gameobject, it's just the base class
class IProperty extends MonoBehaviour
{
    var Name : String;
    function Set(aValue : String) { } ;
    function Get() : String { return ""; };
}
// ****************************************************************************
// IntProperty.js
class IntProperty extends IProperty {
    var value : int;
    function Set(aValue : String) {
        value = int.Parse(aValue);
    }
    function Get() : String {
        return value.ToString();
    }
}
// ****************************************************************************
// FloatProperty.js
class FloatProperty extends IProperty {
    var value : float;
    function Set(aValue : String) {
        value = float.Parse(aValue);
    } 
    function Get() : String {
        return value.ToString();
    }
}
// ****************************************************************************
// StringProperty.js
class StringProperty extends IProperty {
    var value : String;
    function Set(aValue : String) {
        value = aValue;
    } 
    function Get() : String {
        return value;
    }
}

To access the variables:

private var properties : IProperty[] = null;

function GetPropertyByName(aName : String) : IProperty
{
    if (properties == null)
        properties = GetComponents.<IProperty>();
    for (var P : IProperty in properties)
    {
        if (P.Name == aName)
            return P;
    }
    return null;
}

var health : FloatProperty = null;

function Awake()
{
    health = GetPropertyByName("Health") as FloatProperty;
}

function Start()
{
    // fast, direct way
    health.value = 100.0f;
    // slower, dynamic way
    GetPropertyByName("Health").Set("43.3");
}

Well, that's just a concept. The function GetPropertyByName should also be boxed in a strategy-pattern and all scripts that have dynamic variables should implement that interface. Finally i didn't turn IProperty into an interface just because it easier with the "Name" var ;). In general the interface should implement a GetName() function. But as i said, it's just a concept.

Stridor answered 21/2, 2011 at 0:46 Comment(3)

I've read a little and I'd definitly avoid eval() especially since I run into problems evaluating more complex entry. What I need is a script where team members enter aScript,aVariable, and aValue as String. Then the script will access and compare/set defined variable, as part of a system. I've got everything working with int and float, but the String/var syntax is giving me a headache.

Manifold

What kind of variables? What's the difference between those variables? What's the type of those variables. I've written a "small" solution for that kind of problem. It was quite annoying to use JS but in the end it wasn't too different to C# ;). I will add it to my answer

Stridor

It ends up being the most stupid mistake of them all. I had the int variables working all along and couldn't figure what was wrong, after numerous tests....... the variable I was accessing was a reserved keyword therefore throwing an error x( . I'm ashamed, but also thankful to everyone here for their time. Especially Bunny83, I might inspire from your code...

Manifold

© 2022 - 2024 — McMap. All rights reserved.