はじめに
PeanutsCodeさんのプラットフォーマー制作でGodotを学習中。
前回投稿記事から大して進んでおらず、今回もエラー対処ネタです。今回は警告ですが。
環境
- Windows版 Godot Engine Ver.3.5(stable)
- GDScript
現象
プラットフォーマー制作Part8において、以下の2つの警告が出る。
The function 'connect()' returns a value, but this value is never used.
RETURN_VALUE_DISCARDED
The signal 'item_hit' is declared but never emitted.
UNUSED_SIGNAL
以下のことに対する警告です。
- connectの戻り値を無視している。game.gd に
player.connect("item_hit", self, "_on_Player_item_hit")
という形で、戻り値を受けない記述がある。 - スクリプトで定義したシグナルを使っていない。Player.gd で定義した
signal item_hit(point)
を同スクリプト内で使っていない。
connect(...) のボツ案1
https://github.com/godotengine/godot/issues/27576#issuecomment-532736107
assert(connect("changed", self, "update") == OK)
これなら1行で済むじゃんと思ったら、
https://github.com/godotengine/godot/issues/27576#issuecomment-532738045
(DeepL: assert() 文の中のコードには、決して副作用があってはいけません。さもなければ、リリースビルドでは決して実行されず、あなたのゲームを壊してしまうでしょう。)
というわけで、assert はダメ。
connect(...) のボツ案2
var error_code = player.connect("item_hit", self, "_on_Player_item_hit")
if error_code != 0: print("ERROR: ", error_code)
2行に収まるからいいかなと思ったけど、
以下のように、
func add_level():
level = load("res://Levels/Level" + str(current_level) + ".tscn").instance()
var error_code = level.connect("tree_exited", self, "change_level")
if error_code != 0: print("ERROR: ", error_code)
add_child(level)
player = level.get_node("Player")
error_code = player.connect("player_damaged", self, "_on_Player_player_damaged")
if error_code != 0: print("ERROR: ", error_code)
error_code = player.connect("item_hit", self, "_on_Player_item_hit")
if error_code != 0: print("ERROR: ", error_code)
複数行重なると冗長すぎる。関数を作って一行で済ます方法もありそうだけど絶対わかりにくい形になる。
signal のボツ案
signal を定義したスクリプトファイルにダミーのメソッドを追記。
func dummy(body):
emit_signal("item_hit", body.point)
単純にイヤだ。
引き続き、GitHubの長いスレッドを読むことにしました。
採用した対応策
CowThingさんの投稿内容を採用することにしました。
https://github.com/godotengine/godot/issues/27576#issuecomment-478839215
作業内容
メニュー「プロジェクト」→ プロジェクト設定 → GDScript → 下記引用の4項目をオフ → 閉じる
引用
- Unused Class Variable - Plenty of times I have a class variable that isn't actually used within the class itself, but is read by other objects in the game. This is especially a problem for custom Resource scripts, because they are used to store data, so most of their class variables aren't used at all within the script.
- Unused Argument - As mentioned above, when using _process(delta) without needing delta. This warning can be fixed already by changing delta to _delta, because a leading underscore silences it. But it's just more work adding underscores to every unused variable so I'd rather keep this warning disabled.
- Unused Signal - In a few cases I have a class that has a signal, but that signal is emitted from somewhere else instead of within the class itself. Another case is sometimes I'll use call_deferred("emit_signal", "my_signal"), which doesn't register as the signal being emitted, even though it is.
- Return Value Discarded - As mentioned above, there's plenty of times where I don't actually care if a return value is discarded because the function is doing something, and is only returning an error or a bool to notify if it succeeded.
最後に
GubHubのスレッドを追いかけて最終的に
https://github.com/godotengine/godot/pull/69002
にたどり着き、今後、" Don't warn about RETURN_VALUE_DISCARDED by default " ということになりそうなのは分かりました。
もしかしたら、他の3項目までOFFってしまうのはやりすぎなのかもしれません。
ただ、個人でこれから自由気ままに作る身としては、今回採用した CowThingさんの4つOFFする方法で行きたいと思います。
とはいえ今後、チーム制作に携わる、独りであっても規模が大きい作品を作る、単にスキルアップした、といった状況に合わせて見直す機会はあると考えています。