KBE實踐——登錄案例


 

目錄

服務器

 ```

  void maini(){

    printf("hello world");

  }

```

  1. 最小資產庫創建

  2. entity配置

  3. 實體的Python實現

  4. 創建第一個空間Space

  5. 讓entity進入空間Space

客戶端(unity)

  1. 生成客戶端SDK

  2. 實現Client部分驗證

驗證測試

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

服務器

  1. 最小資產庫創建

    • 運行文件“new_assets.bat”,生成最小資產庫“server_assets”,更名為“first_assets"
  2. 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>
        {項目資產庫}\scripts\entity_defs\PlayerEntity.def
  3. entity的 Python實現

    • 名稱規則(實體名.py)
    • base文件夾下的部分
      # -*- coding utf-8 -*-
      import KBEngine
      from KBEDebug import *
      
      class FirstPlayer(KBEngine.Proxy):
          #base部分的實現
          def __init__(self):
              KBEngine.Entity.__init__(self)
      {資產庫}\base\FirstPlayer.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)
      {資產庫}\scripts\cell\FirstPlayer.py
    • entity何時創建?
      教程采用:使用賬戶入口對第一個enetity進行創建,只添加一句:<accountEntityScriptType> FirstEntity </accountEntityScriptType>
          <dbmgr>
              <account_system>
                  <accountEntityScriptType> PlayerEntity </accountEntityScriptType>
      {項目資產庫}/res/server/kbengine.xml
  4. Space配置

    • entity聲明和def文件配置
      <root>
          <PlayerEntity hasClient="true"></PlayerEntity>
          <!-- 添加下面這行 -->
          <WorldSpace></WorldSpace>
      </root>
      {資產庫}\scripts\entities.xml
    • <root>
          <!-- BaseApp上的遠程方法 -->
          <BaseMethods>
          </BaseMethods>
          <!-- CellApp上的遠程方法 -->
          <CellMethods>
          </CellMethods>
      </root>
      {資產庫}\scripts\entity_defs\WorldSpace.def
  5. 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
      {資產庫}\scripts\base\WorldSpace.py
    • cell部分
      import KBEngine
      
      
      class WorldSpace(KBEngine.Space):
          def __init__(self):
              KBEngine.Space.__init__(self)
              pass
      {資產庫}\scripts\cell\WoldSpace.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", {})
      {資產庫}\scripts\base\kbemain.py
  6. 驗證空間Space是否加載成功

    1. 開始驗證:打開guiconsole(......\kbengine-2.4.\kbe\tools\server\guiconsole)選擇baseapp,然后選中debug便簽,輸入Python語句:KBEngine.entities.items(),Ctrl+Enter執行語句,輸出結果如下:

       

    2. 錯誤警告

      我們可能需要設置一下用戶賬戶和密碼,位置在{資產庫}res\server\kbengine.xml: root -> dbmgr -> databaseInterfaces -> default -> auth

    3. 注意2(9個窗口一個都不能少)
      折疊的代碼中所有的實例名稱(PlayerEntity 或者 FIrstPlayer 選一個統一)都需要統一,我上面沒有再修改,吐槽博客園的編輯器   (¬︿¬☆)

  7. 讓entity進入空間Space

    1. 我們再次打開之前的文件({項目資產庫}/scripts/base/PlayerEntity.py)
    2. 添加一個回調和函數,onClientEnabled ,因為我們的PlayerEntity繼承了Proxy,所以當客戶端可用時,這個函數會被執行,此時我們選擇讓entity進入空間


客戶端(unity)

  1. 生成客戶端SDK

    1. 配置SDK生成工具

  2. 實現Client部分驗證

驗證測試


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM