API for how to add components programmatically

Is there really good documentation on how to add components programmatically? Like where can I find how to add something like a rigid body and collision and control their parameters using Code alone.

This was good https://developer.playcanvas.com/en/tutorials/programmatically-creating/

But it doesn’t include the other API, It would be nice to have something that is pretty well documented.

Your best bet is to look at the examples in the core engine code on GitHub: https://github.com/playcanvas/engine/tree/master/examples

For example, adding a rigid body to an entity: https://github.com/playcanvas/engine/blob/master/examples/physics/falling_shapes/index.html#L60

Here is the API reference for the function: Entity | PlayCanvas API Reference

1 Like

You’re the best :slight_smile: I’ll take a look right away!

Not sure what you mean by ‘other’ API though? Did you mean the data part that you need to pass? In which case, it is the properties of the component that you want to add.

Yeah I think what you sent is all I need for now. Do you have examples to find materials that were made in the engine.

For instance, I want to add a ‘myRed_mat’ material I made to an entity that was created via through code.

You want to use the Asset Registry: https://developer.playcanvas.com/en/api/pc.AssetRegistry.html

find() specifically https://developer.playcanvas.com/en/api/pc.AssetRegistry.html#find

I’ve made a start on improving this:

I now need to document the data object for each component. I’ll put that in the overview for each component page I think.

Great! not sure what i’m reading though lol sorry i’m pretty noob.

It’s OK - this topic is never explicitly covered in the API reference manual and I’m glad you raised the issue. I’ll try to improve this in the coming days.

Awesome! Link it as soon as you do! Also what yaustar sent is good for me now :slight_smile:

Not sure how I’d go about adding a script through code, any help here?

https://developer.playcanvas.com/en/user-manual/scripting/creating-new/