互相访问
玩法之间难免有功能耦合,系统间需要互相调用。
系统间调用,一般用到 GetSystem() 获取系统实例,但这个办法很长,而且 IDE 无法补全系统中的方法
于是诞生了 systems 和 system_accessor
情况
假设我们有一个 AServer ,我们需要动态调用它的 Test 方法

方案一
直接在公共模块中手写所有系统获取函数,
好处:一次手写,四处调用,并且从头到尾都有补全
坏处:手写太麻烦
python
# systems.py
# -*- coding: utf-8 -*-
if 1>2: # 骗IDE
from AClient import AClient
from AServer import AServer
from ..modConfig import ModNameSpace
from mcapi import serverApi, clientApi
_AServer = None
def getAServer(): # type: () -> AServer
global _AServer
if _AServer is None:
_AServer = serverApi.GetSystem(ModNameSpace, 'AServer')
return _AServer
"""
使用
from ..framework import systems
systems.getAServer().Test()
"""


方案二
是的,systems 还是有点太麻烦了。于是有了 system_accessor ,实现如下
python
# system_accessor.py
class SystemAccessor(object):
_instance = None
def __new__(cls): # 确保全局单例
if cls._instance is None:
cls._instance = super(SystemAccessor, cls).__new__(cls)
return cls._instance
def __getattr__(self, name):
cls_name = name
return GET_SIDE_API().GetSystem(ModNameSpace, cls_name)
sa = SystemAccessor()原理很简单,使用方式如下
python
from ..framework.system_accessor import sa
sa.AServer.Test() # 即可访问 Test 方法IDE 有补全

好处:可补全,无需手写
坏处:这里的 IDE 补全源于 IDE 自身的支持,并没有主动指定 type ,所以发挥可能不稳定, 实测 pycharm 社区版 2025 支持此功能, 只要后面跟的是个class,都可以补