我希望能夠通過在這個 JSON 檔案中分配的數字來呼叫特定的短語,因此當我呼叫 go(1) 時,它只顯示“Num”設定為 1 的文本。
我的 JSON 檔案:
[
{"Num":0, "Name":"Afely", "Emotion":"Neutral", "Text":"TEST1"},
{"Num":0, "Name":"Afely", "Emotion":"Neutral", "Text":"TEST2"},
{"Num":0, "Name":"Afely", "Emotion":"Neutral", "Text":"TEST3"},
{"Num":1, "Name":"Afely", "Emotion":"Neutral", "Text":"2TEST1"}
]
文本框代碼:
extends ColorRect
export var dialogPath = ""
export(float) var textSpeed = 0.005
var dialog
var phraseNum = 0
var finished = false
func go(phraseNum):
$Timer.wait_time = textSpeed
dialog = getDialog()
assert(dialog, "Dialog not found")
nextPhrase()
var f = File.new()
var img = dialog[phraseNum]["Emotion"] ".png"
$Portrait.texture = load(img)
func _unhandled_input(event):
if event is InputEventKey:
if event.pressed and event.scancode == KEY_Q:
if finished:
$NEXT.play()
nextPhrase()
else:
$Text.visible_characters = len($Text.text)
func getDialog() -> Array:
var f = File.new()
assert(f.file_exists(dialogPath), "File path does not exist")
f.open(dialogPath, File.READ)
var json = f.get_as_text()
var output = parse_json(json)
if typeof(output) == TYPE_ARRAY:
return output
else:
return []
func nextPhrase() -> void:
if phraseNum >= len(dialog):
queue_free()
return
finished = false
$Name.bbcode_text = dialog[phraseNum]["Name"]
$Text.bbcode_text = dialog[phraseNum]["Text"]
$Text.visible_characters = 0
while $Text.visible_characters < len($Text.text):
$Text.visible_characters = 1
$TEXT_AUDIO.play()
$Timer.start()
yield($Timer, "timeout")
finished = true
phraseNum = 1
return
我怎么稱呼它:
$TextBox.show()
$TextBox.go(1)
最后,我遵循的教程:https : //www.youtube.com/watch?v=GzPvN5wsp7Y
我將如何做這件事?
uj5u.com熱心網友回復:
你問的
你所問的需要相當多的額外作業。讓我們從 JSON 檔案開始:
[
{"Num":0, "Name":"Afely", "Emotion":"Neutral", "Text":"TEST1"},
{"Num":0, "Name":"Afely", "Emotion":"Neutral", "Text":"TEST2"},
{"Num":0, "Name":"Afely", "Emotion":"Neutral", "Text":"TEST3"},
{"Num":1, "Name":"Afely", "Emotion":"Neutral", "Text":"2TEST1"}
]
這將決議為Array([和之間的所有內容]),其中每個元素都是 a Dictionary({和之間的元素})。這意味著您將需要遍歷Array,檢查每個Num.
在我們這樣做之前,我們需要承認我們將使用名稱phraseNum來表示兩件事:
- 中的索引
dialogArray - 期望值
Num
由于您使用的源材料,我們處于這種情況。他們有phraseNum一個引數(這里func go(phraseNum):)隱藏一個phraseNum欄位(這里var phraseNum = 0 )。
這阻礙了交流。如果我告訴你用 來做這件事phraseNum,那是哪一個?那勢必會給我們帶來麻煩。
我將進行重寫,go因此它使用Num代替 中的索引dialog Array,因此我將保留phraseNum中的索引dialog Array,并為 的引數使用不同的名稱go。
讓我們開始重寫go:
func go(num):
pass
現在,讓我們得到所有的對話:
func go(num):
dialog = getDialog()
我們將迭代它們。由于我需要一個 的索引phraseNum,我將使用索引進行迭代:
func go(num):
dialog = getDialog()
for index in dialog.size():
pass
我們需要檢查是否Num匹配。如果是,我們就得到了索引:
func go(num):
dialog = getDialog()
for index in dialog.size():
if num == dialog[index]["Num"]:
phraseNum = index
break
我們需要處理我們沒有找到它的情況。現在,嗯……源材料只有一個assert,我會保持這種方法。所以我們需要一種方法來知道代碼沒有找到它......
func go(num):
var found := false
dialog = getDialog()
for index in dialog.size():
if num == dialog[index]["Num"]:
phraseNum = index
found = true
break
assert(found, "Dialog not found")
接下來你打電話nextPhrase(),當然:
func go(num):
var found := false
dialog = getDialog()
for index in dialog.size():
if num == dialog[index]["Num"]:
phraseNum = index
found = true
break
assert(found, "Dialog not found")
nextPhrase()
然后一個 not used var f = File.new(),我就不添加了。
And you set the portrait texture. Sure:
func go(num):
var found := false
dialog = getDialog()
for index in dialog.size():
if num == dialog[index]["Num"]:
phraseNum = index
found = true
break
assert(found, "Dialog not found")
nextPhrase()
var img = dialog[phraseNum]["Emotion"] ".png"
$Portrait.texture = load(img)
And I had skipped over the timer thing, I'll insert it now:
func go(num):
var found := false
dialog = getDialog()
for index in dialog.size():
if num == dialog[index]["Num"]:
phraseNum = index
found = true
break
assert(found, "Dialog not found")
$Timer.wait_time = textSpeed
nextPhrase()
var img = dialog[phraseNum]["Emotion"] ".png"
$Portrait.texture = load(img)
Something else
Now, you said you want only the phrases with the given Num. This is open to interpretation.
To be clear, the code above will have the dialog start at the first instance of the Num you ask for. But it will not end - nor skip - when it finds a different Num. I don't know if you want that or not.
And we have a couple ways to do this. We could remember what was the num and check against it in nextPhrase. I really don't want to do that. So, I will give you an alternative approach: let us make a dialog array that only contains the elements we want.
It looks like this:
func go(num):
var every_dialog = getDialog()
dialog = []
for candidate in every_dialog:
if num == candidate["Num"]:
dialog.append(candidate)
assert(dialog, "Dialog not found")
phraseNum = 0
$Timer.wait_time = textSpeed
nextPhrase()
var img = dialog[phraseNum]["Emotion"] ".png"
$Portrait.texture = load(img)
Please notice that in this example we are not reading getDialog() to dialog. Instead we are building a dialog array that only contains the entries we want. And we do that by iterating the result of getDialog() (we add to the array with append).
This is a subtle change in the meaning of dialog, because it would no longer represent every entry form the JSON file. Instead it only represent the entries that will be displayed. Before those two things were the same, but they aren't anymore with this change.
What you didn't ask
The function
getDialogreads from the JSON file. And you would be doing that every time you callgo. You could instead do it once in_ready.了解每個變數代表什么以及讀取和寫入它們的位置很重要。上面我提到了
dialog對替代變化做有一個微妙的含義。您需要考慮到這一點才能進行此更改。我堅信
nextPhrase應該處理定時器和肖像。應該沒有必要設定那些來自go.我想讓你考慮這個替代的 JSON 檔案結構:
[ [ {"Name":"Afely", "Emotion":"Neutral", "Text":"TEST1"}, {"Name":"Afely", "Emotion":"Neutral", "Text":"TEST2"}, {"Name":"Afely", "Emotion":"Neutral", "Text":"TEST3"}, ], [ {"Name":"Afely", "Emotion":"Neutral", "Text":"2TEST1"} ] ]然后你會得到一個
ArrayofArrays,其中嵌套的每個元素Array都是一個Dictionary。然后您可以“簡單地”通過索引獲取嵌套陣列,而不必遍歷每個元素。您獲得的結構類似于 JSON 檔案的結構。這也意味著您將不得不改變使用它的方式。例如,代替
dialog[phraseNum]["Text"]它可能是dialog[num][phraseNum]["Text"].現在考慮源材料。在這種情況下,我們有一個節點,它既負責決議 JSON 又負責顯示字符對話。如果將它們彼此分開,修改此代碼會更容易。想必作者的意圖是不同的對話會有不同的 JSON 檔案,所以你會在必要的時候切換 JSON 檔案(這也解釋了為什么他們每次都讀取 JSON 檔案)。
但這不是你問的。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/387210.html
上一篇:如何將命名鍵分配給陣列?[復制]
