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

2023-10-17 20:04

本文主要是介绍凉鞋的 Godot 笔记 201. 第三轮循环:引入变量,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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

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

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

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”),代码如下:

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

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

image-20231002160704307

总共输出了十次 Hello Godot。

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

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

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

运行之后结果如下:

image-20231002160952674

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

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

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 那么我们只需要修改变量的值即可,如下所示:

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 中的变量使用。

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

知识地图

image-20231002162151655

转载请注明 凉鞋的笔记

这篇关于凉鞋的 Godot 笔记 201. 第三轮循环:引入变量的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/227648

相关文章

Python中顺序结构和循环结构示例代码

《Python中顺序结构和循环结构示例代码》:本文主要介绍Python中的条件语句和循环语句,条件语句用于根据条件执行不同的代码块,循环语句用于重复执行一段代码,文章还详细说明了range函数的使... 目录一、条件语句(1)条件语句的定义(2)条件语句的语法(a)单分支 if(b)双分支 if-else(

Python判断for循环最后一次的6种方法

《Python判断for循环最后一次的6种方法》在Python中,通常我们不会直接判断for循环是否正在执行最后一次迭代,因为Python的for循环是基于可迭代对象的,它不知道也不关心迭代的内部状态... 目录1.使用enuhttp://www.chinasem.cnmerate()和len()来判断for

Java循环创建对象内存溢出的解决方法

《Java循环创建对象内存溢出的解决方法》在Java中,如果在循环中不当地创建大量对象而不及时释放内存,很容易导致内存溢出(OutOfMemoryError),所以本文给大家介绍了Java循环创建对象... 目录问题1. 解决方案2. 示例代码2.1 原始版本(可能导致内存溢出)2.2 修改后的版本问题在

浅析Rust多线程中如何安全的使用变量

《浅析Rust多线程中如何安全的使用变量》这篇文章主要为大家详细介绍了Rust如何在线程的闭包中安全的使用变量,包括共享变量和修改变量,文中的示例代码讲解详细,有需要的小伙伴可以参考下... 目录1. 向线程传递变量2. 多线程共享变量引用3. 多线程中修改变量4. 总结在Rust语言中,一个既引人入胜又可

SpringBoot项目引入token设置方式

《SpringBoot项目引入token设置方式》本文详细介绍了JWT(JSONWebToken)的基本概念、结构、应用场景以及工作原理,通过动手实践,展示了如何在SpringBoot项目中实现JWT... 目录一. 先了解熟悉JWT(jsON Web Token)1. JSON Web Token是什么鬼

JAVA中while循环的使用与注意事项

《JAVA中while循环的使用与注意事项》:本文主要介绍while循环在编程中的应用,包括其基本结构、语句示例、适用场景以及注意事项,文中通过代码介绍的非常详细,需要的朋友可以参考下... 目录while循环1. 什么是while循环2. while循环的语句3.while循环的适用场景以及优势4. 注意

java如何调用kettle设置变量和参数

《java如何调用kettle设置变量和参数》文章简要介绍了如何在Java中调用Kettle,并重点讨论了变量和参数的区别,以及在Java代码中如何正确设置和使用这些变量,避免覆盖Kettle中已设置... 目录Java调用kettle设置变量和参数java代码中变量会覆盖kettle里面设置的变量总结ja

Perl 特殊变量详解

《Perl特殊变量详解》Perl语言中包含了许多特殊变量,这些变量在Perl程序的执行过程中扮演着重要的角色,:本文主要介绍Perl特殊变量,需要的朋友可以参考下... perl 特殊变量Perl 语言中包含了许多特殊变量,这些变量在 Perl 程序的执行过程中扮演着重要的角色。特殊变量通常用于存储程序的

Python中的异步:async 和 await以及操作中的事件循环、回调和异常

《Python中的异步:async和await以及操作中的事件循环、回调和异常》在现代编程中,异步操作在处理I/O密集型任务时,可以显著提高程序的性能和响应速度,Python提供了asyn... 目录引言什么是异步操作?python 中的异步编程基础async 和 await 关键字asyncio 模块理论

好题——hdu2522(小数问题:求1/n的第一个循环节)

好喜欢这题,第一次做小数问题,一开始真心没思路,然后参考了网上的一些资料。 知识点***********************************无限不循环小数即无理数,不能写作两整数之比*****************************(一开始没想到,小学没学好) 此题1/n肯定是一个有限循环小数,了解这些后就能做此题了。 按照除法的机制,用一个函数表示出来就可以了,代码如下