【3D技术宅公社】XR数字艺术论坛  XR技术讨论 XR互动电影 定格动画

 找回密码
 立即注册

QQ登录

只需一步,快速开始

调查问卷
论坛即将给大家带来全新的技术服务,面向三围图形学、游戏、动画的全新服务论坛升级为UTF8版本后,中文用户名和用户密码中有中文的都无法登陆,请发邮件到324007255(at)QQ.com联系手动修改密码

3D技术论坛将以计算机图形学为核心,面向教育 推出国内的三维教育引擎该项目在持续研发当中,感谢大家的关注。

查看: 1987|回复: 0

【Godot 3.2】3. Scenes and nodes 场景和节点

[复制链接]
发表于 2020-8-6 18:32:16 | 显示全部楼层 |阅读模式
本帖最后由 蚊子 于 2020-8-6 18:33 编辑

Introduction

Imagine for a second that you are not a game developer anymore. Instead, you're a chef! Change your hipster outfit for a toque and a double breasted jacket. Now, instead of making games, you create new and delicious recipes for your guests.
想象一下您不再是游戏开发人员。相反,您是厨师!更改您的时髦服装,以便穿上转矩感和双排扣外套。现在,您无需制作游戏,而是为客人创建新的美味食​​谱。

So, how does a chef create a recipe? Recipes are divided into two sections: the first is the ingredients and the second is the instructions to prepare it. This way, anyone can follow the recipe and savor your magnificent creation.
那么,厨师如何创建食谱?食谱分为两部分:第一部分是配料,第二部分是制备说明。这样,任何人都可以遵循食谱,品尝您的绝妙创造。

Making games in Godot feels pretty much the same way. Using the engine feels like being in a kitchen. In this kitchen, nodes are like a refrigerator full of fresh ingredients with which to cook.
在Godot中制作游戏的感觉几乎相同。使用引擎就像在厨房里。在这个厨房里,节点就像冰箱里装满了新鲜食材,可以用来做饭。

There are many types of nodes. Some show images, others play sound, other nodes display 3D models, etc. There are dozens of them.
节点的类型很多。一些显示图像,其他显示声音,其他节点显示3D模型,等等。其中有数十种。


Nodes节点数

But let's start with the basics. Nodes are fundamental building blocks for creating a game. As mentioned above, a node can perform a variety of specialized functions. However, any given node always has the following attributes:
但是,让我们从基础开始。节点是创建游戏的基本构建块。如上所述,节点可以执行各种专用功能。但是,任何给定节点始终具有以下属性:

  • It has a name. 它有一个名字。
  • It has editable properties. 它具有可编辑的属性。
  • It can receive a callback to process every frame.它可以接收回调以处理每个帧。
  • It can be extended (to have more functions).可以扩展(以具有更多功能)。
  • It can be added to another node as a child.可以将其作为子节点添加到另一个节点。

The last one is important. Nodes can have other nodes as children. When arranged in this way, the nodes become a tree.
最后一个很重要。节点可以将其他节点作为子节点。当以这种方式排列时,节点变成一棵树。

In Godot, the ability to arrange nodes in this way creates a powerful tool for organizing projects. Since different nodes have different functions, combining them allows for the creation of more complex functions.
在Godot中,以这种方式安排节点的能力为组织项目创造了强大的工具。由于不同的节点具有不同的功能,因此将它们组合起来可以创建更复杂的功能。

Don't worry if this doesn't click yet. We will continue to explore this over the next few sections. The most important fact to remember for now is that nodes exist and can be arranged this way.
如果尚未点击,请不要担心。在接下来的几节中,我们将继续对此进行探讨。现在要记住的最重要的事实是节点存在并且可以通过这种方式进行排列。


Scenes场景

Now that the concept of nodes has been defined, the next logical step is to explain what a Scene is.
现在已经定义了节点的概念,接下来的逻辑步骤是解释场景是什么。

A scene is composed of a group of nodes organized hierarchically (in tree fashion). Furthermore, a scene:
场景由按层次结构(以树的形式)组织的一组节点组成。此外,一个场景:

  • always has one root node.始终只有一个根节点。
  • can be saved to disk and loaded back.可以保存到磁盘并重新加载。
  • can be instanced (more on that later).可以被实例化(稍后会更多)。

Running a game means running a scene. A project can contain several scenes, but for the game to start, one of them must be selected as the main scene.
运行游戏意味着运行场景。一个项目可以包含多个场景,但是要开始游戏,必须选择其中一个作为主要场景。

Basically, the Godot editor is a scene editor. It has plenty of tools for editing 2D and 3D scenes as well as user interfaces, but the editor is based on the concept of editing a scene and the nodes that compose it.
基本上,Godot编辑器是场景编辑器。它具有用于编辑2D和3D场景以及用户界面的大量工具,但是该编辑器基于编辑场景及其组成节点的概念。


Editor 编辑

Open the project you made in Introduction to Godot's editor, or create a new one. This will open the Godot editor:
打开您在Godot简介编辑器中创建的项目,或创建一个新项目。这将打开Godot编辑器:

As mentioned before, making games in Godot feels like being in a kitchen, so let's open the refrigerator and add some fresh nodes to the project. We'll begin with a "Hello World" message that we'll put on the screen.
如前所述,在Godot中进行游戏就像在厨房里一样,所以让我们打开冰箱,为项目添加一些新节点。我们将从在屏幕上显示的“ Hello World”消息开始。

To do this we need to add a Label node. Press the "Add Child Node" button at the top left of the scene dock (the icon represents a plus symbol). This button is the main way to add new nodes to a scene, and will always add the chosen node as a child of the currently selected node (or, in an empty scene, as the "root" node).
为此,我们需要添加一个Label节点。按下场景底座左上方的“添加子节点”按钮(该图标表示加号)。此按钮是将新节点添加到场景的主要方法,并且始终将所选节点添加为当前所选节点的子级(或在空场景中添加为“根”节点)。

Note

In an empty scene (without root node), the scene dock shows several options to quickly add a root node to the scene. "2D Scene" adds a Node2D node, "3D Scene" adds a Spatial node, "User Interface" adds a Control node, and "Custom Node" which lets you select any node (so it is equivalent to pressing the "Add Child Node" button). You can also press the star-shaped icon to toggle the display of your favorited nodes.
在空场景(无根节点)中,场景底座显示了几个选项,可以快速将根节点添加到场景中。“ 2D场景”添加一个Node2D节点,“ 3D场景”添加一个空间节点,“用户界面”添加一个控制节点,“自定义节点”允许您选择任何节点(因此等效于按“添加子节点” “按钮)。您也可以按星形图标切换收藏夹节点的显示。

Note that these presets are here for convenience and are not mandatory for the different types of scenes. Not every 3D scene needs a Spatial node as its root node, likewise not every GUI or 2D scene needs a Control node or Node2D as their root node.
请注意,这些预设是为了方便起见,对于不同类型的场景不是必需的。并非每个3D场景都需要一个Spatial节点作为其根节点,同样,并非每个GUI或2D场景都需要一个Control节点或Node2D作为其根节点。


Now, to add a label node to this scene you can click on the Custom Node button or the Add Node button at the top. In scenes that aren't empty you use the add node button to create every child node.
现在,要将标签节点添加到该场景,您可以单击顶部的“自定义节点”按钮或“添加节点”按钮。在不为空的场景中,您可以使用添加节点按钮创建每个子节点。

This will open the Create Node dialog, showing the long list of nodes that can be created:
这将打开“创建节点”对话框,显示可以创建的节点的长列表:

From there, select the "Label" node first. Searching for it is probably the fastest way:
从那里,首先选择“标签”节点。搜索它可能是最快的方法:

And finally, create the Label! A lot happens when Create is pressed:
最后,创建标签!按下创建时会发生很多事情:

First of all, the scene changes to the 2D editor (because Label is a 2D Node type), and the Label appears, selected, at the top left corner of the viewport.
首先,场景将更改为2D编辑器(因为Label是2D节点类型),并且Label将显示在视口的左上角,并处于选中状态。

The node appears in the scene tree editor in the Scene dock, and the label properties appear in the Inspector dock.
节点出现在“场景”停靠区的场景树编辑器中,标签属性出现在“检查器”停靠区中。

The next step will be to change the "Text" Property of the label. Let's change it to "Hello World":
下一步将是更改标签的“文本”属性。让我们将其更改为“ Hello World”:

Ok, everything's ready to run the scene! Press the PLAY SCENE Button on the top bar (or hit F6):
好的,一切准备就绪!按下顶部栏上的“播放场景”按钮(或点击F6):

Aaaand... Oops.
Aaaand ...哎呀。

Scenes need to be saved to be run, so save the scene to something like Hello.tscn in Scene -> Save:
场景需要保存才能运行,因此将场景保存到场景->保存中的Hello.tscn之类的东西中:

And here's when something funny happens. The file dialog is a special file dialog, and only allows you to save inside the project. The project root is res:// which means "resource path". This means that files can only be saved inside the project. For the future, when doing file operations in Godot, remember that res:// is the resource path, and no matter the platform or install location, it is the way to locate where resource files are from inside the game.
这是有趣的事情发生的时候。文件对话框是一个特殊的文件对话框,仅允许您保存在项目内部。项目根是res://“资源路径”。这意味着文件只能保存在项目内部。将来,在Godot中进行文件操作时,请记住这res://是资源路径,无论是平台还是安装位置,这都是从游戏内部定位资源文件位置的方法。

After saving the scene and pressing run scene again, the "Hello World" demo should finally execute:
保存场景并再次按下运行场景后,“ Hello World”演示将最终执行:

Success!

Note

If this doesn't immediately work and you have a hiDPI display on at least one of your monitors, go to Project → Project Settings → Display → Window then enable Allow Hidpi under Dpi.
如果此操作无法立即生效,并且您的至少一台显示器上都显示了hiDPI,请转到“ 项目”→“项目设置”→“显示”→“窗口”,然后在Dpi下启用“ 允许Hidpi ” 。



Configuring the project配置项目

Ok, it's time to configure the project. Right now, the only way to run something is to execute the current scene. Projects, however, may have several scenes, so one of them must be set as the main scene. This is the scene that will be loaded any time the project is run.
好的,该配置项目了。目前,执行某项操作的唯一方法是执行当前场景。但是,项目可能有多个场景,因此必须将其中一个设置为主场景。这是在项目运行时都会加载的场景。

These settings are all stored in a project.godot file, which is a plaintext file in win.ini format (for easy editing). There are dozens of settings that you can change in this file to alter how a project executes. To simplify this process, Godot provides a project settings dialog, which acts as a sort of frontend to editing a project.godot file.
这些设置都存储在project.godot文件中,该文件是win.ini格式的纯文本文件(便于编辑)。您可以在此文件中更改许多设置,以更改项目的执行方式。为简化此过程,Godot提供了一个项目设置对话框,该对话框充当编辑project.godot文件的前端。

To access that dialog, select Project -> Project Settings. Try it now.
要访问该对话框,请选择项目->项目设置。现在就试试。

Once the window opens, let's select a main scene. Locate the Application/Run/Main Scene property and click on it to select 'Hello.tscn'.
窗口打开后,让我们选择一个主要场景。找到“ 应用程序/运行/主场景”属性,然后单击它以选择“ Hello.tscn”。

Now, with this change, when you press the regular Play button (or F5), this scene will run, no matter which scene is actively being edited.
现在,通过此更改,当您按常规的“播放”按钮(或F5)时,无论正在编辑哪个场景,此场景都将运行。

The project settings dialog provides a lot of options that can be saved to a project.godot file and shows their default values. If you change a value, a tick is marked to the left of its name. This means that the property will be saved to the project.godot file and remembered.
项目设置对话框提供了许多选项,可以将其保存到project.godot文件中并显示其默认值。如果更改值,则在其名称的左侧标记一个勾号。这意味着该属性将保存到project.godot文件中并被记住。

As a side note, it is also possible to add custom configuration options and read them in at run-time using the ProjectSettings singleton.
另外,还可以添加自定义配置选项,并在运行时使用ProjectSettings单例读取它们


To be continued...

This tutorial talked about "scenes and nodes", but so far there has been only one scene and one node! Don't worry, the next tutorial will expand on that...



您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|3D数字艺术论坛 ( 沪ICP备14023054号 )

GMT+8, 2024-4-24 15:31

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表