在我正在开发的游戏中,我使用一个数组来跟踪玩家所在公司的当前统计数据,并使用下面的函数来编辑数组。
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次,并无限向上计数)。类似地,如果任何项目都在“装备”数组中,则每当单击文本按钮时,它们的当前状态就会从玩家当前的状态中减去。“项”数组中的项没有任何效果。
以下代码是用于购买和装备/设备设备的窗口。
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“函数,用于装备和均衡器数组中的项目,但我不知道为什么。任何洞察力都将不胜感激!
发布于 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的函数中会导致其他问题)。
就像这样:
init python:
def intermediateFunc(funcToCall, obj, arg):
funcToCall(obj, arg)
renpy.restart_interaction()
screen myScreen:
imagebutton:
# ...
action Function(intermediateFunc, MyObj.DoSomething, obj, "an argument")https://stackoverflow.com/questions/59776715
复制相似问题