You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

GDScript插件代码报错求助:新手无解决思路

GDScript插件代码错误修复方案

错误对应修复点

以下是针对每个报错的具体修复:

  • Line16: Identifier "File" not declared:Godot 4 已移除File类,改用FileAccess的静态方法判断文件是否存在。
  • Line32/62: DirAccess无法实例化:DirAccess是抽象类,需通过DirAccess.open()静态方法获取实例,同时处理打开失败的情况。
  • Line56: plus_file方法不存在:Godot 4 中String不再提供plus_file方法,改用路径拼接方式构建文件路径。
  • 变量类型推断错误:为fs和file_name明确指定变量类型,帮助Godot完成类型推断。
  • 额外逻辑问题:代码中UserProfiles.uuid应为当前脚本的uuid变量;list_profiles中加载资源时需使用完整文件路径,而非仅文件名。

修复后的完整代码

extends Node

const ROOT_DIR := "user://user_settings"

var current_player := "Player"
var _loaded_profile: Profile
var uuid := UUID.v4()

func profile_exists(profile_name: String) -> bool:
    var file_path := _get_file_path(profile_name)
    return FileAccess.file_exists(file_path)


# Returns the profile designated by the provided name.
# If the profile does not exist, it will be created on the fly. If you do not
# want that to happen, check with `profile_exists` first.
#
# Loading a profile will set it as the current file name.
func get_profile(profile_name: String = current_player) -> Profile:
    if profile_name == current_player and _loaded_profile:
        return _loaded_profile
    
    current_player = profile_name
    var file_path := _get_file_path(profile_name)
    
    if not profile_exists(profile_name):
        var fs: DirAccess = DirAccess.open(ROOT_DIR)
        if fs == null:
            Log.error("Failed to open directory: %s" % ROOT_DIR)
            var user_profile := Profile.new()
            user_profile.resource_path = file_path
            user_profile.player_name = profile_name
            _loaded_profile = user_profile
            Log.info({
                "id": uuid,
                "profileName": profile_name
            }, "created profile (fallback)")
            return user_profile
        
        var directory := file_path.get_base_dir()
        fs.make_dir_recursive(directory)
        
        var user_profile := Profile.new()
        user_profile.resource_path = file_path
        user_profile.player_name = profile_name
        
        ResourceSaver.save(user_profile, file_path)
        
        _loaded_profile = user_profile
        Log.info({
            "id": uuid,
            "profileName": profile_name
        }, "created profile")
        return user_profile
    
    Log.info({
        "id": uuid,
        "profileName": profile_name
    }, "loaded profile")
    _loaded_profile = ResourceLoader.load(file_path) as Profile
    return _loaded_profile


func _get_file_path(file_name: String) -> String:
    return "%s/%s.tres" % [ROOT_DIR, file_name]


func list_profiles() -> PackedStringArray:
    var profiles := PackedStringArray()
    
    var fs: DirAccess = DirAccess.open(ROOT_DIR)
    if fs == null:
        profiles.push_back(current_player)
        return profiles
    
    fs.list_dir_begin()
    var file_name: String = fs.get_next()
    while not file_name.is_empty():
        if fs.current_is_dir() or file_name.get_extension() != "tres":
            file_name = fs.get_next()
            continue
        
        var full_path := "%s/%s" % [ROOT_DIR, file_name]
        var profile = ResourceLoader.load(full_path) as Profile
        if profile:
            profiles.push_back(profile.player_name)
        
        file_name = fs.get_next()

    return profiles

额外说明

  • 新增资源保存逻辑:新建Profile后调用ResourceSaver.save(),确保配置文件写入磁盘,避免下次加载失败。
  • 添加目录打开失败的错误处理,防止空指针异常。
  • 完成所有Godot 3到4的API适配,包括文件操作、路径处理等核心逻辑。

内容的提问来源于stack exchange,提问作者The Animator

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 21:16:11