How to create a custom function

This is one of the most simplest thing but is not mentioned on any where on any manual on playcanvas and its pretty frustrating
That is How to create a custom function in playcanavas

var Collectable = pc.createScript('collectable');

// initialize code called once per entity
Collectable.prototype.initialize = function() {




I am trying to delete an object upon collosion using custom function ,the fact is I know how to dlt it but I wanted to understand concept of using custom functions .This is giving an error that dltt function is undefined .

I must say user manual is very badly written when it comes to scipting

Hi @Nofil_khan!

You need to use this.dlttFunction() when you call your function.

1 Like

Try it like this for completeness, you need to set the context too:

this.entity.collision.on('triggerenter',this.dlttFunction, this);

And avoid using () there, since that will execute the method in place. You only want to pass it as a reference for when the event actually fires.


Thanks why we need to use this here
in my script its deleting object upon load what am i doing wrong??

Whih ‘in place’ you mean in this case the initialize function? Why does that happen?

1 Like

Thanksss a lot this was very helpful . I am from unity so that we have to use () at the end of function no matter what ,

also using this really confuses me in playcanvas ,is there a general rule when to use words like this. in pc

thanks again all

Yes, at line 5, observe how the method is passed with () at the end. It will execute not when the event fires, but when at the beginning when the initialize method executes.

1 Like

That’s a JavaScript keyword, not PlayCanvas specific. It’s not required to use it, but most likely you will find it hard to not to.

It refers to the current instance of the object the code is executing in, the current context. Try some tutorials/guides on JS online, especially a C# to JS migration guide, I feel your pain :innocent:

1 Like

Thanks man!!!
Can you refer me some C# to JS migration guide

I don’t have one in mind, try some Google/internet searches with that subject.

1 Like