How can I call a test method from self hosted?

I download my project and I have a method which I can call it like:

app.fire('colliders:test', 'Whatever string');

What the method does:

Colliders.prototype.test = function(text)
{
    alert(text);
    console.log(text);
};

index.html

<body>
    <script src="__modules__.js"></script>
    <script src="__start__.js"></script>
    <script src="__loading__.js"></script>
    <script>
        var app = pc.Application.getApplication();
        app.fire('colliders:test', "Entro");
        app.on("start", function () {
        // get the root of the scene.
        var hierarchy = app.root.getChildren()[0];
        console.log(hierarchy);

        });
    </script>
</body>

But at the moment that I run the project, It doesn’t work. I hope you can help me.

Hi @Axel_Saucedo, you are almost there.

In your Colliders script you need to attach an event listener that is calling as a response the test() method:

Colliders.prototype.initialize = function(){
   this.app.on('colliders:test', this.test, this);
}

Now in your index.html, you need to wait for the pc.Application initialize method before firing any script events. That is called after all script initialize methods have finished executing (so your event handler has been attached):

    <script>
        var app = pc.Application.getApplication();
        app.on('initialize', function(){
            app.fire('colliders:test', "Entro");
        });
    </script>
1 Like

Thanks!! It worked!!

1 Like

Sorry to bother you. Do you know if you can create a playcanvas method inside the html?

It depends on what you are trying to do, but generally you can access anything referenced by your pc.Application instance in the same way:

<script>
   // for example let's find an entity and disable it in the hierarchy
   pc.app.root.findByName('My Entity').enabled = true;
</script>
1 Like

Let’s say I want to create a method outside playcanvas to get certain data. For example:

index html
var Test = pc.createScript('test');

Test.prototype.initialize = function()
{
      this.app.on('test:getData', this.getData, this');
}

If it possible something like this?

playcanvas

var Colliders = pc.createScript('colliders');

Colliders.prototype.someMethod = function()
{
     //Some actions
     data = this.entity.getPosition();
    this.app.fire('test:getData', data);
}

You can do that but it does mean that you can’t add the Test script in the Editor and only at runtime.