LoginSignup
4
0

データパックに潜むバグとの戦い方

Last updated at Posted at 2023-12-12

この記事は Minecraft Command Advent Calendar 2023 13 日目の記事です。1

はじめに

  • 「あれ~????」
  • 「なんかおかしいぞ」
  • 「なにもしてないのにこわれた」
  • 「バグった!」

この記事を読んでる方はこの言葉にとても心当たりがあるのでは無いでしょうか?
つらいですよね、バグ。
特に一般のプログラミング言語に比べ、データパックは色々々々と複雑に書かなければいけないことが多いためバグをとても引き起こしやすいと私は考えています。
この記事ではそんなバグとの戦い方をいくつかのシチュエーションを例にして解説したいと思います。

scoreboard や storage に絡むバグと戦う

scoreboardstorage が関係する処理は非常にバグを引き起こしやすい箇所の一つです。
そのようなコマンドが動かないことに気づいたときは、「問題の直前で scoreboardstorage の値がどうなっているか」 を調べることで原因の推測に役立つことが多くあります。

例えば

execute if score %holder ObjectiveX matches 2 run function advent_calendar:meow

execute if data storage advent_calendar:piyo abc{def: 3} run function advent_calendar:meow

のようなコマンドが動かないことに気づいたときは、こうしてしまいましょう。

tellraw @a [{"text": "%holder: "}, {"score": {"objective": "ObjectiveX", "name": "%holder"}}]
execute if score %holder ObjectiveX matches 2 run function advent_calendar:meow

tellraw @a [{"text": "abc.def: "}, {"storage": "advent_calendar:piyo", "nbt": "abc.def"}]
execute if data storage advent_calendar:piyo abc{def: 3} run function advent_calendar:meow

これでどんな値が入っているか一目でわかるようになりました。やったね。

長文 execute に潜むバグと戦う

複数のサブコマンドで構成される execute もまた非常にバグを引き起こしやすい箇所の一つです。
そのようなコマンドが動かないことに気づいたときは、「execute コマンドがどこまで動いているか」 を調べることでどこに問題があるかを特定することが出来ます。

例えば

execute if data storage advent_calendar:hoge {nya: true} as @a[distance=..1, scores={ObjectiveY=1..}] if score %holder ObjectiveZ matches ..5 at @p facing entity @e[tag=pointP] eyes run function advent_calendar:meow2

のような execute コマンドが動かないことに気づいたときは、こうしてしまいましょう。

execute run say 1
execute if data storage advent_calendar:hoge {nya: true} run say 2
execute if data storage advent_calendar:hoge {nya: true} as @a[distance=..1, scores={ObjectiveY=1..}] run say 3
execute if data storage advent_calendar:hoge {nya: true} as @a[distance=..1, scores={ObjectiveY=1..}] if score %score ObjectiveZ matches ..5 run say 4
execute if data storage advent_calendar:hoge {nya: true} as @a[distance=..1, scores={ObjectiveY=1..}] if score %score ObjectiveZ matches ..5 at @p run say 5
execute if data storage advent_calendar:hoge {nya: true} as @a[distance=..1, scores={ObjectiveY=1..}] if score %score ObjectiveZ matches ..5 at @p facing entity @e[tag=pointP] eyes run say 6

これでどこまで動いているかが一目でわかるようになりました。やったー!

この記事で本当に伝えたかったこと

要はバグの原因がわからないときは状態を目に見えるようにしよう!!

  1. いつも通りアドカレを眺めていたら 12/13 のシリーズ 3 が空いてることに気づき、急に登録してなかった記事が書きたくなったので当日 (12/13) に参加登録をしました。これが パニックモンスター 駆動生活です。ちなみに、当然まともに校閲をする時間がありませんでした。こういうことはやめたほうが良いです。っていうかまだ後に控えてる 4 記事に手すらつけていないのに何やってるんだ......

4
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
0