Up to date

This page is up to date for Godot 4.2. If you still find outdated information, please open an issue.

コードスタイルガイドライン

Godotのソースコードに投稿する場合は、以下に示すスタイルガイドラインに従ってください。一部のインテグレーションプロセスを介してチェックされ、レビュー担当者から潜在的な問題の修正を求める質問が表示されますので、以下に示すようにシステムをセットアップして、すべてのコミットがガイドラインに従うようにします。

C++およびObjective-C

There are no written guidelines, but the code style agreed upon by the developers is enforced via the clang-format code beautifier, which takes care for you of all our conventions. To name a few:

  • インデントと配置は両方ともタブベースです(それぞれ1つと2つのタブ)

  • コンマの後だけでなく、数学演算子と代入演算子の周りに1つのスペース

  • ポインタ演算子と参照演算子は、型名ではなく変数識別子に貼り付けられます

  • ヘッダーのインクルードに関する詳細を参照してください

clang形式で使用されるルールは、Godotリポジトリの.clang形式ファイルで概説されています。

As long as you ensure that your style matches the surrounding code and that you not introducing trailing whitespace or space-based indentation, you should be fine. If you plan to contribute regularly, however, we strongly advise that you set up clang-format locally to check and automatically fix all your commits.

警告

Godot's code style should not be applied to third-party code, i.e. that is included in Godot's source tree but was not written specifically for our project. Such code usually comes from different upstream projects with their own style guides (or lack thereof), and don't want to introduce differences that would make syncing with upstream repositories harder.

Third-party code is usually included in the thirdparty/ folder and can thus easily be excluded from formatting scripts. For the rare cases where a third-party code snippet needs to be included directly within a Godot file, you can use /* clang-format off */ and /* clang-format on */ to tell clang-format to ignore a chunk of code.

参考

These guidelines only cover code formatting. See C++ usage guidelines for a list of language features that are permitted in pull requests.

clang形式をローカルで使用する

First of all, you will need to install clang-format. As of now, you need to use clang-format 13 to be compatible with Godot's format. Later versions might be suitable, but previous versions may not support all used options, or format some things differently, leading to style issues in pull requests.

インストール手順

clang形式をインストールする方法は次のとおりです:

  • Linux: It will usually be available out-of-the-box with the clang toolchain packaged by your distribution. If your distro version is not the required one, you can download a pre-compiled version from the LLVM website, or if you are on a Debian derivative, use the upstream repos.

  • macOS and Windows: You can download precompiled binaries from the LLVM website. You may need to add the path to the binary's folder to your system's PATH environment variable to be able to call clang-format out of the box.

変更にclang-formatを適用する方法はいくつかあります:

手動使用

You can apply clang-format manually for one or more files with the following command:

clang-format -i <path/to/file(s)>
  • -i は、変更をファイルに直接書き込む必要があることを意味します(デフォルトではclang形式では、固定バージョンは端末にのみ出力されます)。

  • パスは、次々に、または典型的なUnixシェルのようにワイルドカードを使用して、複数のファイルを指すことができます。 グロブするときは、Godotのツリーにあるコンパイル済みオブジェクト(.oおよび.aファイル)でclang-formatを実行しないように注意してください。 そのため、 core/* よりも core/*.{cpp,h} を使用する方が適切です。

プレコミット フック

使いやすさのために、Gitのプレコミットフックを提供し、すべてのコミットで自動的にclang形式を実行してチェックし、最終的なコミットでその変更を適用できるようにします。

This "hook" is a script that can be found in misc/hooks, refer to that folder's README.md for installation instructions.

あなたのclang-formatが `` PATH`` にない場合、動作するために正しいバイナリを指すように pre-commit-clang-format を編集する必要があるかもしれません。 このフックはLinuxおよびmacOSでテストされましたが、WindowsのGitシェルでも動作するはずです。

IDEプラグイン

Most IDEs or code editors have beautifier plugins that can be configured to run clang-format automatically, for example, each time you save a file.

一部のIDE用の美しいプラグインの非網羅的なリストを以下に示します:

(Pull requests are welcome to extend this list with tested plugins.)

ヘッダーのインクルード

新しいC++またはObjective-Cファイルを追加するとき、または既存のファイルに新しいヘッダーを含めるときは、次の規則に従う必要があります:

  • ファイルの最初の行は、別のファイルからコピーアンドペーストされたGodotの著作権ヘッダーとMITライセンスでなければなりません。必ずファイル名を調整してください。

  • .h ヘッダーでは、インクルードガードを FILENAME_H の形式で使用する必要があります。

  • .cpp ファイル(たとえば filename.cpp)では、最初のインクルードはクラスが宣言されているもの(たとえば #include "filename.h")で、その後に分離のための空白行が続く必要があります。

  • 次に、Godot独自のコードベースからヘッダーを取得します。このヘッダーは、アルファベット順(clang-format で強制)に含まれ、ルートフォルダからの相対パスを持ちます。これらのインクルードは引用符で行う必要があります。#include "core/object.h"。 Godotヘッダーのインクルードブロックの後には、分離のために空白行が続く必要があります。

  • Finally, third-party headers (either from thirdparty or from the system's include paths) come next and should be included with the < and > symbols, e.g. #include <png.h>. The block of third-party headers should also be followed by an empty line for separation.

  • Godot and third-party headers should be included in the file that requires them, i.e. in the .h header if used in the declarative code or in the .cpp if used only in the imperative code.

例:

/**************************************************************************/
/*  my_new_file.h                                                         */
/**************************************************************************/
/*                         This file is part of:                          */
/*                             GODOT ENGINE                               */
/*                        https://godotengine.org                         */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur.                  */
/*                                                                        */
/* Permission is hereby granted, free of charge, to any person obtaining  */
/* a copy of this software and associated documentation files (the        */
/* "Software"), to deal in the Software without restriction, including    */
/* without limitation the rights to use, copy, modify, merge, publish,    */
/* distribute, sublicense, and/or sell copies of the Software, and to     */
/* permit persons to whom the Software is furnished to do so, subject to  */
/* the following conditions:                                              */
/*                                                                        */
/* The above copyright notice and this permission notice shall be         */
/* included in all copies or substantial portions of the Software.        */
/*                                                                        */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,        */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF     */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY   */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,   */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE      */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                 */
/**************************************************************************/

#ifndef MY_NEW_FILE_H
#define MY_NEW_FILE_H

#include "core/hash_map.h"
#include "core/list.h"
#include "scene/gui/control.h"

#include <png.h>

...

#endif // MY_NEW_FILE_H
/**************************************************************************/
/*  my_new_file.cpp                                                       */
/**************************************************************************/
/*                         This file is part of:                          */
/*                             GODOT ENGINE                               */
/*                        https://godotengine.org                         */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur.                  */
/*                                                                        */
/* Permission is hereby granted, free of charge, to any person obtaining  */
/* a copy of this software and associated documentation files (the        */
/* "Software"), to deal in the Software without restriction, including    */
/* without limitation the rights to use, copy, modify, merge, publish,    */
/* distribute, sublicense, and/or sell copies of the Software, and to     */
/* permit persons to whom the Software is furnished to do so, subject to  */
/* the following conditions:                                              */
/*                                                                        */
/* The above copyright notice and this permission notice shall be         */
/* included in all copies or substantial portions of the Software.        */
/*                                                                        */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,        */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF     */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY   */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,   */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE      */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                 */
/**************************************************************************/

#include "my_new_file.h"

#include "core/math/math_funcs.h"
#include "scene/gui/line_edit.h"

#include <zlib.h>
#include <zstd.h>

Java

Godot's Java code (mostly in platform/android) is also enforced via clang-format, so see the instructions above to set it up. Keep in mind that this style guide only applies to code written and maintained by Godot, not third-party code such as the java/src/com/google subfolder.

Python

GodotのSConsビルドシステムはPythonで書かれており、ソースツリーに含まれる様々なスクリプトもPythonを使用しています。

For those, we follow the Black style guide. Blacken your Python changes using Black.

Using black locally

First of all, you will need to install Black. Black requires Python 3.7+ to run.

インストール手順

Here's how to install black:

pip3 install black --user

You then have different possibilities to apply black to your changes:

手動使用

You can apply black manually to one or more files with the following command:

black -l 120 <path/to/file(s)>
  • -l 120 means that the allowed number of characters per line is 120. This number was agreed upon by the developers.

  • The path can point to several files, either one after the other or using wildcards like in a typical Unix shell.

プレコミット フック

For ease of use, we provide a pre-commit hook for Git that will run black automatically on all your commits to check them, and let you apply its changes in the final commit.

This "hook" is a script which can be found in misc/hooks. Refer to that folder's README.md for installation instructions.

Editor integration

Many IDEs or code editors have beautifier plugins that can be configured to run black automatically, for example, each time you save a file. For details, you can check Black editor integration.

Comment style guide

This comment style guide applies to all programming languages used within Godot's codebase.

  • Begin comments with a space character to distinguish them from disabled code.

  • Use sentence case for comments. Begin comments with an uppercase character and always end them with a period.

  • Reference variable/function names and values using backticks.

  • Wrap comments to ~100 characters.

  • You can use TODO:, FIXME:, NOTE:, or HACK: as admonitions when needed.

例:

// Compute the first 10,000 decimals of Pi.
// FIXME: Don't crash when computing the 1,337th decimal due to `increment`
//        being negative.

Don't repeat what the code says in a comment. Explain the why rather than how.

Bad:

// Draw loading screen.
draw_load_screen();

You can use Javadoc-style comments above function or macro definitions. It's recommended to use Javadoc-style comments only for methods which are not exposed to scripting. This is because exposed methods should be documented in the class reference XML instead.

例:

/**
 * Returns the number of nodes in the universe.
 * This can potentially be a very large number, hence the 64-bit return type.
 */
uint64_t Universe::get_node_count() {
    // ...
}

For member variables, don't use Javadoc-style comments but use single-line comments instead:

class Universe {
    // The cached number of nodes in the universe.
    // This value may not always be up-to-date with the current number of nodes
    // in the universe.
    uint64_t node_count_cached = 0;
};