godot游戏引擎自学入门笔记--GDScript语言,官方文档翻译(六)

简介

GDScript is a high level, dynamically typed programming language used to create content. It uses a syntax similar to Python (blocks are indent-based and many keywords are similar). Its goal is to be optimized for and tightly integrated with Godot Engine, allowing great flexibility for content creation and integration.

GDScript 是一种用于创建内容的高级动态类型化编程语言。它使用类似于 Python 的语法 (块基于索引, 许多关键字相似)。它的目标是针对 godot 引擎进行优化与集成, 从而为内容创建提供极大的灵活性。

In the early days, the engine used the Lua scripting language. Lua is fast, but creating bindings to an object oriented system (by using fallbacks) was complex and slow and took an enormous amount of code. After some experiments with Python, it also proved difficult to embed.

在早期, 引擎使用 Lua 脚本语言,它速度很快, 但创建面向对象的系统的绑定 (通过使用回退) 既复杂又缓慢, 并且需要大量的代码。在用 Python 进行了一些实验后, 它也被证明难以嵌入。

The last third party scripting language that was used for shipped games was Squirrel, but it was dropped as well. At that point, it became evident that a custom scripting language could more optimally make use of Godot’s particular architecture

最后一种第三方脚本语言是 Squirrel,但它也被移除了。在这一点上,很明显为godtot定制脚本语言,才可以更好地发挥Godot的特殊架构。

Godot embeds scripts in nodes. Most languages are not designed with this in mind.
Godot uses several built-in data types for 2D and 3D math. Script languages do not provide this, and binding them is inefficient.
Godot uses threads heavily for lifting and initializing data from the net or disk. Script interpreters for common languages are not friendly to this.
Godot already has a memory management model for resources, most script languages provide their own, which results in duplicate effort and bugs.
Binding code is always messy and results in several failure points, unexpected bugs and generally low maintainability.

①Godot在节点中嵌入脚本。大多数语言的设计都没有考虑到这一点
②Godot为2D和3D数学使用了几种内置的数据类型。脚本语言不提供这种功能,而且绑定它们是低效的。
③Godot大量使用线程来提升和初始化来自网络或磁盘的数据。公共语言的脚本解释器对此并不友好。
④Godot已经为资源提供了一个内存管理模型,大多数脚本语言都提供了自己的内存管理模型,这会导致重复的工作和错误。
⑤绑定代码总是很混乱,会导致许多故障点、意外错误和通常较低的可维护性

The result of these considerations is GDScript. The language and interpreter for GDScript ended up being smaller than the binding code itself for Lua and Squirrel, while having equal functionality. With time, having a built-in language has proven to be a huge advantage.

这些考虑的结果是 GDScript语言 。GDScript的语言和解释器最终比Lua和Squirrel的绑定代码本身要小,同时具有相同的功能。随着时间的推移,拥有一种内置的语言已经被证明是一个巨大的优势。

示例

Some people can learn better by taking a look at the syntax, so here’s a simple example of how GDScript looks.

有些人可以通过查看语法来更好地学习,下面是GDScript的一个简单示例

# A file is a class!

# Inheritance

extends BaseClass

# (optional) class definition with a custom icon

class_name MyClass, "res://path/to/optional/icon.svg"

# Member Variables

var a = 5
var s = "Hello"
var arr = [1, 2, 3]
var dict = {"key": "value", 2:3}
var typed_var: int
var inferred_type := "String"

# Constants

const ANSWER = 42
const THE_NAME = "Charly"

# Enums

enum {UNIT_NEUTRAL, UNIT_ENEMY, UNIT_ALLY}
enum Named {THING_1, THING_2, ANOTHER_THING = -1}

# Built-in Vector Types

var v2 = Vector2(1, 2)
var v3 = Vector3(1, 2, 3)

# Function

func some_function(param1, param2):
    var local_var = 5

    if param1 < local_var:
        print(param1)
    elif param2 > 5:
        print(param2)
    else:
        print("Fail!")

    for i in range(20):
        print(i)

    while param2 != 0:
        param2 -= 1

    var local_var2 = param1 + 3
    return local_var2

# Functions override functions with the same name on the base/parent class.
# If you still want to call them, use '.' (like 'super' in other languages).

func something(p1, p2):
    .something(p1, p2)

# Inner Class

class Something:
    var a = 10

# Constructor

func _init():
    print("Constructed!")
    var lv = Something.new()
    print(lv.a)

If you have previous experience with statically typed languages such as C, C++, or C# but never used a dynamically typed one before, it is advised you read this tutorial: GDScript: An introduction to dynamic languages.

如果您以前有过静态类型语言(如C、C++或C语言)的经验,但以前从未使用过动态类型的语言,建议您阅读本教程: GDScript:动态语言导论.

动态语言导论

This tutorial aims to be a quick reference for how to use GDScript more efficiently. It focuses on common cases specific to the language, but also covers a lot of information on dynamically typed languages.
It’s meant to be especially useful for programmers with little or no previous experience with dynamically typed languages.

本教程旨在快速介绍如何更有效地使用GDScript。它只关注该语言的常见情况,但是也涉及许多关于动态类型语言的信息。
对于以前很少或没有使用动态类型语言经验的程序员来说,它特别有用

GDScript is a Dynamically Typed language. As such, its main advantages are that:
The language is simple and easy to learn.
Most code can be written and changed quickly and without hassle.
Less code written means less errors & mistakes to fix.
Easier to read the code (less clutter).
No compilation is required to test.
Runtime is tiny.
Duck-typing and polymorphism by nature.

GDScript是一种动态类型语言。因此,它的主要优点是:
①这种语言简单易学。
②大多数代码都可以快速地编写和更改,而且没有任何麻烦。
③更少的代码编写意味着更少的错误和失误。
④更容易阅读代码(减少混乱)。
⑤测试不需要编译。
⑥Runtime很小。
⑦本质上是鸭子类型(一种编程风格,见于python)和多态性。

While the main disadvantages are:
Less performance than statically typed languages.
More difficult to refactor (symbols can’t be traced)
Some errors that would typically be detected at compile time in statically typed languages only appear while running the code (because expression parsing is more strict).
Less flexibility for code-completion (some variable types are only known at run-time).

主要缺点是:
①比静态类型语言的性能要低。
②重构更困难(符号无法跟踪)
③在静态类型语言中,一些通常在编译时检测到的错误只会在运行代码时出现(因为表达式解析更严格)。
④代码完成的灵活性较低(一些变量类型只在运行时知道)。

This, translated to reality, means that Godot+GDScript are a combination designed to create games quickly and efficiently. For games that are very computationally intensive and can’t benefit from the engine built-in tools (such as the Vector types, Physics Engine, Math library, etc), the possibility of using C++ is present too. This allows you to still create most of the game in GDScript and add small bits of C++ in the areas that need a performance boost

这意味着Godot+GDScript是一个旨在快速高效创造游戏的组合。对于那些计算密集且不能从引擎的内置工具(如向量类型、物理引擎、数学库等)中获益的游戏,也可以使用C++。它允许你在GDScript中创建整个游戏,并在需要性能提升的地方添加少量C++

你可能感兴趣的:(游戏引擎)