从Godot 3转Godot 4:GDScript中export(Resource)替代方案咨询
Godot 4 中替代 Godot 3
export(Resource) 的等价写法 在 Godot 4 的 GDScript 里,原 export(Resource) 的功能可以直接用 @export 加类型注解 实现,写法更简洁直观:
基本等价写法
Godot 3 中的代码:
export(Resource) var my_resource
对应 Godot 4 的代码:
@export var my_resource: Resource
指定具体资源子类
如果你的平台跳跃游戏需要绑定特定类型的资源(比如自定义的跳跃配置、动画资源等),直接写具体类型即可,编辑器会自动过滤可选资源:
# 示例:绑定自定义的跳跃设置资源 @export var jump_config: JumpSettings # 示例:绑定动画资源 @export var idle_animation: AnimationResource
允许空资源(可选)
如果需要允许该属性为空,可在类型后加 ?:
@export var optional_resource: Resource?
平台跳跃游戏实用示例
假设你定义了一个存储跳跃参数的自定义资源:
# JumpSettings.gd extends Resource class_name JumpSettings @export var jump_height: float = 800.0 @export var gravity: float = 2000.0 @export var coyote_time: float = 0.1
在玩家角色脚本中使用这个资源:
# Player.gd extends CharacterBody2D @export var jump_settings: JumpSettings func _physics_process(delta): if Input.is_action_just_pressed("jump") and is_on_floor(): velocity.y = -jump_settings.jump_height velocity.y += jump_settings.gravity * delta move_and_slide()
这样就能在编辑器面板中直接拖放 JumpSettings 资源实例到玩家节点的属性上,完全替代 Godot 3 中 export(Resource) 的功能。
内容的提问来源于stack exchange,提问作者100k7gm
相关产品推荐
相关产品推荐

