await
我遇到了在循环内多次触发的错误。
代码应该循环遍历所有帝国,告诉他们轮流行动,然后await
在触发下一个帝国之前完成任务。这适用于第一轮(玩家的轮次),但当 AI 尝试调用时endTurn()
,我收到错误:
E 0:01:08:0911 Map.gd:419 @ endTurn(): Resumed function 'beginRound()' after await, but script is gone.
At script: res://scripts/HostGameMenu.gd:197
<C++ Error> Method/function failed. Returning: Variant()
<C++ Source> modules/gdscript/gdscript_function.cpp:197 @ resume()
<Stack Trace> Map.gd:419 @ endTurn()
Tribe.gd:221 @ takeTurn()
Map.gd:414 @ beginNewTurn()
HostGameMenu.gd:196 @ beginRound()
Map.gd:419 @ endTurn()
Map.gd:1912 @ endEmpireTurn()
脚本一功能:
func beginRound():
for i in range(empireTurnOrder.size()):
empireTurnIndex = i #the only purpose this serves is being able to easily access whose turn it is for debugging from outside the for loop
gameMap.beginNewTurn.rpc(empireTurnOrder[empireTurnIndex]) #empireTurnOrder is a PackedStringArray
await gameMap.beginNextTurn
gameMap.endRound.rpc() #gameMap points to the node containing Script Two
脚本二功能:
signal beginNextTurn
@rpc("any_peer", "call_local", "reliable")
func beginNewTurn(nextEmpire: String) -> void:
var empire = getEmpire(nextEmpire)
if empire.playerId != -1 and empire.playerId != userEmpire.playerId: #check is only for multiplayer, which is not the case for my tests. userEmpire is a reference to the empire node (node of Script Three) that the player controls
pass
else:
if empire.playerId == userEmpire.playerId:
#UI edits, irrelevant
empire.takeTurn() #empire points to the node(s) of Script Three
func endTurn():
if not serverHandler.inServer or multiplayer.get_unique_id() == 1: #serverHandler points to the node of Script One. inServer will always be true for singleplayer, which I am testing
print(is_instance_valid(serverHandler))
emit_signal("beginNextTurn")
func endRound():
if not serverHandler.inServer or multiplayer.get_unique_id() == 1:
serverHandler.beginRound()
func endEmpireTurn(): #button is tied to this function
#edits UI a little
endTurn()
脚本三功能:
func takeTurn():
if playerId == -1:
map.endTurn() #map points the node of Script Two
else:
#does nothing; button press will call endTurn for players
打印输出:
真
真
任何帮助都非常感谢。我发现关于此错误的唯一其他帖子是人们在等待_process()
函数中的计时器,而这些问题的答案并不完全适用于我的情况。
编辑:
我在脚本一中添加了一个函数:
func turnEnded() -> void:
print(empireTurnOrder[empireTurnIndex] + "'s turn is over.")
并将beginNextTurn
信号连接到它,并且它也成功/正确地打印了信号的两次发射,但错误没有改变。
编辑2:
出于好奇想看看它是否有效,我更改了两个函数,beginNewTurn 和 beginRound,如下所示:
@rpc("any_peer", "call_local", "reliable")
func beginNewTurn(nextEmpire: String) -> bool:
var empire = getEmpire(nextEmpire)
if empire.playerId != -1 and empire.playerId != userEmpire.playerId:
pass
else:
if empire.playerId == userEmpire.playerId:
loadedData.get_node("ImportantMenuHub/EndTurn").set_disabled(false)
loadedData.get_node("ResourceHUD/RaiseAll").set_disabled(false)
empire.takeTurn()
await beginNextTurn
return true
func beginRound():
for i in range(empireTurnOrder.size()):
empireTurnIndex = i
await gameMap.beginNewTurn(empireTurnOrder[empireTurnIndex])
gameMap.endRound.rpc()
这会在行上给出错误“参数“函数”为空” emit_signal("beginNextTurn")
。现在,显然,我确实希望我的解决方案与 rpc 函数调用兼容,但非常奇怪的是信号拒绝与beginRound()
和 一起正确运行beginNewTurn()
。顺便说一句,此输出仍然是
真
<帝国 1> 的回合结束了。
真
<帝国 2> 的回合结束了。
这是 Godot 4.2.2 版的一个错误,已在 4.3 版中修复。我不确定这是否是该网站可接受的答案,但这是针对这个问题的唯一答案。