Skip to content

自定义配置

KBEngine Nex 支持在 kbengine.xml 中通过 <customCfg> 段添加业务自定义配置,Python 层无需手动解析 XML,直接通过 API 读取。

配置定义

{项目资产库}/res/server/kbengine.xml<root> 节点下新增 <customCfg> 段:

xml
<root>
    ...
    <customCfg>
        <param name="battle.maxPlayers" type="int" desc="max players per battle">1000</param>
        <param name="battle.enableRank" type="bool" desc="enable battle rank">true</param>
        <param name="battle.speedScale" type="float" desc="battle speed scale">1.0</param>
        <param name="battle.welcome" type="string" desc="welcome text">hello</param>
        <param name="battle.dropRates" type="dict" desc="drop rate config">{"gold": 1.2, "item": 0.05}</param>
        <param name="battle.spawnPoints" type="list" desc="spawn point ids">[1, 2, 3]</param>
    </customCfg>
</root>

参数说明

属性说明
name配置键名,支持点号分隔的层级命名(如 battle.maxPlayers
type值类型,支持 int / bool / float / string / dict / list
desc配置说明注释
标签内容配置的默认值,需与 type 类型匹配

Python 层读取

通过 KBEngine.getCustomCfg() 读取配置,无需手动解析 XML:

python
# 读取单个配置,不存在时返回默认值
max_players = KBEngine.getCustomCfg("battle.maxPlayers", 500)

# bool 类型
enable_rank = KBEngine.getCustomCfg("battle.enableRank", False)

# float 类型
speed_scale = KBEngine.getCustomCfg("battle.speedScale", 1.0)

# string 类型
welcome = KBEngine.getCustomCfg("battle.welcome", "welcome")

# dict 类型
drop_rates = KBEngine.getCustomCfg("battle.dropRates", {})

# list 类型
spawn_points = KBEngine.getCustomCfg("battle.spawnPoints", [])
  • 第一个参数是配置键名,对应 XML 中的 name 属性
  • 第二个参数是默认值,当配置不存在时返回
  • 返回值类型自动匹配 XML 中声明的 type

使用场景

适合需要在运维层面灵活调整、不想改 Python 代码的配置项,例如:

  • 战斗数值(最大玩家数、速度倍率等)
  • 功能开关(排行榜开关、掉落开关等)
  • 运营活动参数(掉落倍率、活动时间等)
  • 调试参数