owned this note
owned this note
Published
Linked with GitHub
###### tags: `minecraft`
<style>
* {font-family: 'Terminal','ヒラギノ丸ゴ ProN','Hiragino Maru Gothic ProN',YuGothic,'Yu Gothic','Hiragino Kaku Gothic ProN','ヒラギノ角ゴ ProN W3','Verdana','Arial',sans-serif;}
.markdown-body {font-family: 'Terminal','ヒラギノ丸ゴ ProN','Hiragino Maru Gothic ProN',YuGothic,'Yu Gothic','Hiragino Kaku Gothic ProN','ヒラギノ角ゴ ProN W3','Verdana','Arial',sans-serif;}
h2 {padding-top: 50px;}
img {margin: 20px auto;}
.alert-success {margin-top: 50px;}
.alert h2 {padding-top: 0px;}
.part {letter-spacing: .1rem;}
.markdown-body h3 {margin-top: 12px;margin-bottom: 12px;}
.markdown-body p {font-size: 1.25em;margin-top: 12px; margin-bottom: 16px;}
.markdown-body li {font-size: 1.25em;}
.markdown-body pre {font-size: 20px;}
.focus {color: red;font-weight: bold;}
.link-box {display: flex;justify-content: space-between; margin-top: 50px;margin-bottom: 50px;}
a.link {display: block; width: 50%; padding: 10px 15px; text-align: center; color: #fff; font-weight: bold; font-size: 20px; letter-spacing: .1em; text-decoration: none;}
a.link:hover {opacity: 0.85;}
a.prev {background: linear-gradient(to bottom, #1A7BBD 0%, #1A7BBD 50%, #125684 51%, #125684 100%); border-radius: 10px 0 0 10px;}
a.next {background: linear-gradient(to bottom, #D76820 0%, #D76820 50%, #964816 51%, #964816 100%); border-radius: 0 10px 10px 0;}
</style>
> [name=shinya kunisada]
# 【第18回】 アイテムを識別してみよう!2
:::success
## 【今回の目標】
- ### Table(テーブル)型の復習
- ### nil(ニル)を知ろう!
- ### スロットのアイテムを識別してみよう!
:::
## ◆ 【復習】Table型
### Table型は、いくつかの変数を入れることができる変数をイメージしよう!

## ◆ 【復習】Table型
### さっきの図をプログラムに書くとこんな感じ
### この変数tblが<span class="focus">Table(テーブル)型</span>といわれるものだよ

## ◆ 【復習】Table型の作り方

### 変数を作るときとほとんど一緒で、違うのは右側だね
### 値の部分には<span class="focus">数、文字、TrueとFalse(真偽値)、変数、テーブル</span>を入れることができるよ
### また,値は「<span class="focus"> , </span>」で区切ればいくらでもTableに入れることができるよ!
## ◆ 【復習】Table型の中身を読む
### Table型の中身を読むときは、<span class="focus">変数名</span>[<span class="focus">番号</span>]で読むことができるよ

## ◆ 【復習】Table型の中身を書きかえる
### 読むときと同じように <span class="focus">変数名</span>[<span class="focus">番号</span>] = <span class="focus">値</span>で中身の値を書きかえることができるよ

## ◆ 【復習】値に名前をつける
### Table型では<span class="focus">値</span>に、1番目、2番目のような番号ではなく、「<span class="focus">Tech</span>」のような<span class="focus">名前</span>をつけることができるんだ!

## ◆ 【復習】名前のついた値の読み書き
### 名前のついた値は番号のかわりにその名前を使って<span class="focus">変数</span>["<span class="focus">名前</span>"]と書けば読み書きできるよ!

## ◆ スロットのアイテムを識別しよう!
### 今日やることは前とちがって、<span class="focus">スロット内のアイテムを識別する</span>こと

## ◆ スロットのアイテムを識別する命令
### <span class="focus">turtle.getItemDetail(スロット番号)命令</span>を使うことで、スロットのアイテムを識別することができる!

## ◆ Table型の変数の中身
### 変数は下のようなTable型になっていて、<span class="focus">["name"]</span>で、そのアイテムの名前を調べることができる。
```lua=
{
count = 64, <-- アイテムの個数
name = "minecraft:dirt", <-- アイテムの名前
damage = 0, <-- アイテムの耐久値の減り
}
```
:::success
## 【課題1】アイテムの種類を表示しよう!
:::
### <span class="focus">turtle.getItemDetail()命令</span>を使って、手持ちのアイテムの名前を表示してみよう!

## ◆ エラー発生
### 何もアイテムが入っていないスロットのアイテム名を表示してみよう!
### エラーが起こるかな?

## ◆ データそのものを表示
### このとき、["name"]じゃなくて、データそのものを<span class="focus">print()命令</span>で表示してみよう!

## ◆ nil
### すると、<span class="focus">nill(ニル)</span>が表示された!
### nillは変数の中身が<span class="focus">何もない(からっぽ)という意味</span>

### データがからっぽだと、その中にはnameという名前の値はないよね?
### だからエラーになったんだ。

## ◆ nilの特徴(とくちょう)
### if文の条件式に入れると<span class="focus">falseと同じ意味</span>になる。
### これはすごく重要だから覚えておこう!

:::success
## 【課題2】エラーを回避(かいひ)しよう!
:::
### <span class="focus">nilの特徴(とくちょう)を使って</span>、スロットにアイテムがあるときだけアイテムの名前を表示してみよう。

:::success
## 【課題3】すべてのスロットのアイテムを調べる
:::
### for文を使って、手持ちのアイテムの名前をすべて表示してみよう!
### エラー回避を忘れずに!

## ◆ アイテムの個数(こすう)
### <span class="focus">turtle.getItemDetail(スロット番号)命令</span>では、<span class="focus">["count"]</span>で、そのスロット番号に入っているアイテムの個数を調べることができるんだ!

:::success
## 【課題4】土は全部でいくつ?
:::
### <span class="focus">変数、for文、足し算</span>をうまく使って、タートルが持っている土の合計を調べて表示させよう!

## ◆ どんなことができる?
- ### ダイヤを10個取ってきたら帰ってくるようにする
- ### インベントリから土と砂利を捨てる
- ### 水や溶岩を見つけてバケツで取ってくる
### いろいろなことに活用できそうだね!
<div class="link-box">
<a href="https://hackmd.io/JLskgXnaSNaufS6N9rDthQ" class="link prev">前に戻る</a>
<a href="https://hackmd.io/Q82QXDJOQ86_X2JchtdVqg" class="link next">次に進む</a>
</div>