[SOLVED] Stop script function in new playing state, and access script from another script?


#1

Hello, I have two questions. If these questions are simple, I’m sorry. I’m a new coder and things are hard to understand for me. My first question is: is there any way to access a script from another? I have tried this.

var app = this.app;
var Game = app.root.findByPath('/scripts/game.js');

However, this did not work, and I suspect it’s because you can’t navigate files like that. I have also tried many other things that haven’t worked either (deleted code), and I’m completely stuck.

My other question is, is there any way to stop a script when entering a playing stage? ( this.state = 'playing'; ). I am trying to stop a sine effect on an entity once it enters this stage, I’ve tried

var Sine = pc.createScript('sine');

Sine.attributes.add('amplitudeScale', { type: 'number', default: 1 });
Sine.attributes.add('frequencyScale', { type: 'number', default: 1 });
Sine.attributes.add('startEvent', { type: 'string', default: 'start' });
Sine.attributes.add('stopEvent', { type: 'string', default: 'stop' });


// initialize code called once per entity
Sine.prototype.initialize = function() {
    this.timer = 0;
};

// update code called every frame
Sine.prototype.update = function(dt) {
    dt *= this.frequencyScale;
    this.timer += dt;
    this.entity.setLocalPosition(0, Math.sin(this.timer) * this.amplitudeScale, 0);
};

var app = this.app;
var Game = app.root.findByPath('/scripts/game.js');
// var game doesn't work
var running = true;

if (running) {
   running = true;
}

if (!running) {
   running = false;
}


// this.on('enable', this.onEnable, this);
// this.on('disable', this.onDisable, this);

// this.onEnable();

// Sine.prototype.onEnable = function () {
// this.app.scripts('sine.js').enabled = true;
// };

// Sine.prototype.onDisable = function () {
// this.app.script.enabled = false;
// }

Any help is appreciated, I’ve been trying to do this for awhile now, thanks!


#2

In terms of scripts/components accessing properties from others, this post may help: How to refer to different parts of the API while scripting

Have you also gone through the Keepy Uppy tutorial series? https://developer.playcanvas.com/en/tutorials/keepyup-part-one/


#3

Thank you for your reply. I will read through these and see if they help :slight_smile: