Skip to content

KBEngine Module

The KBEngine module provides logic script layer access to entities, as well as data for other clients in the current process, etc.

Member Functions

def addBots( reqCreateAndLoginTotalCount, reqCreateAndLoginTickCount=0, reqCreateAndLoginTickTime=0 ):

def callback( initialOffset, callbackObj ):

def cancelCallback( id ):

def genUUID64( ):

def getWatcher( path ):

def getWatcherDir( path ):

def scriptLogType( logType ):

def urlopen( url, callback, postData, headers ):

Callback Functions

def onInit( isReload ):

def onFinish( ):

Properties

bots bots

component read-only string

Member Function Documentation


def addBots( reqCreateAndLoginTotalCount, reqCreateAndLoginTickCount=0, reqCreateAndLoginTickTime=0 ):

Description:

Add bots to the server.

Example:

py
# Example of using addBots
import KBEngine

# Instantly add 5 bots to the server
KBEngine.addBots( 5 )

# Add a total of 1000 bots to the server, 5 each time, with a time interval (s) for each batch
KBEngine.addBots( 1000, 5, 10 )

Parameters:

reqCreateAndLoginTotalCount: integer, total number of bots to add to the server.

reqCreateAndLoginTickCount: integer, number of bots to add each time.

reqCreateAndLoginTickTime: integer, time (seconds) for each batch.


def callback( initialOffset, callbackObj ):

Description:

Register a callback, the callback function callbackObj will be triggered after "initialOffset" seconds.

Example:

py
# Example of using callback
import KBEngine

# Add a timer, execute after 1 second
KBEngine.callback( 1, onCallbackfun )

def onCallbackfun( ):
    print("onCallbackfun called")

Parameters:

initialOffset: float, the interval (seconds) from registration to callback execution.

callbackObj: function, the callback function object.

Returns:

integer, returns the internal id of the callback, which can be used to remove the callback with cancelCallback.


def cancelCallback( id ):

Description:

The cancelCallback function is used to remove a registered but not yet triggered callback. After removal, the callback will not be executed. If cancelCallback is called with an invalid id (e.g., already removed), an error will occur.

See KBEngine.callback for an example.

Parameters:

id: integer, specifies the callback id to remove.


def genUUID64( ):

Description:

This function generates a 64-bit unique ID.

Note

This function depends on the Cellapps process startup parameter gus. Please set the startup parameter correctly to maintain uniqueness.

Also, if gus exceeds 65535, this function can only maintain uniqueness within the current process.

Usage:

Generate unique item IDs across multiple service processes without conflicts during server merges.

Generate a room ID across multiple service processes without needing uniqueness checks.

Returns:

Returns a 64-bit integer.


def getWatcher( path ):

Description:

Get the value of a watcher variable from the KBEngine debug system.

Example: In the Python command line of baseapp1:

py
>>> KBEngine.getWatcher("/root/stats/runningTime")
12673648533

>>> KBEngine.getWatcher("/root/scripts/players")
32133

Parameters:

path: string, the absolute path of the variable including the variable name (can be viewed in the watcher page of GUIConsole).

Returns:

The value of the variable.


def getWatcherDir( path ):

Description:

Get the list of elements (directories, variable names) under a watcher directory from the KBEngine debug system.

Example: In the Python command line of baseapp1:

bash
>>> KBEngine.getWatcher("/root")
('stats', 'objectPools', 'network', 'syspaths', 'ThreadPool',
'cprofiles', 'scripts', 'numProxices', 'componentID',
'componentType', 'uid', 'numClients', 'globalOrder',
'username', 'load', 'gametime', 'entitiesSize', 'groupOrder')

Parameters:

path: string, the absolute path of the variable (can be viewed in the watcher page of GUIConsole).

Returns:

A list of elements (directories, variable names) under the watcher directory.


def scriptLogType( logType ):

Description:

Set the type of information output by Python.print (see: KBEngine.LOG_TYPE_*).


def urlopen( url, callback, postData, headers ):

Description:

This script function provides asynchronous HTTP/HTTPS requests.

Parameters:

url: valid HTTP/HTTPS URL, string type.

callback: optional, a callback object with 5 parameters: HTTP response code (e.g., 200), content, HTTP headers, success flag, and requested URL.

Example Declaration

py
def onHttpCallback(httpcode, data, headers, success, url):
    print(httpcode, data, headers, success, url)

As shown above:

httpcode: corresponds to the "HTTP response code", an integer value.

data: corresponds to the "content", a string.

headers: corresponds to the "HTTP headers returned by the server", e.g., {"Content-Type": "application/x-www-form-urlencoded"}, a dictionary.

success: indicates "whether the execution was successful", if there was an error during the request, it will be False, and httpcode can be used to further determine the error.

url: is the URL used for the request.

postData: optional, default is GET request. If a POST request is needed, provide the POST content, and the engine will automatically use POST to request the server, in bytes.

headers: optional, HTTP headers used in the request, e.g., {"Content-Type": "application/x-www-form-urlencoded"}, a dictionary.

Callback Function Documentation


def onInit( isReload ):

Description:

This interface is called after the engine starts and all scripts are initialized.

Note

This callback interface must be implemented in the entry module (kbengine_defaults.xml->entryScriptFile).

Parameters:

isReload: bool, whether this is triggered by reloading the script.


def onFinish( ):

Description:

This function is called when the process is shutting down.

Note

This callback interface must be implemented in the entry module (kbengine_defaults.xml->entryScriptFile).

Property Documentation


bots

Description:

bots is a dictionary object containing all client objects in the current process.

Type:

PyBots


component

Description:

The component currently running in the script environment. (So far) possible values are 'cellapp', 'baseapp', 'client', 'dbmgr', 'bots', and 'editor'.

文档内容由 Comblock® 提供