object-list.rst 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. .. include:: ../_header.rst
  2. Object List
  3. -----------
  4. Often, you need to group objects with a common property or type, or with a common goal in the game. For example, you need to group the enemy objects, the pickable items, the platform objects, so you can test if the player collides with them.
  5. For this purpose, the |SceneEditor|_ allows grouping the objects of the scene into a JavaScript array.
  6. You can create an `Object List`_ by dropping a `List built-in block <blocks-view-integration.html>`_ on the scene:
  7. .. image:: ../images/scene-editor-object-list-add-from-blocks-20230516.webp
  8. :alt: Add object list from the Blocks view.
  9. Or you can create a new list when adding an object to the list:
  10. .. image:: ../images/scene-editor-object-list-new-list-from-section-20230516.webp
  11. :alt: Create new list in the Lists section.
  12. .. image:: ../images/scene-editor-object-list-add-new-2-20230516.webp
  13. :alt: Enter the name of the new list.
  14. To add an object to an `Object List`_, select the object and look for the **Lists** section. Click on the drop-down of the **Lists** property, it shows the lists availables in the scene with the option of add the selected object to a list or remove it from a list:
  15. .. image:: ../images/scene-editor-object-list-add-object-20230516.webp
  16. :alt: Add object to the list.
  17. The |OutlineView|_ shows the Object Lists of your scene, in the **Lists** category. It also shows the objects of every list:
  18. .. image:: ../images/scene-editor-object-list-outline-20230516.webp
  19. :alt: Object lists in the outline.
  20. You can remove items from a list by selecting them in the outline and pressing the **Delete** command (``Del``).
  21. Also, you can sort the items in the list with the Move commands, just like you order the game objects in the scene:
  22. .. image:: ../images/scene-editor-object-list-order-commands-20230627.webp
  23. :alt: Sorting commands.
  24. You can locate the a list item in the scene. Select the list item, and in the **List Item** section of the |InspectorView|_, click on the **Select Game Object** button:
  25. .. image:: ../images/scene-editor-object-list-item-section-20230516.webp
  26. :alt: List Item section.
  27. Object List properties
  28. ~~~~~~~~~~~~~~~~~~~~~~
  29. The `Object List`_ type is not part of the |PhaserAPI|_, it is something introduced by the |SceneEditor|_. In the **Variable** section of the |InspectorView|_, it shows the `Variable properties <variable-properties.html>`_:
  30. .. image:: ../images/scene-editor-object-list-section-20230516.webp
  31. :alt: Object List properties.
  32. In this section you can modify the name of the list, see the inferred type of the list items, and change the scope (``CLASS`` by default) of the object list variable.
  33. Object List code generation
  34. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  35. The code generated by the |SceneCompiler|_, to create a list, is like this:
  36. .. code::
  37. this.pickableItems = [melon, melon_1, melon_2, bomb, bomb_1, bomb_2];
  38. If the output language is TypeScript, the |SceneCompiler|_ generates a field declaration for the list:
  39. .. code::
  40. private pickableItems: Array<Melon|Bomb>;
  41. Note that the |SceneCompiler|_ infers the type of the array elements: a union of ``Melon`` and ``Bomb``, that are prefabs_. It also infers the type of the elements if are Phaser_ built-in types:
  42. .. code::
  43. private parallax: Phaser.GameObjects.Image[];
  44. This detailed type declaration of the arrays allows that code editors like |VSCode|_ can provide a smarter coding experience.
  45. Object List vs Phaser Group
  46. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  47. Traditionally, Phaser_ uses the `Group game object <https://photonstorm.github.io/phaser3-docs/Phaser.GameObjects.Group.html>`_ to join objects around a common purpose. But grouping is not the only feature of a `Group game object`_:
  48. * It implements an object pool.
  49. * Is a data structure with methods for sorting and iterate the items.
  50. * Can be used as a proxy to modify the state of the items.
  51. * To call a method of the items.
  52. * To apply game-related global operations to the items.
  53. So, why the |SceneEditor|_ is not using the `Group game object`_?
  54. #. Many of the features of a `Group game object`_ are especially helpful to implement the logic of the game, but what a level maker needs is just to organize the objects, we think.
  55. #. The `Group game object`_ type is not parameterized. A code editor type-inference/auto-completion engine works much better with a simple JavaScript array. When you write ``const enemies = [enemy1, enemy2, ...]``, a code editor like |VSCode|_ can infer the type of the array items.
  56. #. Even for a human, a simple JavaScript array could be more comprehensible.
  57. #. If you need a `Group game object`_, you can create it with the array of objects generated by the |SceneEditor|_:
  58. .. code::
  59. const pickableItemsGroup = this.add.group(this.pickableItems);