Understanding Var

I was looking at a tut ,I came across a slight confusion
image

I created an attribute boxes with a type as ‘entity’

Now you can see line 17
there Boxx is defined as var to duplicate at each for loop

at line 18 ,19 confusion starts

do Boxx now act as entity ???

also for line 17 and 18 there is not this. with Boxx ?? Why is it

On line 17, you’re using the clone() method. clone() creates a deep copy of the this.boxes Entity. It duplicates the full entity hierarchy, with all components and all descendants. This is why line 18 and 19 allow you to set the enabled property and call the setPosition() method.

1 Like

thanks man ,what about

also for line 17 and 18 there is not this. with Boxx ?? Why is it

You want to use this. when you need to access the script’s properties and methods. For example, on line 3 you’ve added a ‘boxes’ attribute to your script. When you need to access this in the BoxxxManager script’s methods, you would use this.boxes as you have successfully done on line 7 and 17.

The difference with line 17-19 is that each of the 10 the cloned boxes are not being saved as a property to be accessed later so there is no reason to use this.. Hypothetically, if you wanted to clone a box and then access it later on in your script, you could do this:

this.clonedBox = this.boxes.clone();
this.clonedBox.enabled = true;
this.clonedBox.setPosition(PosX, PosY, PosZ);
this.entity.addChild(this.clonedBox);

You could then access this.clonedBox again elsewhere in your script.

** Just be aware the above example code won’t work inside your for loop, it would keep overwriting this.clonedBox every loop.

1 Like

Thanks a Bunch Man ,this helped a lot :pray: :pray: :pray: :pray:

1 Like