- Home /
What is typed variable?
Hi Guys,
1) What is typed variable?
2) And Examples of it?
3) which part of my code can be replaced with typed variables?
4) and how does it speed up my game scene process?
5) please do let me know if there's anything else I need to know in order to understand typed variables better, thanks!
I got this question from my previous question, one of person suggested to me to use typed variables, but I don't know what is it.
http://answers.unity3d.com/questions/282486/what-does-this-for-loop-mean.html
Below is coding( in javascript / unityscript) :
private var ttag = "enemyAim";
private var target : Transform;
var closestEnemy : Transform = null;
var dist : float;
function getClosestEnemy () : Transform
{
var taggedEnemys = GameObject.FindGameObjectsWithTag(ttag);
var closestDistSqr = Mathf.Infinity;
var closestEnemy : Transform = null;
print("get closestEnemy");
for (taggedEnemy in taggedEnemys) // this part (1)
{
var objectPos = taggedEnemy.transform.position;
dist = (objectPos - transform.position).sqrMagnitude;
if (dist < 3.0)
{
if (dist < closestDistSqr)
{
closestDistSqr = dist;
closestEnemy = taggedEnemy.transform;
}
}
}
target = closestEnemy;
}
Thanks in advance!
Uhm . . . All variables have types . . . Are you asking about a specific type, or . . .?
$$anonymous$$aybe take a look at this for more information: http://docs.unity3d.com/Documentation/ScriptReference/index.$$anonymous$$ember_Variables_26_Global_Variables.html
Answer by Eric5h5 · Nov 04, 2012 at 11:43 PM
All variables are automatically typed for you, as long as you supply a value. You can optionally add types to your code where it doesn't have them. For example:
private var ttag = "enemyAim";
can be written as
private var ttag : String = "enemyAim";
However this has no actual performance benefit, since both lines of code compile to the exact same thing. The compiler sees that you are assigning a string to the ttag variable, therefore it is typed as String. Writing out the type may help you understand the code better in some cases, but it's optional. The only thing you should avoid doing is neglecting to supply both a type and a value, such as this:
var foo; // bad!
This can still work in some cases (desktop platforms where you're not using #pragma strict), however it will be quite slow since the type has to be figured out at runtime. As long as you supply a type or a value (or both), then you're fine.
(The person you're referring to in the comments to your other question is mistaken, by the way. All of your variables are typed.)
@Eric5h5 , thanks Eric! I was wondering what did I didn't declared as type! heheh thanks again!
your explanation really helped me in understand e var type better and what not to do!
For others, for question 1 & 2, please see answer above too!
Answer by Demigiant · Nov 04, 2012 at 09:58 PM
1) A typed variable is a variable that defines its type (Transform, Vector3, etc) explicitly. It is defined by adding ":VariableType" after the name of the variable.
2) For example, this is a variable:
var myPosition;
While this is a typed variable:
var myPosition:Vector3;
3) Everything that says "var" con be assigned a type, as shown above
4) If the engine knows the type of a variable, it won't need to lose time to "guess" it, which leads to a faster performance. Also, you'll notice that more errors are reported to you while you code instead than while you run the game, which is very good.
@$$anonymous$$e , thanks! it really helped me in understand my question's answers very clearly! I wish I could mark 2 answers but I cant. So I thumb this one up.
For others, for question 3 & 4, please see below answer.
Your answer
Follow this Question
Related Questions
How could you access a script of varying name? 5 Answers
Variable Type for an Instance of any Script 2 Answers
Lower player's health from separate script. 2 Answers
Variable Not Changing. 0 Answers