【问题标题】:Calling a variable value from another script's class's functions从另一个脚本的类的函数中调用变量值
【发布时间】:2018-11-21 17:05:59
【问题描述】:

我需要帮助!我正在尝试修改游戏 BombSquad 并尝试为我的服务器构建一个禁止系统。

好的,所以有一个拒绝人们进入的功能

def onPlayerRequest(self, player):
    """
    Called when a new bs.Player wants to join;
    should return True or False to accept/reject.
    """
    # limit player counts based on pro purchase/etc *unless* we're in a stress test
    if bsUtils._gStressTestResetTimer is None:
        if len(self.players) >= self._maxPlayers:
            # print a rejection message *only* to the client trying to joinz
            # (prevents spamming everyone else in the game)
                bs.playSound(bs.getSound('error'))
                bs.screenMessage(bs.Lstr(resource='playerLimitReachedText', subs=[('${COUNT}', bsInternal._getAccountDisplayString())]),
                            color=(0.8, 0.0, 0.0),
                            clients=[player.getInputDevice().getClientID()],
                            transient=True)
                return False
    bs.playSound(bs.getSound('dripity'))
    return True

我需要另一个文件中的播放器名称,我将它放在 if 语句中以检查它是否与禁用列表匹配并完成! 带出播放器名称的文件是

class DamnPartyWindow(PartyWindow):

def _onPartyMemberPress(self, clientID, isHost, widget):
    # if we're the host, pop up 'kick' options for all non-host members
    if bsInternal._getForegroundHostSession() is not None:
        kickStr = bs.Lstr(resource='kickText')

    else:
        # kick-votes appeared in build 14248
            if bsInternal._getConnectionToHostInfo().get('buildNumber', 0) < 14248:
                return
    kickStr = bs.Lstr(resource='kickVoteText')
    for rst in self._roster:
            cid = rst['clientID']
            if cid == clientID:
                bs.screenMessage(rst['displayString'])
                break
    p = PopupMenuWindow(position=widget.getScreenSpaceCenter(),
                scale=2.3 if gSmallUI else 1.65 if gMedUI else 1.23,
                choices=['kick'],
                choicesDisplay=[kickStr],
                currentChoice='kick',
                delegate=self).getRootWidget()
    self._popupPartyMemberClientID = clientID
    self._popupPartyMemberIsHost = isHost

(这是另一个人的模组)我需要 rst['displayString'] 的值并在第一个示例中检查它...帮助!

我尝试将 xyz = rst['displayString'] 然后放入 file1 使用

从文件 2 导入 xyz

文件 1:

class DamnPartyWindow(PartyWindow):

def _onPartyMemberPress(self, clientID, isHost, widget):
    # if we're the host, pop up 'kick' options for all non-host members
    if bsInternal._getForegroundHostSession() is not None:
        kickStr = bs.Lstr(resource='kickText')

    else:
        # kick-votes appeared in build 14248
            if bsInternal._getConnectionToHostInfo().get('buildNumber', 0) < 14248:
                return
    kickStr = bs.Lstr(resource='kickVoteText')
    for rst in self._roster:
            cid = rst['clientID']
            xyz = rst['displayString']
            if cid == clientID:
                bs.screenMessage(rst['displayString'])
                break
    p = PopupMenuWindow(position=widget.getScreenSpaceCenter(),
                scale=2.3 if gSmallUI else 1.65 if gMedUI else 1.23,
                choices=['kick'],
                choicesDisplay=[kickStr],
                currentChoice='kick',
                delegate=self).getRootWidget()
    self._popupPartyMemberClientID = clientID
    self._popupPartyMemberIsHost = isHost

文件 2:

def onPlayerRequest(self, player):

    """
    Called when a new bs.Player wants to join;
    should return True or False to accept/reject.
    """
    # importing xyz i.e the value of client
    from file2 import xyz
    if bsUtils._gStressTestResetTimer is None:
        if len(self.players) >= self._maxPlayers or xyz=="PC14567":
            # print a rejection message *only* to the client trying to joinz
            # (prevents spamming everyone else in the game)
                bs.playSound(bs.getSound('error'))
                bs.screenMessage(bs.Lstr(resource='playerLimitReachedText', subs=[('${COUNT}', bsInternal._getAccountDisplayString())]),
                            color=(0.8, 0.0, 0.0),
                            clients=[player.getInputDevice().getClientID()],
                            transient=True)
                return False
    bs.playSound(bs.getSound('dripity'))
    return True

但是说不能导入 xyz 是行不通的。 请帮忙!我可以提供你们需要的任何信息,请帮助我!

【问题讨论】:

  • 您能否添加触发错误的确切行和实际错误消息,而不仅仅是 xyzblah ?我以这种方式更容易理解和帮助。意思是,您将 xyz = .... 放在哪里以及尝试导入到哪里?在你的代码示例中展示它
  • @Andrew_Lvov 哦,是的,我真的很抱歉!我会做的

标签: python function class variables


【解决方案1】:

因此,首先,您的变量xyz 在函数内部声明,因此在函数外部不可用(不存在)。这就是你不能导入它的原因。

第二:跨模块共享全局变量被认为是一种非常糟糕的做法,因为跟踪它的值变化可能会变得非常复杂。

作为一种解决方案,您可以:

  1. xyz值存储为DamnPartyWindow类的属性,所以 每当您拥有此类的实例(对象)时,您都可以获得 这个属性。
  2. 定义将存储“系统 状态”,包括您需要的价值。
  3. 看起来这是某种 UI 框架,所以应该有一种方法可以在组件之间发送带值的事件。这样你就可以通过了。

【讨论】:

  • @Andre_Lvov 你能帮我创建类的属性吗?我将 xyz 更改为全局变量并在函数外部但在类内部创建了一个变量 xyz2 并使用了 DamnPartyWindow.xyz2 但它说 xyz 即使是全局变量也没有定义
【解决方案2】:

尝试: 导入文件1

然后使用file1.xyz

【讨论】:

  • 实际上 xyz 是类内部函数中的一个变量,所以 file1.xyz 会导入一个函数还是什么?无论如何,我试过了,它说: AttributeError : 'module' object has no attribute 'xyz'
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-01-08
  • 2017-11-09
  • 2018-04-01
  • 2018-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多