[SOLVED] Changing object Properties outside of attached script

I had a question about my project I am trying to set the username and animations in a scrip that isn’t attached, for instance: in this project I’m trying to set the animation but don’t have any idea on how to do it. I saw that there are some forum posts about bones like this one by @ayrin where you can get the model entity, can you set animations by doing the same thing?

Network.prototype.movePlayer = function(data) {
   if (this.initialized && !this.players[data.id].deleted) {
       if(oldData === null){
           this.players[data.id].entity.rigidbody.teleport(data.x, data.y, data.z);
       }else{ 
           this.players[data.id].entity.rigidbody.teleport(new pc.Vec3().lerp(new pc.Vec3(oldData.x, oldData.y, oldData.z),new pc.Vec3(data.x, data.y, data.z),0.25));
       }
       this.players[data.id].entity.setEulerAngles(data.rotx, data.roty, data.rotz);
       this.player[data.id].entity.children[0].entity.animation.set(data.currAnim); // Set animation to remote player.
       console.log(this.player[data.id].entity.children[0]);
       
       oldData = data;
   }
};

Also thanks in advance, this community is very supportive.

Hi @Scottss,

Not sure what’s in data.currAnim, but there isn’t any set() method available on the animation component.

https://developer.playcanvas.com/en/api/pc.AnimationComponent.html

To play an animation, you use the animation name (e.g. Jog Left) and you can also set an optional blend time (how long it takes to blend in from the previous animation playing):

this.entity.animation.play('Jog Left', 0.3);

If the question about how to access the different entities in the world, you can use the findByName, findByTag, etc calls: https://developer.playcanvas.com/en/api/pc.Entity.html#findByName

Generally, people use those functions either on the this.app.root graphNode to search the whole scene or on the current entity to find a particular child entity.

Alternatively, you can use script attributes to directly reference another entity in the scene here: https://playcanvas.com/editor/scene/501307 where the root entity has reference to the player and camera entity.

Once you have the entity, you can access any script or component on it. See more here: How to refer to different parts of the API while scripting

Hi @Leonidas,

Thank you for your reply!

data.currAnim = this.app.root.findByName("Model").animation.currAnim;

it gets the current animation and sends it to the socket server. I need a way to set the animation to the remote players.

Hi @yaustar I appreciate your response,

Does that mean you can access the entity.animation.play(); function via findByName? I’ve tried that way with no luck.

Im going to look in to the How to refer to different parts of the API while scripting that you mentioned.

Using your project as an example:
image

The PlayerMovement script is on the Player entity and the model/animation is on child entity Model.

To access the animation component on the Model entity from the script on the PlayerMovement script we first would have to find the Model entity:

var modelEntity = this.entity.findByName('Model');

Now can access the animation component on the Model entity via the modelEntity variable:

modelEntity.animation.play('Jog Left', 0.3);

Yaustar,

I reworked the code
That worked! thank you!