KBE实践——登录案例

目录

服务器

 “`

  void maini(){

    printf(“hello world”);

  }

“`

最小资产库创建

entity配置

实体的Python实现

创建第一个空间Space

让entity进入空间Space

客户端(unity)

生成客户端SDK

实现Client部分验证

验证测试

====================================正文=====================================

服务器

最小资产库创建

运行文件“new_assets.bat”,生成最小资产库“server_assets”,更名为“first_assets”

entity配置

entity声明
“`
void main(){
  printf(“shit”);
}
“`

hasClient 表示这个实体有客户端

entity配置

新建def配置文件,配置文件名称规定为(实体名称.def)
添加方法声明

<root>
  <!-- Cell上的方法 -->
  <CellMethods>
    <Broadcast>
      <!--Exposed 表示此方法暴漏给客户端调用-->
      <Exposed/>
      <Arg> UNICODE </Arg>
    </Broadcast>
  </CellMethods>

  <!-- 客户端上的方法 -->
  <ClientrMethods>
    <onEnter>
    </onEnter>

    <SendMsgToServer>
      <Arg> UNICODE </Arg>
    </SendMsgToServer>
  </ClientrMethods>
</root>

{项目资产库}scriptsentity_defsPlayerEntity.def

entity的 Python实现

名称规则(实体名.py)
base文件夹下的部分

# -*- coding utf-8 -*-
import KBEngine
from KBEDebug import *

class FirstPlayer(KBEngine.Proxy):
    #base部分的实现
    def __init__(self):
        KBEngine.Entity.__init__(self)

{资产库}aseFirstPlayer.py

cell文件夹下的部分

# -*- coding: utf-8 -*-
import KBEngine
from KBEDebug import *


class FirstEntity(KBEngine.Entity):
    def __init__(self):
        KBEngine.Entity.__init__(self)
        #这里是FirstEntity的cell,当其Cell创建完毕时,通知其自身的客户端的onEnter函数
        self.client.onEnter()

    def SendMsgToServer(self, callerID, content):
        INFO_MSG("FirstPlay-SendMsgToServer")
        #广播给所有客户端
        self.allClient.Broadcast(str(self.id) + ":" + content)

{资产库}scriptscellFirstPlayer.py

entity何时创建?
教程采用:使用账户入口对第一个enetity进行创建,只添加一句:<accountEntityScriptType> FirstEntity </accountEntityScriptType>

    <dbmgr>
        <account_system>
            <accountEntityScriptType> PlayerEntity </accountEntityScriptType>

{项目资产库}/res/server/kbengine.xml

Space配置

entity声明和def文件配置

<root>
    <PlayerEntity hasClient="true"></PlayerEntity>
    <!-- 添加下面这行 -->
    <WorldSpace></WorldSpace>
</root>

{资产库}scriptsentities.xml

<root>
    <!-- BaseApp上的远程方法 -->
    <BaseMethods>
    </BaseMethods>
    <!-- CellApp上的远程方法 -->
    <CellMethods>
    </CellMethods>
</root>

{资产库}scriptsentity_defsWorldSpace.def

Space的 Python实现

名称规则(空间名.py)
base部分

# -*- coding-utf8 -*-
import KBEngine
from KBEDebug import *


class WorldSpace(KBEngine.Space):
    # WorldSpace的base部分,这是一个实体,并不是Space本身,Space位于内存中,我们通过这个实体关联并控制Space
    def __init__(self):
        KBEngine.Space.__init__(self)
        # 在全局变量globaldata字典中保存Space
        KBEngine.globalData["WorldSpace"] = self

{资产库}scriptsaseWorldSpace.py

cell部分

import KBEngine


class WorldSpace(KBEngine.Space):
    def __init__(self):
        KBEngine.Space.__init__(self)
        pass

{资产库}scriptscellWoldSpace.py

space何时创建
教程采用:Baseapp就绪时创建Space空间,在baseapp就绪后加载空间space

def onBaseAppReady(isBootstrap):
    """
    KBEngine method.
    baseapp已经准备好了
    @param isBootstrap: 是否为第一个启动的baseapp
    @type isBootstrap: BOOL
    """
    INFO_MSG('onBaseAppReady: isBootstrap=%s, appID=%s, bootstrapGroupIndex=%s, bootstrapGlobalIndex=%s' % 
     (isBootstrap, os.getenv("KBE_COMPONENTID"), os.getenv("KBE_BOOTIDX_GROUP"), os.getenv("KBE_BOOTIDX_GLOBAL")))
    #添加此句以使得space在baseapp启动后就加载进来
    KBEngine.createEntityLocally("WorldSpace", {})

{资产库}scriptsasekbemain.py

验证空间Space是否加载成功

开始验证:打开guiconsole(……kbengine-2.4.kbe oolsserverguiconsole)选择baseapp,然后选中debug便签,输入Python语句:KBEngine.entities.items(),Ctrl+Enter执行语句,输出结果如下:

错误警告

我们可能需要设置一下用户账户和密码,位置在{资产库}resserverkbengine.xml: root -> dbmgr -> databaseInterfaces -> default -> auth

注意2(9个窗口一个都不能少)
折叠的代码中所有的实例名称(PlayerEntity 或者 FIrstPlayer 选一个统一)都需要统一,我上面没有再修改,吐槽博客园的编辑器   (¬︿¬☆)

让entity进入空间Space

我们再次打开之前的文件({项目资产库}/scripts/base/PlayerEntity.py)
添加一个回调和函数,onClientEnabled ,因为我们的PlayerEntity继承了Proxy,所以当客户端可用时,这个函数会被执行,此时我们选择让entity进入空间

客户端(unity)

生成客户端SDK

配置SDK生成工具

实现Client部分验证

验证测试

Published by

风君子

独自遨游何稽首 揭天掀地慰生平

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注