0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【1から】Godot Engineで簡易電卓を作る Part2

Last updated at Posted at 2021-11-22

今回は「+」「=」ボタンを追加して計算式を作るまでの過程を解説します。

前回
【1から】Godot Engineで簡易電卓を作る Part1

(実際の計算部分は次回)

はじめに”2段目”の「Node2D」をクリックしてコピーします(ctrl+C)
スクリーンショット 2021-11-22 15.03.03.png
そして次に”一番上”の「Node2D」をクリックで選んでからペースト(ctrl+V)すると「Node2D-Button」の構造が複製されます。
スクリーンショット 2021-11-22 15.03.24.png
コピーされたものはクリック&ドラッグで移動させられます。
スクリーンショット 2021-11-22 15.03.55.png
移動させたらコピーした「Buttonn」をクリックして「インスペクター」の「Text」を編集して「+」と書き換えます。
スクリーンショット 2021-11-22 15.05.09.png
ついでに「=」ボタンも追加します。
スクリーンショット 2021-11-22 15.05.39.png
実行してボタンを押してキチンと出力されるか確認します。
スクリーンショット 2021-11-22 15.05.57.png
このままでは数式(例:11111+11=)を作れないので、プログラムを書き換えます。

数式にする

まずは、ボタンを押すたびにボタンの値を変数に入れて、それを出力するプログラムを作ってみます。

以下のプログラムを全部コピーしてみましょう。

button.gd
ends Node2D


# Declare member variables here. Examples:
# var a = 2
# var b = "text"
var num = "0"

# Called when the node enters the scene tree for the first time.
func _ready():
	pass # Replace with function body.


# Called every frame. 'delta' is the elapsed time since the previous frame.
# func _process(delta):
#	pass


func _on_Button_button_down():
	num += $Button.text
	print(num)

変更点:変数の宣言 var num = "0"
pythonみたく宣言なしに使おうとするとエラーになります。

”0”にして文字列型なのは、数字だと+=で代入すると「1,2,3...」と加算されてしまうので、文字列として足していきます。

そしてnumに$Button.textを代入して、numをprint。

実行して1を連打すると文字列が表示されるようになります。
スクリーンショット 2021-11-22 15.11.42.png

ただ、他の文字を入力すると途端に続かなくなり、それぞれ1や+、=だけが続くように出力されます。
スクリーンショット 2021-11-22 15.15.09.png
これは各「Node2D-button」の構造において、それぞれ個別に変数numが設定され、そこに個別のボタンテキストの値が代入されていくことで、↑のような結果になるわけです。

つまり、それぞれが独立して動いているので、全てを同じ変数に入れるためには、共有できる変数を用意する必要があります。

ただ、「button.gd」に変数を用意しても先程と同じ結果になるので、一番上の階層にある「Node2D」に別のプログラムを書き込んでいきます。

「Node2D」を選択して、シーンカテゴリ右上の「ページアイコン(緑色の+がついたもの)」をクリックしてファイルを作成します(ここでは名前をmainに変更)。
スクリーンショット 2021-11-22 15.15.36.png
追加されたら
スクリーンショット 2021-11-22 15.19.47.png
main.gdにて「var num = "0"」と打ち込み
スクリーンショット 2021-11-22 15.22.31.png
button.gdは以下に書き換え

button.gd
extends Node2D


# Declare member variables here. Examples:
# var a = 2
# var b = "text"
onready var main = get_node("/root/Node2D")

# Called when the node enters the scene tree for the first time.
func _ready():
	pass # Replace with function body.


# Called every frame. 'delta' is the elapsed time since the previous frame.
# func _process(delta):
#	pass


func _on_Button_button_down():
	main.num += $Button.text
	print(main.num)

変更点
1.onready var main = get_node("/root/Node2D")
main.gdのnum変数をbutton.gdでも使えようにするための宣言。

onreadyは付けないとエラーが帰ってきます。

2.main.num += $Button.text と print(main.num)
そしてmain.gdにあるtextに値を代入する場合はmain.numと書くことになります。

そして実行すると打ち込んだ文字がそのまま直列でひょうじされるようになりました。

スクリーンショット 2021-11-22 15.35.52.png

次回はここから計算ができるようになる方法を解説していきます。

【1から】Godot Engineで簡易電卓を作る Part3

0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?