How to call other script file functions?

Hi,
I need in the UI.js calls a function in another script,
app._openDoor.toggle(); but be prompted to “app._openDoor.toggle is not function”.
I try to : app._openDoor.fire(‘enterCar:toggle’); and app._openDoor.on(‘enterCar:toggle’);
but can’t find the function toggle().

I use a lot of time to solve it without success.

1 Like

Hi yangcj, the ‘app’ object is part of the ‘this’ object in the component script. Without looking at your code, I guessing that you will just need to add ‘this’ at the beginning of those lines of code involving the fire and open event calls.

eg.

this.app.openDoor.fire('enterCar:toggle');

This is assuming that ‘openDoor’ object has been assigned to the ‘app’ object.

If you want to call a function from another script that is attached to an entity, you need to have a reference to the entity to get to the script instance. Eg.

door.js

var Door = pc.createScript('door');

Door.prototype.open = function() {
    // Do something in function
};

trigger.js

var Trigger = pc.createScript('trigger');
Trigger.attributes.add('doorA', {type: 'entity'});

Trigger.prototype.update = function() {
    if (/*something*/) {
        this.doorA.script.door.open();
    }
};

If you are still have problems with this, can you provide a link to your project please? It will help us solve your problem faster :slight_smile:

3 Likes

Thank you very much for your detailed answers. I’m trying to modify again.
If I are still have problems with this ,I request your help again.

Development consists loads of debugging - process of finding and investigating issues and information in your code.
Please refer to this page: http://developer.playcanvas.com/en/user-manual/scripting/debugging/ that helps to get used to dev tools in your browser, that allows you to debug your code. This will help you to understand what is going on better.

this.app.openDoor.fire(‘enterCar:toggle’);
This is assuming that ‘openDoor’ object has been assigned to the ‘app’ object.

Can you specify how we can attach a script to the “app” object?