首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python (RenPy):执行函数的time按钮不是每次按下都会显式调用

Python (RenPy):执行函数的time按钮不是每次按下都会显式调用
EN

Stack Overflow用户
提问于 2020-01-16 19:42:18
回答 1查看 1.9K关注 0票数 0

在我正在开发的游戏中,我使用一个数组来跟踪玩家所在公司的当前统计数据,并使用下面的函数来编辑数组。

代码语言:javascript
复制
init python:

#The following store item objects, which include an array of their own stats
#Stores currently owned equipment
   Equipment = []
#Stores items available to buy
   Items = []
#Stores currently equipped equipment
   Equipped = []
#The company's current stats   
   SecArray = [0, 0, 0, 0, 0, 0]

#Called whenever moving something in or out of the currently equipped items.
#Pass the current item's stat array, the stat array, and a + or - symbol
   def arrayedit(array, SecArray, symbol):
        Notify("The stats have changed")
        if symbol == "+":
            SecArray[0] += array[0]
            SecArray[1] += array[1]
            SecArray[2] += array[2]
            SecArray[3] += array[3]
            SecArray[4] += array[4]
            SecArray[5] += array[5]
        if symbol == "-":
            SecArray[0] -= array[0]
            SecArray[1] -= array[1]
            SecArray[2] -= array[2]
            SecArray[3] -= array[3]
            SecArray[4] -= array[4]
            SecArray[5] -= array[5]
        return()

但是,如果“设备”数组中有任何项,则每次单击文本按钮时,都会将它们的状态添加到当前状态中(例如,如果某项在stat中有3项,则每当单击任意按钮时,玩家的当前状态将增加3次,并无限向上计数)。类似地,如果任何项目都在“装备”数组中,则每当单击文本按钮时,它们的当前状态就会从玩家当前的状态中减去。“项”数组中的项没有任何效果。

以下代码是用于购买和装备/设备设备的窗口。

代码语言:javascript
复制
screen shopping1():

    frame:
        xpos (config.screen_width*25/64) ypos (config.screen_height*11/64)
        ysize (config.screen_height*31/64)
        xsize (config.screen_width*36/64)

        has side "c r b"

        viewport:
            yadjustment tutorials_adjustment
            mousewheel True

            vbox:
              xpos (config.screen_width*2/5) ypos (config.screen_height*3/16)
              ysize (config.screen_height/2)
              xsize (config.screen_width/2)
              for i in Items:
                    if i.kind == "Item":
                      if i.cost <= Money:
                          textbutton "[i.title]   $[i.cost]":
                            action [AddToSet(Equipment, i), RemoveFromSet(Items, i), Hide("shopping1"), Return(i.cost)]
                            left_padding 20
                            xfill True
                            hovered Notify(i.hover)

                    else:
                        null height 10
                        text i.title alt ""
                        null height 5

              for i in Policies:
                    if i.kind == "Policy":
                      if i.cost <= Money:
                          textbutton "[i.title]   $[i.cost]":
                            action [AddToSet(OwnedPolicies, i), RemoveFromSet(Policies, i), Hide("shopping1"), Return(i.cost)]
                            left_padding 20
                            xfill True
                            hovered Notify(i.hover)                           

                    else:
                        null height 10
                        text i.title alt ""
                        null height 5

              for i in Trainings:
                    if i.kind == "Training":
                      if i.cost <= Money:
                          textbutton "[i.title]   $[i.cost]":
                            action [AddToSet(OwnedTrainings, i), RemoveFromSet(Trainings, i), Hide("shopping1"), Return(i.cost)]
                            left_padding 20
                            xfill True
                            hovered Notify(i.hover)

                    else:
                        null height 10
                        text i.title alt ""
                        null height 5

        bar adjustment tutorials_adjustment style "vscrollbar"

        textbutton _("Return"):
            xfill True
            action [Hide("shopping1")]
            top_margin 10

screen equipmentedit():

    frame:
        xpos (config.screen_width*5/128)  ypos (config.screen_height*2/64)
        ysize (config.screen_height*47/64)
        xsize (config.screen_width*19/64)

        has side "c r b"

        viewport:
            yadjustment tutorials_adjustment
            mousewheel True

            vbox:
              null height 10
              text "Unequipped Items" alt ""
              null height 5
              for i in Equipment:

                    if i.kind == "Item":

                        textbutton "[i.title]   $[i.cost]":
                            action [arrayedit(i.stats, SecArray, "+"), AddToSet(Equipped, i), RemoveFromSet(Equipment, i), Hide("equipmentedit"), Return(i.cost)]
                            left_padding 20
                            xfill True

                    else:

                        null height 10
                        text i.title alt ""
                        null height 5
              null height 10
              text "Equipped Items" alt ""
              null height 5
              for i in Equipped:

                    if i.kind == "Item":

                        textbutton "[i.title]   $[i.cost]":
                            action [arrayedit(i.stats, SecArray, "-"), AddToSet(Equipment, i), RemoveFromSet(Equipped, i), Hide("equipmentedit"), Return(i.cost)]
                            left_padding 20
                            xfill True

                    else:

                        null height 10
                        text i.title alt ""
                        null height 5




        bar adjustment tutorials_adjustment style "vscrollbar"

        textbutton _("Return"):
            xfill True
            action [Hide("equipmentedit")]
            top_margin 10

除此之外,使用的数组和函数不会在程序的其他地方调用或引用。我相信,每次单击按钮,包括返回按钮时,都会调用"arrayedit“函数,用于装备和均衡器数组中的项目,但我不知道为什么。任何洞察力都将不胜感激!

EN

回答 1

Stack Overflow用户

发布于 2022-12-04 14:17:42

我也遇到了同样的问题,我相信你正在成为Renpy预测的牺牲品,正如GitHub:https://github.com/renpy/renpy/issues/3718上的这个帖子所证明的那样。

Renpy在屏幕上显示(和多次)时会遍历所有代码,包括搜索任何函数调用以预加载资产。通常,这不会造成问题,因为大多数Renpy调用都使用renpy.restart_interaction(),它会将任何更改恢复到变量。不幸的是,当直接调用Python对象时,它可能会遇到困难。

我使用的解决方案是让screen元素调用一个包含renpy.restart_interaction()调用的中间函数(由于某种原因,将重启直接放入我的Python的函数中会导致其他问题)。

就像这样:

代码语言:javascript
复制
init python:
    def intermediateFunc(funcToCall, obj, arg):
        funcToCall(obj, arg)
        renpy.restart_interaction()

screen myScreen:
    imagebutton:
        # ...
        action Function(intermediateFunc, MyObj.DoSomething, obj, "an argument")
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59776715

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档