等待后恢复函数“beginRound()”,但脚本消失了。 Godot 等待错误

问题描述 投票:0回答:1

我遇到了一个错误,

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()
正常运行。顺便说一句,这个的输出仍然是

真实
回合结束了。
正确
回合结束了。

godot gdscript godot4
1个回答
0
投票

这是 Godot 4.2.2 版本的错误,已在 4.3 中修复。我不确定这是否是该网站可以接受的答案,但它是这个问题的唯一答案。

© www.soinside.com 2019 - 2024. All rights reserved.