下载客户端

如何使用Godot保存和加载数据

2026-02-14 13:00:21
发布在Godot Engine
转载

AI智能总结导读

这篇教程讲解了如何用Godot Engine实现数据的保存与加载,包含设置保存路径、文件名等变量,创建自定义存储目录,以及编写加密保存和加载数据的函数,帮助开发者掌握Godot的数据存储操作。

In this guide you will learn how to save and load data using Godot Engine Variables Firstly lest get the variables needed for saving and loading data. var Player_Directory = "user://Player/" var Money_File = Player_Directory + "Data.dat" var Data = 0 1 Line : The first variable is for the path that saves our data. By " user:// " it means that user path of your computer is windows it's " C: Users (Your Name) AppData Roaming (The name of the Godot Project) " The " Player/ " is a custom directory is side of the file mention before that we are going make using Godot later.(It can be anything you want) 2 Line : It's the file that we are going to save date and load data By " Player_Directory + " it tells that the file is in the Custom folder that we make to the Program. You can have it just " Data.dat " if you want the file in main folder. " Data.dat " is the file name that we are going make later and save data to (You can have it to any name you want) 3 Line : It's the Float , String or the data that we are going to save. " Data " is just a name you can have anything instead of just Data " 0 " is the data that we are going to save and load you can have it whatever you need instead of 0 Creating the Directory Lets see how to make that custom folder in the main folder mentioned earlier. func Player_Dir(): var Player_Dir = Directory.new() if !Player_Dir.dir_exists(Player_Directory): Player_Dir.make_dir_recursive(Player_Directory) 1 Line : It's the function that we are going to call later in order to make the custom folder.(You can have any name to it) 2 Line : It's just for ease of access. (You can have name to the variable) 3 Line : It check if the directory is already there and if its not it will continue. In my code it checks if its not there and continue you can have it to check if its there and not to continue. Make sure to put whatever you put as the name of the variable that contains the path instead of " Player_ Directory " in the brackets. 4 Line : just keep it as it is only change " Player_Dir " if changed the variable name in 2 line, and like the 3 line only change " Player_Directory " in brackets if didn't use it earlier. Saving Data Let's see how can we save data. func Data_File_Create(): var file__ = File.new() var error__ = file__.open_encrypted_with_pass(Money_File, File.WRITE, "321TestCode") if error__ == OK: file__.store_var(Data) file__.close() 1 Line : It Creates the function for saving data that we are going to call later.(You can have anything you want) 2 Line : Like before its just for ease of access.(You can have it whatever you want) 3 Line : It creates the file for saving. " file__ " is the variable we created on the 2 line.(make sure to keep it the name you put for that variable) " open_encrypted_with_pass " is for saving the data encrpted in order to make it not editable by the user. " Money_File " is the file's directory variable we created before in the variable section.(make sure to keep it the name you put for that variable) " File.WRITE " is to tell the program that we want to write the file. " 321TestCode " is the encrypt pass that we have to use in order to load the data later.(You can have it whatever you want it to) 4 Line : It checks is the file is successfully created and continues. 5 Line : It stores the value you want to save in to the file. " Data " is the variable we created in the variable section.(amake sure to keep it the name you put for that variable) 6 Line : it closes file from the program.(This is very important so dont forget to enter this line) Loading Data Let's see how to load the data saved in to the file. func Load_Data_File(): var file_ = File.new() if file_.file_exists(Money_File): var error = file_.open_encrypted_with_pass(Money_File, File.READ, "321TestCode") if error == OK: var loaded_data_ = file_.get_var() Data = loaded_bal_ file_.close() 1 Line : It Creates the function for loading data that we are going to call later.(You can have anything you want) 2 Line : Like before its just for ease of access.(You can have it whatever you want) 3 Line : It checks if there is file in the " Money_File " variable directory wich we created in the variable section.(make sure to keep it the name you put for that variable) 4 Line : It open the file for loading data from it. " open_encrypted_with_pass " is very important since we saved data encrypted so we'll have to enter this line. " Money_File " is the file's directory variable we created before in the variable section.(make sure to keep it the name you put for that variable) " File.WRITE " is to tell the program that we want to read the file instead of writing it. " 321TestCode " is the pass we entered in the saving data section.(Make sure to keep it whatever you put for that before) 5 Line : It checks of the file successfully loaded and continue. 6 Line : It saves the loaded file's data into a variable.(You can only change the name of the variable other than that keep everything like it is) 7 Line : It saves the loaded data in to the main data variable that we created in the variable section so that next time it saves in will save the loaded data instead of default count we gave to it.(" loaded_data_ " van be changed to whatever you put to it in line 6, " Data " should be the nhme you put to the main data variable in the variable section) 8 Line : it closes file from the program.(This is very important so dont forget to enter this line)

评论

共0条评论
face
inputImg
相关阅读
最新更新

最新更新

  • 释放Godot的全部潜能 — 你是否一直想知道Godot引擎能实现什么?更小的构建体积、更简洁的工作流程、更出色的调试选项。在Godot游戏引擎中,你确实可以进行大量自定义设置!每个使用该引…
  • 贝壳纹理制作完全指南4.4 — 这是一份关于在Godot 4.4.1中制作外壳纹理的指南。 截图 链接已屏蔽 链接2已屏蔽
  • Android 导出设置 — 1. 必要工具 首先我们需要Android Studio,因为通过它我们可以安装Godot创建APK所需的SDK。 2. 安装Android Studio和SD…
  • 使用C++在Godot中构建完整游戏! — 在GODOT中使用C++开发游戏?本指南将介绍相关可能性以及各种方法的优缺点,还会讲解使用Godot Engine和C++进行开发所需的一切知识。该指南包含7个…
  • 【新手教程】在Godot引擎中制作真实时钟 — 我们将从头到尾制作一个显示本地时间的时钟。如果你希望更熟悉Godot Engine和GDScript,这是一个不错的起点。
  • 如何在场景之间切换? — 什么是场景 场景在Godot中是游戏的独立元素:关卡、菜单、设置窗口等。 每个场景由节点和资源组成。要让玩家从一种游戏状态切换到另一种状态,需要在场景之间进行切…
  • 为玩家提供最简单的亮度调节方式 — 最简单的亮度调整方法 :3 主要指南 1. 在所需位置创建一个Hslider节点。将通过它进行调节。
  • Godot 中的 Switch-Case(Match) — 我将为你介绍如何在游戏脚本中使用Switch-Case语句 1. 什么是Switch-Case "switch"结构可以替代多个if语句。它是一种更直观的方式,…
  • 如何导出为单个Windows可执行文件 — 如何导出为单个Windows可执行文件(.exe) Godot 4.0及以上版本 1. 打开项目 2. 在应用程序功能区中选择【项目】 3. 选择【导出...】…
  • Godot引擎2.2+中使用KinematicBody2D的玩家控制器 — 展示使用运动学体2D(KinematicBody2D)的简单玩家控制器。