从另一个脚本的类函数中调用变量值

时间:2018-06-12 10:21:36

标签: python function class variables

我需要帮助!我试图修改一个游戏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语句中以检查它是否与banlist匹配并完成! 带出玩家名称的文件是

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

(这是另一个人的mod)我需要rst ['displayString']的值并在第一个样本中检查...帮助!

我尝试将xyz = rst ['displayString']放在文件1中,然后使用

来自file2 import 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。 请帮忙!我可以提供你们需要的任何信息,请帮助我!

3 个答案:

答案 0 :(得分:0)

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

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

作为解决方案,您可以:

  1. xyz值存储为DamnPartyWindow类的属性,所以 只要你有这个类的实例(对象),你就可以得到 这个属性。
  2. 定义一个存储“系统”的公共服务 陈述“包括你需要的价值。”
  3. 看起来这是某种UI框架,因此应该有一种方法来在具有值的组件之间发送事件。所以你可以通过它。

答案 1 :(得分:0)

您不必使用该变量,只需使用

bsInternal._getGameRoster()['displayString']

答案 2 :(得分:-1)

尝试: import file1

然后使用file1.xyz