- Home /
What does the word for mean in javascript?
I am just starting in JavaScript and Unity. I came acrost this code in the FPS_tutorial. It finds all the RididBodys near a collision and blasts them away from collision. Here is the code.
//Find all nearby colliders
var colliders : Collider[] = Physics.OverlapSphere( transform.position,
explosionRadius );
//Apply a force to all surrounding rigid bodies.
**for( var hit in colliders )
{
if( hit.rigidbody )
{
hit.rigidbody**.AddExplosionForce( explosionPower,
transform.position, explosionRadius );
}
}
//If we have a particle emitter attached, emit particles for .5 seconds
if( particleEmitter )
{
particleEmitter.emit = true;
yield WaitForSeconds( 0.5 );
particleEmitter.emit = false;
}
The part I do not understand has two stars around it(which I added). What is the "for()" doing? Is it like a "if()"? Why do I not see the "hit in colliders" variable anywhere but in the the "for()"? What is asked in the if statement "if(hit.rigidbody)"? Is "hit" a list of things that have collided with something? Is that why the code says "hit.rigidbody.AddExplosionFroce()" to add a explosion force to rigidbodys that have been hit? Please don't assume I know anything about Unity or JavaScript in your answers.
Answer by Statement · Dec 11, 2010 at 10:38 PM
Clean your code, so it is easier to follow.
for( var hit in colliders )
{
if( hit.rigidbody )
{
hit.rigidbody.AddExplosionForce( explosionPower, transform.position,
explosionRadius );
}
}
What is the "for()" doing?
It loops through every collider in colliders. The variable hit is for every loop assigned to the next collider. It is the same as:
for( var i = 0; i < colliders.Length; ++i ) { var hit = colliders[i]; // This is your "var hit in colliders"
if( hit.rigidbody )
{
hit.rigidbody.AddExplosionForce( explosionPower, transform.position,
explosionRadius );
}
}
Is it like a "if()"?
No, but it does have a scope (the { and }) so it looks very much like an if. The for will repeat the code inside the scope for every item in your collection.
What is asked in the if statement "if(hit.rigidbody)"
It checks if the collider has a rigid body. It is the same as calling "if(hit.rigidbody != null)".
Is "hit" a list of things that have collided with something?
No, hit is one of the objects that you have in your collider collection. Since it is in a loop, hit changes each time the loop is run so it will repeat for every object in your collider collection. Just think that "hit" is going to be "all" your colliders, one at a time in the loop.
Is that why the code says "hit.rigidbody.AddExplosionFroce()" to add a explosion force to rigidbodys that have been hit?
Yes, it applies an explosion force to every rigid body that was attached to a collider in the collision test.
I think you could do some simple tests to boost your confidence in what the code is doing. Try this:
function Start() { // Create a collection of strings var collection = new String[3];
// Put some data in it
collection[0] = "Hello";
collection[1] = "For";
collection[2] = "Loop";
print ("Test 1");
// Test the for with enumeration
for (var each in collection)
{
print ("each = " + each);
}
print ("Test 2");
// Test the for with explicit control,
// backwards for fun!
for (var i = collection.Length - 1; i >= 0; --i)
{
var each = collection[i];
print ("i = " + i);
print ("each = " + each);
}
}
The output for the first test:
Test 1 each = Hello each = For each = Loop
and the second test:
Test 2 i = 2 each = Loop i = 1 each = For i = 0 each = Hello
Thank you for answering my question. I am sorry I can only understand simple code right now. That is why I am asking questions so I will soon understand. So are you saying that once a "for()" is called the code continues and everything in the"()" loops? Is the variable "hit in colliders" one variable or is it "hit" "in" "colliders"? What I mean is, is "in" something like "=". If not, that would that mean that "var hit in colliders" is set to nothing every loop? Is hit a variable we make in this code or does it have a preassigned value, like transform does.
One more thing, What is the "[]" after the "Collider" type of variable "collider". I have never seen something like that. I searched it the Unity scripting help but did not find anything. What is it?
Both are good questions. Let's start with the "Collider[]" part first. The "[]" in that means that the variable "colliders" is an array. An array just means it holds many of one thing as opposed to one of one type of thing. Now, on to the "for()" part. Since you have many of one thing, you need to look at each one and consider it. This is why you have "for(var each in colliders)". This is saying, "for each of collider that I have in the colliders array, run this code between the brackets". Does this make a bit more sense now? You might also want to read up on: http://en.wikipedia.org/wiki/
O$$anonymous$$, thank you, I understand what a "for()" is now. But what is a "hit"?
In your example, hit is a collider. It is just a variable taken from the colliders array. I think it is improperly named in this case and I agree "hit" raises some questions if you're new to for loops. If you rename "hit" to "collider", does it make more sense to you? for (var collider in colliders) { if (collider.rigidbody) .....