凉鞋的 Godot 笔记 201. 第三轮循环:引入变量

201. 第三轮循环:引入变量

在这一篇,我们进行第三轮 编辑-测试 循环。

在之前我们编写了 输出 Hello Godot 的脚本,如下:

python 复制代码
extends Node


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


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

如果我们要输出 10 次 Hello Godot 该怎么办呢?

答案很简单,就是复制十行 print("Hello Godot"),代码如下:

python 复制代码
extends Node


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


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

这样当我们运行次场景后,结果如下:

总共输出了十次 Hello Godot。

此时我突然想要把输出十次 Hello Godot 改成输出十次 Hello GDScript。

那么最简单的方式,就是直接修改代码,如下:

python 复制代码
extends Node


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


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

运行之后结果如下:

但是这样太不优雅了,我们需要复制粘贴十次,如果我们有 100 个甚至 1000 个 Hello Godot,那么我们可能需要复制粘贴很多次,或者使用代码编辑器所提供的查找/替换功能完成。

比较优雅的方式就是引入一个变量,代码如下所示:

python 复制代码
extends Node


# Called when the node enters the scene tree for the first time.
func _ready():
	var text_to_print = "Hello GDScript"
	print(text_to_print)
	print(text_to_print)
	print(text_to_print)
	print(text_to_print)
	print(text_to_print)
	
	print(text_to_print)
	print(text_to_print)
	print(text_to_print)
	print(text_to_print)
	print(text_to_print)
	
	pass # Replace with function body.


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

这样当我们想要输出十次 Hello World 那么我们只需要修改变量的值即可,如下所示:

python 复制代码
extends Node


# Called when the node enters the scene tree for the first time.
func _ready():
	var text_to_print = "Hello World"
	print(text_to_print)
	print(text_to_print)
	print(text_to_print)
	print(text_to_print)
	print(text_to_print)
	
	print(text_to_print)
	print(text_to_print)
	print(text_to_print)
	print(text_to_print)
	print(text_to_print)
	
	pass # Replace with function body.


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

这样非常优雅。

我们在代码中新引入的 text_to_print 叫做变量。

变量可以存储一个值,然后在接下来可以通过这个变量来代替具体的值。

比如 text_to_print 是 "Hello World",那么接下来的每一句 print(text_to_print) 其实都是 print("Hello World")。

变量给编程带来了巨大的便利。

当然,变量是每一个程序语言都有的,而每一个游戏引擎不管是提供专业脚本支持还是可视化脚本支持都会提供变量的使用,所以变量也是通识部分的内容,在接下来的篇幅里,笔者会好好介绍变量,以及 Godot 的 GDScript 中的变量使用。

这一篇的内容就这些,我们下一篇再见,拜拜。

知识地图

转载请注明 凉鞋的笔记

相关推荐
丑小鸭是白天鹅2 小时前
嵌入式C语言学习笔记之枚举、联合体
c语言·笔记·学习
十一10243 小时前
FX10/20 (CYUSB401X)开发笔记5 固件架构
笔记
FakeOccupational3 小时前
【电路笔记 通信】AXI4-Lite协议 FPGA实现 & Valid-Ready Handshake 握手协议
笔记·fpga开发
奶黄小甜包4 小时前
C语言零基础第18讲:自定义类型—结构体
c语言·数据结构·笔记·学习
rannn_1116 小时前
【MySQL学习|黑马笔记|Day7】触发器和锁(全局锁、表级锁、行级锁、)
笔记·后端·学习·mysql
草莓熊Lotso7 小时前
《详解 C++ Date 类的设计与实现:从运算符重载到功能测试》
开发语言·c++·经验分享·笔记·其他
_Kayo_13 小时前
node.js 学习笔记3 HTTP
笔记·学习
星星火柴93617 小时前
关于“双指针法“的总结
数据结构·c++·笔记·学习·算法
Cx330❀19 小时前
【数据结构初阶】--排序(五):计数排序,排序算法复杂度对比和稳定性分析
c语言·数据结构·经验分享·笔记·算法·排序算法
小幽余生不加糖20 小时前
电路方案分析(二十二)适用于音频应用的25-50W反激电源方案
人工智能·笔记·学习·音视频