下面是我的代码:扩展CanvasLayer
export(String, FILE, "*.json") var d_file
var dialogue = []
func _ready():
start()
func start():
dialogue = load_dialogue()
$NinePatchRect/Name.text = dialogue[0]['name']
$NinePatchRect/Chat.text = dialogue[0]['text']
func load_dialogue():
var file = File.new()
if file.file_exists(d_file):
file.open(d_file, file.READ)
return parse_json(file.get_as_text())
,我要看的视频是这个:https://youtu.be/7CCofjq_dHM
我尝试了视频中解释的许多事情,但都不起作用。我希望它能够解析一个JSON文件,里面有文本,并为NPC制作对话。
那么,在这个Json数据上:
[ {"name": "Blacksmith", "text": "OH! A survivor! I'm not the only one here!"} {"name": "you", "text": "uh, yeah! I am."} {"name": "Blacksmith", "text": "ah yes, I forgot. I'm a blacksmith. Not a NPC or anything..."} {"name": "you", "text": "..."} {"name": "Blacksmith", "text": "Well, anyway, I'm here so you can get better guns, kill zombos to get gun parts and come back here!"} {"name": "you", "text": "Ok."} {"name": "Blacksmith", "text": "anyway, off you go, bye!"} {"name": "GAME", "text": "(you were kicked out.)"} ]
让我调整一下间距…
[
{"name": "Blacksmith", "text": "OH! A survivor! I'm not the only one here!"}
{"name": "you", "text": "uh, yeah! I am."}
{"name": "Blacksmith", "text": "ah yes, I forgot. I'm a blacksmith. Not a NPC or anything..."}
{"name": "you", "text": "..."}
{"name": "Blacksmith", "text": "Well, anyway, I'm here so you can get better guns, kill zombos to get gun parts and come back here!"}
{"name": "you", "text": "Ok."}
{"name": "Blacksmith", "text": "anyway, off you go, bye!"}
{"name": "GAME", "text": "(you were kicked out.)"}
]
可以创建一个以[
和]
为分隔符的数组。它有物体,用{
和}
划分…但是数组应该用逗号分隔。所以它应该是这样的:
[
{"name": "Blacksmith", "text": "OH! A survivor! I'm not the only one here!"},
{"name": "you", "text": "uh, yeah! I am."},
{"name": "Blacksmith", "text": "ah yes, I forgot. I'm a blacksmith. Not a NPC or anything..."},
{"name": "you", "text": "..."},
{"name": "Blacksmith", "text": "Well, anyway, I'm here so you can get better guns, kill zombos to get gun parts and come back here!"},
{"name": "you", "text": "Ok."},
{"name": "Blacksmith", "text": "anyway, off you go, bye!"},
{"name": "GAME", "text": "(you were kicked out.)"}
]
数组的元素(即对象)之间有逗号(",")