extends:继承。
_ready():第一次加载入场景中调用,相当于Unity中的Start()。
_process(delta):每一帧调用,相当于Unity中的Update()。delta是自上一帧经过的时间,类似于Time.deltatime。
pass:关键字pass的作用相当于空函数体("{}"),什么也不用做。
一、打印HelloWorld
extends Node
# Called when the node enters the scene tree for the first time.
func _ready():
print("Hello, World!")
二、语法(Syntax)
- 方法体不需要像基于C的语言一样添加花括号({ })。
- 类似Python的语法,使用缩进(Tab)表示代码的所属。
- 大小写敏感,例如:print()与Print()是不一样的。
- 语句末尾不需要添加分号(;)。
- 方法的括号后需要添加冒号(:),定义/调用的时候。
三、修改节点(Modifying nodes)
1、修改节点属性
如何获取节点属性在脚本中的名称?
鼠标悬浮到节点的属性面板上的某个属性,即可显示脚本中的属性名称。
如何在脚本中使用节点?
- 打开脚本编辑器。
- 鼠标选择场景中的需要修改的节点后,拖入到脚本中即可访问节点
extends Node
# Called when the node enters the scene tree for the first time.
func _ready():
#print("Hello, World!")
$Label.text = "Hello, World!"
四、输入(Input)
1、设置Input action
选择Project->Project Settings打开后,选择Input Map。
输入Action的名字,点击Add按钮添加Action。
选择创建的Action项,点击右侧的“+”按钮添加事件绑定。打开事件配置(Event Configuration)窗口后,在光标/插入点后显示为Listening for Input后,按下任意物理输入设备的按键即可自动找到对应的输入事件,点检OK按钮添加事件绑定。
2、在脚本中访问输入
使用Godot内置的_input(event)方法访问输入,每当在游戏中接收输入的时候就是调用_input方法。event参数用来获取触发行为的信息,例如鼠标、键盘的按下/释放。
- event.is_action_pressed("{action_name}"):按下
- event.is_action_released("{action_name}"):释放
- action_name为我们在Input Map中定义的Action,作为参数使用的时候不需要添加“{}”
func _input(event):
if event.is_action_pressed("my_action"): # 释放
$Label.modulate = Color.RED
if event.is_action_released("my_action"): # 按下
$Label.modulate = Color.GREEN
五、变量(Variables)
声明变量类型的关键字var
六、分支语句
1、语法
- 条件外不需要添加括号
- 条件后需要添加冒号(:)
if condition:
pass
if condition:
pass
else:
pass
if condition_A:
pass
elif condition_B:
pass
else:
pass
2、运算符
逻辑运算符
- 逻辑与(and)
- 逻辑或(or)
if condition_A and condition_B:
pass
if condition_A or condition_B:
pass
比较运算符
- 大于(>)
- 小于(<)
- 等于(==)
- 大于等于(>=)
- 小于等于(<=)
- 不等于(!=)
七、注释(Comment)
未完待续……