
本指南包含一些代码,可能有助于加载某些无法被引擎反序列化为正确值的Json数据。 问题说明: Godot无法直接将从Json获取的值解析为Vector3和颜色等类型的值,因此在尝试执行此操作时会返回如下错误:

The reason why that error happens, is because, as the error text says, it's interpreting the value as a String. I had managed to make some codes which will allow you to convert such texts to a object of the value type you want to take the data of, if I replace "var vec32 : Vector3 = Data["Vector3"]" for "var vec32 : Vector3 = ConvertStringToVector3(Data["Vector3"])", the method will extract the Vector3 value from the Data dictionary String, and I wont need to split such information into 3 different saved and loaded entries. Bellow I'll make available a number of codes I made that will allow you to get the desired data type values from Json. Feel free to take whatever codes you need. The actual method to solve this: use "var_to_str" method to convert when saving the variable value, and use "str_to_var" method to turn the text of the variable into its value. Saving var Save = { "Name" = name, "Position" = var_to_str(position), "Rotation" = var_to_str(rotation) } Loading name = Save["Name"] position = str_to_var(Save["Position"]) rotation = str_to_var(Save["Rotation"]) Thank jagholin for this info. The parsing codes. The name of the codes are self explanatory. Just place them on wherever you can call them, and use them whenever you want to extract the desired information from the array you loaded. func ConvertStringToVector2i(Text: String) -> Vector2i: var CommaPosition = Text.find(",") var Position = Vector2i(int(Text.substr(1, CommaPosition - 1)), int(Text.substr(CommaPosition + 1, Text.length() - 1 - CommaPosition))) return Position func ConvertStringToVector2(Text: String) -> Vector2: var CommaPosition = Text.find(",") var Position = Vector2(float(Text.substr(1, CommaPosition - 1)), float(Text.substr(CommaPosition + 1, Text.length() - 1 - CommaPosition))) return Position func ConvertStringToColor(Text: String) -> Color: var Comma1 = Text.find(",") var Comma2 = Text.find(",", Comma1 + 1) var Comma3 = Text.find(",", Comma2 + 1) var color = Color(float(Text.substr(1, Comma1 - 1)), float(Text.substr(Comma1 + 1, Comma2 - Comma1 - 1)), float(Text.substr(Comma2 + 1, Comma3 - Comma2 - 1)), float(Text.substr(Comma3 + 1, Text.length() - Comma2 - 1))) return color func ConvertStringToVector3(Text: String) -> Vector3: var Comma1 = Text.find(",") var Comma2 = Text.find(",", Comma1 + 1) var vec3 = Vector3(float(Text.substr(1, Comma1 - 1)), float(Text.substr(Comma1 + 1, Comma2 - Comma1 - 1)), float(Text.substr(Comma2 + 1, Text.length() - Comma2 - 1))) return vec3 func ConvertStringToVector3i(Text: String) -> Vector3i: var Comma1 = Text.find(",") var Comma2 = Text.find(",", Comma1 + 1) var vec3 = Vector3i(int(Text.substr(1, Comma1 - 1)), int(Text.substr(Comma1 + 1, Comma2 - Comma1 - 1)), int(Text.substr(Comma2 + 1, Text.length() - Comma2 - 1))) return vec3 func ConvertStringToVector4(Text: String) -> Vector4: var Comma1 = Text.find(",") var Comma2 = Text.find(",", Comma1 + 1) var Comma3 = Text.find(",", Comma2 + 1) var vec4 = Vector4(float(Text.substr(1, Comma1 - 1)), float(Text.substr(Comma1 + 1, Comma2 - Comma1 - 1)), float(Text.substr(Comma2 + 1, Comma3 - Comma2 - 1)), float(Text.substr(Comma3 + 1, Text.length() - Comma2 - 1))) return vec4 func ConvertStringToVector4i(Text: String) -> Vector4i: var Comma1 = Text.find(",") var Comma2 = Text.find(",", Comma1 + 1) var Comma3 = Text.find(",", Comma2 + 1) var vec4 = Vector4i(int(Text.substr(1, Comma1 - 1)), int(Text.substr(Comma1 + 1, Comma2 - Comma1 - 1)), int(Text.substr(Comma2 + 1, Comma3 - Comma2 - 1)), int(Text.substr(Comma3 + 1, Text.length() - Comma2 - 1))) return vec4 func ConvertStringToPackedColorArray(Text: String) -> PackedColorArray: var Colors = PackedColorArray() var ColorIndex = 0 var NewColor : Color = Color.WHITE var Begun = false var CaughtString = "" for c in Text: var ParseNumber = false if (c == '('): Begun = true NewColor = Color.WHITE ColorIndex = 0 CaughtString = "" elif (c == ' '): continue elif (c == ','): ParseNumber = true elif (c == ')'): Begun = false ParseNumber = true else: CaughtString += c if (ParseNumber): var NewNumber = float(CaughtString) match(ColorIndex): 0: NewColor.r = NewNumber ColorIndex += 1 1: NewColor.g = NewNumber ColorIndex += 1 2: NewColor.b = NewNumber ColorIndex += 1 3: NewColor.a = NewNumber Colors.append(NewColor) NewColor = Color.WHITE ColorIndex = 0 CaughtString = "" return Colors Outro That's about it. Again, Thanks to jagholin for the info about the methods to convert the variable to string and vice versa, or else I wouldn't have known and shared this. If my guide helped you, it's not necessary to do so, but you may hide somewhere in your game project a raccoon face (or just thank Nakano15 in the credits). Now go on, and finish your masterpiece :).
2026-02-13 16:00:28 发布在
Godot Engine
说点好听的...
收藏
0
0
