Attention: Here be dragons
This is the latest
(unstable) version of this documentation, which may document features
not available in or compatible with released stable versions of Godot.
Checking the stable version of the documentation...
Tworzenie instancji
Informacja
This tutorial refers to instantiating scenes in the editor. To learn how to instantiate scenes from code, see Nodes and scene instances.
Godot's approach to instantiation described below should not be confused with hardware instancing that can be used to render large amounts of similar objects quickly. See Optimization using MultiMeshes instead.
W poprzedniej części nauczyliśmy się, że scena to kolekcja węzłów ułożonych w strukturę drzewa, z jednym, głównym węzłem stanowiącym korzeń (root). Możesz podzielić swój projekt na dowolną ilość scen. Ta funkcja pomoże Ci wydzielić mniejsze części i zorganizować wszystkie składowe Twojej gry.
Możesz utworzyć dowolną liczbę scen i zapisać je jako pliki z rozszerzeniem .tscn, co jest skrótem od angielskiego wyrażenia "text scene". Przykładem jest plik label.tscn z poprzedniej lekcji. Nazywamy te pliki "spakowanymi scenami" ("Packed Scenes"), ponieważ zapakowano do nich informacje o zawartości sceny.
Oto przykład z piłką. Składa się z węzła RigidBody2D będącego korzeniem o nazwie Ball - element ten pozwala piłce spadać i odbijać się od ścian, węzła Sprite2D oraz CollisionShape2D.
Once you have saved a scene, it works as a blueprint: you can reproduce it in other scenes as many times as you'd like. Replicating an object from a template like this is called instantiation.
As we mentioned in the previous part, instantiated scenes behave like a node: the editor hides their content by default. When you instantiate the Ball, you only see the Ball node. Notice also how each duplicate has a unique name.
Każda instancja sceny Ball powstaje mając tą samą strukturę i właściwości jak oryginalny plik ball.tscn. Można jednak modyfikować każdą instancję niezależnie, na przykład zmieniać sposób odbijania się piłki, jej ciężar lub dowolną właściwość sceny źródłowej.
W praktyce
Let's use instantiation in practice to see how it works in Godot. We invite you to download the ball's sample project we prepared for you: instancing_starter.zip.
Extract the archive on your computer. To import it, you need the Project Manager. The Project Manager is accessed by opening Godot, or if you already have Godot opened, click on (Ctrl + Shift + Q, Ctrl + Option + Cmd + Q on macOS)
In the Project Manager, click the button.
In the file dialog that appears, navigate to the folder you extracted.
Double-click the project.godot file to select it.
Finally, click the button to import the project.
A window notifying you that the project was last opened in an older Godot version may appear. That's not an issue. Click to open the project.
The project contains two packed scenes: main.tscn, containing walls against
which the ball collides, and ball.tscn. The main scene should open
automatically. If you're seeing an empty 3D scene instead of the main scene, click the 2D button at the top of the screen.
Dodajmy kulkę jako element podrzędny węzła "Main". W doku "Scena" wybierz węzeł "Main". Następnie kliknij ikonę "łącze" (ten łańcuch) w doku "Scena". Przycisk ten umożliwia dodanie instancji sceny jako elementu podrzędnego (dziecko węzła) dla aktualnie wybranego węzła.
Double-click the ball scene to instantiate it.
Kulka pojawi się w górnej części widoku perspektywy (viewport).
Kliknij na kulkę i przeciągnij ją w kierunku centrum w widoku Perspektywy.
Play the game by pressing F5 (Cmd + B on macOS). You should see it fall.
Now, we want to create more instances of the Ball node. With the ball still selected, press Ctrl + D (Cmd + D on macOS) to duplicate it. Click and drag the new ball to a different location.
Kontynuuj to działanie tworząc kilka kulek na scenie.
Zagraj w grę ponownie. Powinieneś teraz zobaczyć, jak każda piłka spada niezależnie od drugiej. Tak właśnie robią instancje. Każdy z nich jest niezależnym odlewem z formy jaką jest scena.
Edycja scen i instancji
To nie wszystko odnośnie instancjonowania. Ta funkcja pozwala Ci również:
Change the properties of one ball without affecting the others using the Inspector.
Zmienić domyślne właściwości każdej z kulek, otwierając scenę
ball.tscni dokonując zmian w węźle Ball. Po zapisaniu wartości, wszystkie kulki (instancje Ball) w projekcie zostaną zaktualizowane.
Informacja
Zmiana właściwości instancji nadpisuje właściwości na scenie, na której jest umiejscowiona.
Let's try this. Double-click ball.tscn in the FileSystem to open it.
In the Scene dock on the left, select the Ball node. Then, in the Inspector on the right, click on the PhysicsMaterial property to expand it.
Set its Bounce property to 0.5 by clicking on the number field, typing 0.5,
and pressing Enter.
Play the game by pressing F5 (Cmd + B on macOS) and notice how all balls now bounce a lot more. As the Ball scene is a template for all instances, modifying it and saving causes all instances to update accordingly.
Let's now adjust an individual instance. Head back to the main scene by clicking on the corresponding tab above the viewport.
Select one of the instantiated Ball nodes and, in the Inspector, set its
Gravity Scale value to 10.
A gray "revert" button appears next to the adjusted property.
Zauważ, że obok dostosowanej właściwości pojawia się szary przycisk przywrócenia. Gdy przycisk ten jest obecny, oznacza to, że zmodyfikowano właściwość instancji w celu zastąpienia jej wartości w zapisanej scenie. Nawet jeśli ta właściwość zostanie zmodyfikowana w oryginalnej scenie, wartość niestandardowa pozostanie. Naciśnięcie przycisku przywracania spowoduje przywrócenie właściwości do wartości zapisanej sceny.
Uruchom grę ponownie i zwróć uwagę, że ta kulka spada teraz znacznie szybciej niż inne.
Informacja
You may notice you are unable to change the values of the PhysicsMaterial of the ball. This is because PhysicsMaterial is a resource, and needs to be made unique before you can edit it in a scene that is linking to its original scene. To make a resource unique for one instance, right-click on the Physics Material property in the Inspector and click in the context menu.
Resources are another essential building block of Godot games we will cover in a later lesson.
Scene instances as a design language
Instances and scenes in Godot offer an excellent design language, setting the engine apart from others out there. We designed Godot around this concept from the ground up.
We recommend dismissing architectural code patterns when making games with Godot, such as Model-View-Controller (MVC) or Entity-Relationship diagrams. Instead, you can start by imagining the elements players will see in your game and structure your code around them.
For example, you could break down a shooter game like so:
You can come up with a diagram like this for almost any type of game. Each rectangle represents an entity that's visible in the game from the player's perspective. The arrows point towards the instantiator of each scene.
Once you have a diagram, we recommend creating a scene for each element listed in it to develop your game. You'll use instantiation, either by code or directly in the editor, to build your tree of scenes.
Programmers tend to spend a lot of time designing abstract architectures and trying to fit components into it. Designing based on scenes makes development faster and more straightforward, allowing you to focus on the game logic itself. Because most game components map directly to a scene, using a design based on scene instantiation means you need little other architectural code.
Here's the example of a scene diagram for an open-world game with tons of assets and nested elements:
Imagine we started by creating the room. We could make a couple of different room scenes, with unique arrangements of furniture in them. Later, we could make a house scene that uses multiple room instances for the interior. We would create a citadel out of many instantiated houses and a large terrain on which we would place the citadel. Each of these would be a scene instantiating one or more sub-scenes.
Later, we could create scenes representing guards and add them to the citadel. They would be indirectly added to the overall game world.
With Godot, it's easy to iterate on your game like this, as all you need to do is create and instantiate more scenes. We designed the editor to be accessible to programmers, designers, and artists alike. A typical team development process can involve 2D or 3D artists, level designers, game designers, and animators, all working with the Godot editor.
Podsumowanie
Instantiation, the process of producing an object from a blueprint, has many handy uses. With scenes, it gives you:
The ability to divide your game into reusable components.
A tool to structure and encapsulate complex systems.
A language to think about your game project's structure in a natural way.