[SOLVED] Using a variable from 1 script in a different one (or using flipx in a if statement

I’m trying to have a bullet go in the direction of the player but i have 2 choices to do it.

  1. have a variable in the playermovement script that changes when you press a key
  2. have a if statement that checks the player’s flipx data.

Project link:
https://playcanvas.com/editor/project/585024

It’s flipX rather than flipx https://developer.playcanvas.com/en/api/pc.SpriteComponent.html#flipX

i mean it in something like this

if(entityvar.flipX===true)

this is what im trying to do

var PlayerBullit = pc.createScript('playerBullit');
var yes = yes;
// initialize code called once per entity
PlayerBullit.prototype.initialize = function() {
    var play = this.app.root.findByName('player');
    
    if(play.sprite.flipX===false){
        yes =  1;
    }
    return yes;
};
// update code called every frame
PlayerBullit.prototype.update = function(dt) {
   if(yes===1){
       this.entity.translate(0.2,0,0);
   } if(yes===0){
       this.entity.translate(-0.2,0,0);
   }
};

Ah, I see!

I would do it slightly differently. I have a direction variable on the bullet class rather than it looking for the player entity and set the direction when it gets cloned based on the current player direction.

This way, the bullet is completely independent of the player so you could use the script to fire enemy bullets or from turrets, etc.

Example: https://playcanvas.com/editor/scene/661927

was i just over complicating it?

Just think of it as a different way. I personally don’t want to couple the player with the bullet when the bullet script could be used for bullets from enemies, turrets etc.

The issue in your code is that you are using global variables (the yes variable) rather than object scoped ones so all the bullets would go the same direction.

so the var this.directon is located in the bullit code but the player code modifys it?

In Javascript, everything is accessible (technically).

The direction variable in the bullet script is accessed and changed in bullet cloner script when the bullet entity is cloned:

        // Set the direction of the bullet
        e.script.playerBullit.direction = this.playerentity.script.player.getPlayerDirection();

I’ve added a function to the player script to get it’s direction which you see being used above in the bullet cloner script.

Player.prototype.getPlayerDirection = function() {
    if (this.entity.sprite.flipX) {
        return "left";
    } else {
        return "right";
    }
};

The main reason for this is now the logic for whether the player is facing left or right is in the player script so it can be modified if the player graphics or animation is changed. Which means that the bullet cloner can be left un touched.

it is just im am new to this engine and mediocre at implementing js (although i do know it)

so put ‘this.’ in front of anything and you can call it from anywhere?

and how to you check if a entity is in front of a camera?

The pc.script are Javascript classes (https://www.digitalocean.com/community/tutorials/understanding-classes-in-javascript) so the this is referencing the instance of the class.

If you have used C++ or C# before, it’s the same concept.