prefab-nested.rst 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. .. include:: ../_header.rst
  2. Nested prefabs
  3. ~~~~~~~~~~~~~~
  4. The nested prefab is a powerful concept. Like the overall prefab concept, it is inspired by |Unity|_. Its name describes its purpose: allow nesting prefabs.
  5. There are many cases where your prefab is a compound of objects, and you would like to modify them in prefab instances. For example, look at this **AlienShip** prefab:
  6. .. image:: ../images/scene-editor-nested-prefab-alienship-08182021.webp
  7. :alt: AlienShip, a prefab with nested prefabs.
  8. It is a `container <container-object.html>`_, with an **alien** and a **ship** children.
  9. When you create an instance of the **AlienPrefab** (that is an instance of a `container`_), you can change its position, its scale. It's custom properties. But you cannot change the properties of the **ship** or the **alien** children.
  10. But if you declare the **alien** and **ship** objects as nested prefab, you can access them in the prefab instance.
  11. For setting an object as nested prefab, you select the **NESTED_PREFAB** option in the **Scope** parameter of the **Variable** section:
  12. .. image:: ../images/scene-editor-nested-prefab-scope-20230516.webp
  13. :alt: Setting the nested prefab scope.
  14. Variables with a **NESTED_PREFAB** scopes are generated in code as public fields. It is like the **PUBLIC** scope but allowing to modify the object in prefab instances.
  15. Nested prefab instances
  16. ```````````````````````
  17. When you create an instance of a prefab that contains nested prefabs, the `Outline <../workbench/outline-view.html>`_ view shows the nested prefabs. Also, the |SceneEditor|_ allows selecting the nested prefab instance:
  18. .. image:: ../images/scene-editor-nested-prefab-instance-08192021.webp
  19. :alt: Prefab instance with nested prefabs.
  20. Note the nested prefab instance **ship** of the **AlienShip**'s prefab instance, has a different texture. Nested prefab instances work just like prefab instances: for changing a property, it first requires unlocking the property:
  21. .. image:: ../images/scene-editor-nested-prefab-instance-change-texture-08192021.webp
  22. :alt: Changing the texture property of a nested prefab instance.
  23. The code generated for the **alien1** object is like this:
  24. .. code::
  25. // alien1
  26. const alien1 = new AlienShip(this, 142, 398);
  27. this.add.existing(alien1);
  28. alien1.ship.setTexture("shipYellow");
  29. Note the nested prefab instance **ship** is a field of the prefab instance **alien1**.
  30. Things you cannot do with nested prefab instances
  31. `````````````````````````````````````````````````
  32. In practice, nested prefabs are like prefabs, with certain differences:
  33. (Remember a prefab is a type, a template, and a prefab instance is the realization of the prefab in the scene)
  34. * In code, a prefab is a user JavaScript class. A nested prefab is part of a prefab. It is an object referenced by a prefab class field.
  35. * You cannot define user properties for a nested prefab (check the comment below).
  36. * You cannot delete a nested prefab instance.
  37. * You cannot change the type of a nested prefab instance.
  38. A nested prefab is an object that is part of a prefab. Its type can be a built-in Phaser_ type or a prefab. You cannot define user properties for the nested prefab if it has a Phaser_ built-in type. But, if it has a prefab as a type, you can define user properties for that prefab. Saying it in other words. If you need to add custom properties to a nested prefab (**A**), then you can create a separated prefab scene (**B**) with user properties, and use that prefab **B** as type creating the nested prefab **A**.