内容来自千问AI整理,感觉还不错,分享一下。 在 Godot 中,节点类提供了大量实用的内置方法。以下是常用的节点方法分类:
1. 节点管理方法
# 添加和移除节点
add_child(node)
remove_child(node)
queue_free() # 安全删除节点
free() # 立即删除(不推荐)
# 节点查找
get_node(path) # 根据路径获取节点
has_node(path) # 检查节点是否存在
get_parent() # 获取父节点
get_children() # 获取所有子节点
# 节点遍历
get_child(index)
get_child_count()
find_child(name, recursive=true, owned=false)
2. 信号相关方法
# 连接和断开信号
connect("signal_name", callable)
disconnect("signal_name", callable)
is_connected("signal_name", callable)
# 自定义信号
signal my_signal(param1, param2)
# 发射信号
emit_signal("my_signal", param1, param2)
3. 节点生命周期方法
func _ready(): # 节点及其子节点都已添加到场景树
func _enter_tree(): # 节点进入场景树
func _exit_tree(): # 节点离开场景树
func _process(delta): # 每帧调用(非物理)
func _physics_process(delta): # 每物理帧调用
4. 场景树相关方法
# 场景树操作
get_tree() # 获取场景树
get_viewport() # 获取视口
is_inside_tree() # 检查是否在场景树中
is_processing() # 检查是否在处理
is_physics_processing() # 检查是否在物理处理
# 场景切换
get_tree().change_scene_to_file("res://scene.tscn")
get_tree().change_scene_to_packed(packed_scene)
get_tree().reload_current_scene()
5. 变量和属性相关
# 属性设置
set("property_name", value)
get("property_name")
has_meta("meta_name") # 检查元数据
set_meta("meta_name", value)
get_meta("meta_name")
# 变量检查
has_method("method_name")
call("method_name", args...)
6. 节点可见性和状态
# 可见性控制
show()
hide()
set_visible(bool)
is_visible_in_tree() # 检查是否在场景中可见
# 处理控制
set_process(true/false)
set_physics_process(true/false)
set_process_input(true/false)
7. 输入处理方法
# 输入相关
func _input(event): # 处理所有输入
func _gui_input(event): # 处理GUI输入
func _unhandled_input(event): # 处理未处理的输入
# 输入状态检查
Input.is_action_pressed("action_name")
Input.is_key_pressed(KEY_SPACE)
Input.get_mouse_position()
8. 节点坐标和变换
# 位置变换
global_position # 全局位置
position # 本地位置
rotation # 旋转角度
scale # 缩放
# 坐标转换
to_global(local_position)
to_local(global_position)
global_transform # 全局变换矩阵
transform # 本地变换矩阵
9. 2D/3D 特定方法
2D 节点
# 2D 变换
global_position
position
rotation
scale
rotation_degrees
# 2D 工具
look_at(target_position)
rotate(angle)
move_local_x(distance)
move_local_y(distance)
3D 节点
# 3D 变换
global_transform
transform
rotation
scale
translation
# 3D 工具
look_at(target, up=Vector3.UP)
rotate_x(angle), rotate_y(angle), rotate_z(angle)
10. 调试和工具方法
# 调试输出
print("message")
push_error("error message")
push_warning("warning message")
# 断点调试
assert(condition, "error message")
11. 资源和文件操作
# 资源加载
preload("res://path.tscn") # 编译时加载
load("res://path.tscn") # 运行时加载
# 场景实例化
var scene_instance = PackedScene.new()
scene_instance.instantiate()
12. 计时器和延迟调用
# 延迟调用
call_deferred("method_name", args...)
await get_tree().create_timer(1.0).timeout # 等待1秒
# 计时器节点
var timer = Timer.new()
timer.timeout.connect(callback)
add_child(timer)
timer.start(duration)
13. 组和标签方法
# 节点组操作
add_to_group("group_name")
remove_from_group("group_name")
is_in_group("group_name")
# 遍历组内节点
for node in get_tree().get_nodes_in_group("enemies"):
node.take_damage(10)
14. 常用的全局方法
# 随机数
randf() # 0.0 到 1.0 的随机浮点数
randi() # 随机整数
rand_range(min, max) # 指定范围的随机数
# 数学函数
lerp(from, to, weight) # 线性插值
clamp(value, min, max) # 限制值范围
stepify(value, step) # 步进化值
# 字符串和转换
str(variable) # 转换为字符串
int(string) # 转换为整数
float(string) # 转换为浮点数
15. 实用的组合方法
# 等待节点
await node.ready # 等待节点就绪
await node.tree_exited # 等待节点离开场景树
# 安全的节点操作
if has_node("path"):
var node = get_node("path")
# 安全操作
# 批量操作
for child in get_children():
if child is Node2D:
child.visible = false
这些方法是 Godot 开发中最常用的节点操作方法,掌握它们可以大大提高开发效率。根据具体需求选择合适的方法,可以让代码更加简洁和高效。