無料・軽快な 2D / 3D 用のゲームエンジン Godot Engine 4 で、EditorSettings の機能を用いて、GodotEngine のエディタの設定に保存した設定値をロードして、辞書変数に取得する実装例を紹介します。
EditorInterface の機能は、エディタ上でのみ利用可能なので、シーン実行ではなく新規プラグインから追加したツールメニューを選択することで実行させます。

※ GodotEngine 4.3 を使用しています。.NET 版ではありません。
※スクリプトは自己責任でご利用ください。
前回の記事
前回は、EditorSettings の機能を用いて、GodotEngine のエディタの設定に、独自の設定を追加・保存しました。
スクリプト例
前回作成したプラグインを構成する gd ファイルを開き、後述するスクリプトを貼り付けて保存します。

以下は、貼り付けるスクリプトです。
EditorInterface.get_editor_settings 関数で取得した EditorSettings クラスのインスタンスを用いて、has_setting メンバ関数で指定した name の設定の存在を確認してから get_setting メンバ関数を用いて、辞書変数にその設定値を格納します。
@tool
extends EditorPlugin
## EditorSettings の「ユーザ名/アドオン名/セクション名/キー」の読み書きを Dictionary 型を用いて行う関数とそのテストです。
## EditorInterface の機能はエディタ上で利用できます。
## EditorSettings の機能を含む関数を実行するために、プラグインとしてこのスクリプトを追加して
## ツールメニュー(「プロジェクト」→「ツール」)から、これが追加したメニューを選択してください。
##
## EditorSettings のファイルは C:\Users\ユーザ名\AppData\Roaming\Godot\editor_settings-4.3.tres にあります。
## 4.3 の部分はバージョンによって変わります。
## メニューアイテムの表示名です。
const tool_menu_item_name: StringName = "Study EditorSettings By Dictionary"
## プラグインの初期化処理を定義します。
## プラグイン (プロジェクト設定ダイアログ>プラグイン>有効) を有効にした直後や、プラグインが有効な状態のプロジェクトを開いた際に呼び出されます。
func _enter_tree():
# Initialization of the plugin goes here.
# メニュー「プロジェクト」→「ツール」にサブメニューを追加します。メニュー選択時に、第二引数のメンバ関数を呼び出します。
add_tool_menu_item(tool_menu_item_name, Callable(self, "_on_menu_item_study_editor_settings_by_dictionary"))
return
## プラグインの後片付けの処理を定義します。
## プラグインを無効 (プロジェクト設定ダイアログ>プラグイン>有効) にした直後や、プラグインが有効な状態でプロジェクトを閉じた際に呼び出されます。
func _exit_tree():
# Clean-up of the plugin goes here.
remove_tool_menu_item(tool_menu_item_name)
return
## プラグインで追加したツールメニューを選択した際に呼び出される関数です。
func _on_menu_item_study_editor_settings_by_dictionary():
test_load_editor_settings_section_to_dictionary()
return
func test_save_editor_settings_section_from_dictionary():
## 以下の 2 つを組み合わせて、さらにキー名を追加した name で辞書変数の値にアクセスします。
const EDITOR_SETTINGS_PATH = "sclib/fit_font_size/"
const EDITOR_SETTINGS_SECTION = "scale_correction_value/"
## セクション内に保存されるデータです。
var dic_answer = {}
dic_answer["Label"] = 0.9
dic_answer["Button"] = 1.0
# 辞書のデータ群を、指定した EditorSettings ファイルのセクションに保存します。
# ファイルが存在しない場合は作成し、存在する場合は辞書の要素ごとに上書きします。
print("save_editor_settings_section_from_dictionary call")
save_editor_settings_section_from_dictionary(
EDITOR_SETTINGS_PATH, EDITOR_SETTINGS_SECTION, dic_answer)
return
## load_editor_setting_section_to_dictionaryのテスト関数です。
func test_load_editor_settings_section_to_dictionary():
## 以下の 2 つを組み合わせて、さらにキー名を追加した name で辞書変数の値にアクセスします。
const EDITOR_SETTINGS_PATH = "sclib/fit_font_size/"
const EDITOR_SETTINGS_SECTION = "scale_correction_value/"
## セクション内に保存されるデータです。
var dic_answer = {}
dic_answer["Label"] = 0.9
dic_answer["Button"] = 1.0
# 辞書のデータ群を、指定した EditorSettings ファイルのセクションに保存します。
# ファイルが存在しない場合は作成し、存在する場合は辞書の要素ごとに上書きします。
print("save_editor_settings_section_from_dictionary call")
save_editor_settings_section_from_dictionary(
EDITOR_SETTINGS_PATH, EDITOR_SETTINGS_SECTION, dic_answer)
## 取得するキーを設定します。値は上書きされなかった場合のデフォルト値を指定します。
var dic := {}
dic["Label"] = 0.5
dic["Button"] = 0.5
## 指定した EditorSettings ファイルのセクションを読み込み、辞書として取得します。
print("load_editor_settings_section_to_dictionary call")
print("dic.keys() = ", dic.keys())
dic = load_editor_settings_section_to_dictionary(
EDITOR_SETTINGS_PATH, EDITOR_SETTINGS_SECTION, dic.keys())
# 保存した辞書の内容と、読み込んだ辞書の内容が等しいことを確認します。
# 異なる場合は警告を出力します。
if dic != dic_answer:
push_warning("study_editor_settings_to_dictionary.gd:" +
"test_load_sample_dictionary:" +
"dic != dic_answer")
# デバッグ用。読み込んだ辞書の内容を出力します。
var str_dic: String = ScUtil.to_pretty_print_string(dic)
print(str_dic)
return
## 指定した EditorSettings の path (例:「ユーザ名/アドオン名」)のセクションを辞書型で取得します。
## [param path] と [param section] については [method save_editor_settings_section_from_dictionary] を
## 参照してください。
func load_editor_settings_section_to_dictionary(
path: String, section: String, key_array: Array) -> Dictionary:
var dic := {}
# 設定を保存する name のキーの1つ前までの階層の文字列を作成します。
var base_path := path + section
# EditorSettings から key_array に指定したキーを base_path に加えた各 name の値を
# 戻り値の辞書のキーの値として取得します。
var editor_settings = EditorInterface.get_editor_settings()
if editor_settings == null:
push_error("study_eidotr_settings_by_dictionary.gd:" +
"load_editor_settings_section_to_dictionary:" +
"editor_settings == null")
return dic
# 辞書変数の要素ごとに EditorSettings から値を取得します。
for key in key_array:
# name の最後には、辞書変数のキーを追加します。
var editr_settings_name: String = base_path + key
# name の設定が存在する場合は辞書変数に格納して、しない場合は警告を出力します。
if editor_settings.has_setting(editr_settings_name) == false:
push_warning("study_eidotr_settings_by_dictionary.gd:" +
"load_editor_settings_section_to_dictionary:" +
"editor_settings.has_setting(editr_settings_name) == false, " +
"editr_settings_name = ", editr_settings_name)
else:
dic[key] = editor_settings.get_setting(editr_settings_name)
return dic
## 指定した EditorSettings の path (例:「ユーザ名/アドオン名」)のセクションに、辞書型のデータを保存します。
## 辞書の要素のキーは、EditorSettings.set_settings の name 引数、値は value 引数に対応します。
##
## [param path] と [param section] を連結した base_path に、
## 辞書の要素キーを付け加えた name に対して value を設定します。
##
## [param path] に "user_name/addon_name/" を指定して、
## [param section] に "section_name/" を設定すると
## ユーザ名、アドオン名、セクション名を / で区切った後にキーを加えた name に value を設定します。
func save_editor_settings_section_from_dictionary(
path: String, section: String, dic: Dictionary) -> void:
# 設定を保存する name のキーの1つ前までの階層の文字列を作成します。
var base_path := path + section
# EditorSettings に dic の各要素を保存します。
var editor_settings = EditorInterface.get_editor_settings()
if editor_settings == null:
push_error("study_eidotr_settings_by_dictionary.gd:" +
"save_editor_settings_section_from_dictionary:" +
"editor_settings == null")
return
# 辞書変数の要素ごとに EditorSettings に保存します。
for key in dic.keys():
# name の最後には、辞書変数のキーを追加します。
var editr_settings_name: String = base_path + key
editor_settings.set_setting(editr_settings_name, dic[key])
return
処理の流れ
_enter_tree イベント関数で、ツールメニューを追加して、そのメニューの選択時に_on_menu_item_study_editor_settings_by_dictionary 関数を呼び出すように設定しています。
その関数では、test_load_editor_settings_section_to_dictionary テスト関数を呼び出して、2つの項目を EditorSettings から取得する処理を実行します。
テスト関数は、指定されたキーを含む name に対応する設定値を辞書変数に取得する load_editor_settings_section_to_dictionary 関数を呼び出します。
使用した自作関数
ScUtil.to_pretty_print_string 関数については以下の記事を参照してください。
※この関数を使用しなくても、EditorSettings と辞書変数のやりとりは機能します。
テスト
前回追加したツールメニュー「プロジェクト」→「ツール」→「Study EditorSettings By Dictionary」を選択します。
出力ボトムパネルに表示した EditorSettings からロードした値を持つ辞書変数の内容は、事前にセーブした際の辞書変数の内容と同じであることが確認できました。

save_editor_settings_section_from_dictionary call
load_config_file_section_to_dictionary call
dic.keys() = ["Label", "Button"]
{
"Label": 0.9,
"Button": 1
}
まとめ
- EditorInterface の EditorSettings などの機能は、シーン実行時は利用できないため、ツールメニューを選択して実行しました。
- EditorSettings の has_setting メンバ関数で、その name の設定が存在しているかを確認してから取得しました。
- EditorSettings の set_setting メンバ関数を用いて独自の設定値を name と value の組み合わせで保存しました。
参照サイト Thank You!
- Godot Engine – Free and open source 2D and 3D game engine
- Dictionary — Godot Engine (4.3) documentation in English
- EditorInterface — Godot Engine (4.x)の日本語のドキュメント
- EditorSettings — Godot Engine (4.3) documentation in English
記事一覧 → Compota-Soft-Press


コメント