user-components-instancing.rst 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. .. include:: ../_header.rst
  2. Adding User Components to a Game Object
  3. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. You can add a user component to an object (entity) in two ways: manually, and with the |SceneEditor|_.
  5. Adding a component to an object manually:
  6. .. code::
  7. const platform = ...;
  8. const move = new HorizontalMove(platform);
  9. move.minX = 100;
  10. move.maxX = 400;
  11. The constructor of the user component **HorizontalMove** adds the component to the object. You can keep a reference to the component or get the component from the game object, with the **getComponent** method:
  12. .. code::
  13. const move = HorizontalMove.getComponent(platform);
  14. However, the interesting is adding components to objects in the |SceneEditor|_.
  15. This is possible with the **Add User Component** command:
  16. 1. Select an object.
  17. 2. Execute the **Add User Component** command (``M``). It is also available in the context menu **Scripting** |-| **Add User Component**.
  18. .. image:: ../images/scene-editor-user-components-obj-add-component-1-20230627.webp
  19. :alt: Add User Component command.
  20. 3. The command opens a dialog with all the components defined in the project (grouped by the ``*.components`` files). Select the one you want to add:
  21. .. image:: ../images/scene-editor-user-components-obj-add-component-2-20230627.webp
  22. :alt: Select user component.
  23. 4. The |InspectorView|_ provides a new **HorizontalMove** section with the properties of the component:
  24. .. image:: ../images/scene-editor-user-components-show-components-20230627.webp
  25. :alt: Showing the user components of an object.
  26. Generating the code for a component in an object
  27. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  28. The `user components compiler <user-components-compiler.html>`_ will generate the code to create the component, just like if you write it by hand:
  29. .. code::
  30. // bridge2 (components)
  31. const bridge2HorizontalMove = new HorizontalMove(bridge2);
  32. bridge2HorizontalMove.horizVelocity = 100;
  33. bridge2HorizontalMove.minX = 10;
  34. bridge2HorizontalMove.maxX = 400;
  35. Actions for a component
  36. ^^^^^^^^^^^^^^^^^^^^^^^
  37. These are the actions associated to the user component of an object:
  38. .. image:: ../images/scene-editor-user-components-section-menu-20230627.webp
  39. :alt: User component section's menu.
  40. * **Select Objects With HorizontalMove**: selects in the scene all objects containing the **HorizontalMove** component.
  41. * **Open Definition Of HorizontalMove**: opens the definition of the component in the `User Components Editor <./user-components-editor.html>`_.
  42. * **Move Up**, **Move Down**: changes the order of the component.
  43. * **Delete**: deletes the component from the object.
  44. Browsing the User Components in a scene
  45. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  46. You can take an overview of all the user components in all the objects of the scene with the **Browse User Components** (``Shift+M``) command:
  47. .. image:: ../images/scene-editor-user-components-browse-1-20230627.webp
  48. :alt: Browse User Component command.
  49. .. image:: ../images/scene-editor-user-components-browse-2-20230627.webp
  50. :alt: User components in the scene.