# API for how to add components programmatically

**URL:** <https://forum.playcanvas.com/t/api-for-how-to-add-components-programmatically/4617>\
**Category:** Help & Support\
**Created:** [August 7, 2017, 8:37pm UTC](https://forum.playcanvas.com/t/api-for-how-to-add-components-programmatically/4617 "2017-08-07T20:37:36Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Robotpencil](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/robotpencil/32/9167_2.png) [@Robotpencil](https://forum.playcanvas.com/u/Robotpencil)\
**Post date:** [August 7, 2017, 8:37pm UTC](https://forum.playcanvas.com/t/api-for-how-to-add-components-programmatically/4617/1 "2017-08-07T20:37:36Z")

</div>

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/](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.

---

<div class="post-metadata">

**Author:** ![yaustar](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/yaustar/32/2268_2.png) [@yaustar](https://forum.playcanvas.com/u/yaustar)\
**Post date:** [August 7, 2017, 8:41pm UTC](https://forum.playcanvas.com/t/api-for-how-to-add-components-programmatically/4617/2 "2017-08-07T20:41:38Z")

</div>

Your best bet is to look at the examples in the core engine code on GitHub: [engine/examples at main · playcanvas/engine · 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](https://github.com/playcanvas/engine/blob/master/examples/physics/falling_shapes/index.html#L60)

Here is the API reference for the function:

> **[Entity | Engine API Reference - v2.7.4](https://api.playcanvas.com/engine/classes/Entity.html#addcomponent)**
>
> Documentation for Engine API Reference

---

<div class="post-metadata">

**Author:** ![Robotpencil](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/robotpencil/32/9167_2.png) [@Robotpencil](https://forum.playcanvas.com/u/Robotpencil)\
**Post date:** [August 7, 2017, 8:45pm UTC](https://forum.playcanvas.com/t/api-for-how-to-add-components-programmatically/4617/3 "2017-08-07T20:45:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![yaustar](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/yaustar/32/2268_2.png) [@yaustar](https://forum.playcanvas.com/u/yaustar)\
**Post date:** [August 7, 2017, 8:46pm UTC](https://forum.playcanvas.com/t/api-for-how-to-add-components-programmatically/4617/4 "2017-08-07T20:46:27Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Robotpencil](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/robotpencil/32/9167_2.png) [@Robotpencil](https://forum.playcanvas.com/u/Robotpencil)\
**Post date:** [August 7, 2017, 8:48pm UTC](https://forum.playcanvas.com/t/api-for-how-to-add-components-programmatically/4617/5 "2017-08-07T20:48:41Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![yaustar](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/yaustar/32/2268_2.png) [@yaustar](https://forum.playcanvas.com/u/yaustar)\
**Post date:** [August 7, 2017, 9:19pm UTC](https://forum.playcanvas.com/t/api-for-how-to-add-components-programmatically/4617/6 "2017-08-07T21:19:13Z")

</div>

You want to use the Asset Registry:

> **[AssetRegistry | Engine API Reference - v2.7.4](https://api.playcanvas.com/engine/classes/AssetRegistry.html)**
>
> Documentation for Engine API Reference

`find()` specifically

> **[AssetRegistry | Engine API Reference - v2.7.4](https://api.playcanvas.com/engine/classes/AssetRegistry.html#find)**
>
> Documentation for Engine API Reference

---

<div class="post-metadata">

**Author:** ![will](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/will/32/4560_2.png) [@will](https://forum.playcanvas.com/u/will)\
**Post date:** [August 7, 2017, 11:12pm UTC](https://forum.playcanvas.com/t/api-for-how-to-add-components-programmatically/4617/7 "2017-08-07T23:12:42Z")

</div>

I’ve made a start on improving this:

> <https://github.com/playcanvas/engine/pull/981>

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

---

<div class="post-metadata">

**Author:** ![Robotpencil](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/robotpencil/32/9167_2.png) [@Robotpencil](https://forum.playcanvas.com/u/Robotpencil)\
**Post date:** [August 8, 2017, 3:29am UTC](https://forum.playcanvas.com/t/api-for-how-to-add-components-programmatically/4617/8 "2017-08-08T03:29:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![will](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/will/32/4560_2.png) [@will](https://forum.playcanvas.com/u/will)\
**Post date:** [August 8, 2017, 5:52am UTC](https://forum.playcanvas.com/t/api-for-how-to-add-components-programmatically/4617/9 "2017-08-08T05:52:44Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Robotpencil](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/robotpencil/32/9167_2.png) [@Robotpencil](https://forum.playcanvas.com/u/Robotpencil)\
**Post date:** [August 8, 2017, 7:47pm UTC](https://forum.playcanvas.com/t/api-for-how-to-add-components-programmatically/4617/10 "2017-08-08T19:47:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Robotpencil](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/robotpencil/32/9167_2.png) [@Robotpencil](https://forum.playcanvas.com/u/Robotpencil)\
**Post date:** [August 11, 2017, 4:24am UTC](https://forum.playcanvas.com/t/api-for-how-to-add-components-programmatically/4617/11 "2017-08-11T04:24:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![vaios](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/vaios/32/4566_2.png) [@vaios](https://forum.playcanvas.com/u/vaios)\
**Post date:** [August 11, 2017, 4:39am UTC](https://forum.playcanvas.com/t/api-for-how-to-add-components-programmatically/4617/12 "2017-08-11T04:39:31Z")

</div>

[https://developer.playcanvas.com/en/user-manual/scripting/creating-new/](https://developer.playcanvas.com/en/user-manual/scripting/creating-new/)
