KBEngine - KBEngine base

KBEngine API

KBEngine

KBEngine module

This KBEngine module provides the Python script access to the entity's base part, in particular it provides the registration and removal of timers, as well as the creation of entities.

Classes

Entity
Proxy

Member functions

def addWatcher( path, dataType, getFunction ):
def address( ):
def MemoryStream( ):
def charge( ordersID, dbID, byteDatas, pycallback ):
def createEntity( ):
def createEntityAnywhere( entityType, *params, callback ):
def createEntityRemotely( entityType, baseMB, *params, callback ):
def createEntityFromDBID( entityType, dbID, callback, dbInterfaceName ):
def createEntityAnywhereFromDBID( entityType, dbID, callback, dbInterfaceName ):
def createEntityRemotelyFromDBID( entityType, dbID, baseMB, callback, dbInterfaceName ):
def createEntityLocally( entityType, *params ):
def debugTracing( ):
def delWatcher( path ):
def deleteEntityByDBID( entityType, dbID, callback, dbInterfaceName ):
def deregisterReadFileDescriptor( fileDescriptor ):
def deregisterWriteFileDescriptor( fileDescriptor ):
def executeRawDatabaseCommand( command, callback, threadID, dbInterfaceName ):
def genUUID64( ):
def getResFullPath( res ):
def getWatcher( path ):
def getWatcherDir( path ):
def getAppFlags( ):
def hasRes( res ):
def isShuttingDown( ):
def listPathRes( path, extension ):
def lookUpEntityByDBID( entityType, dbID, callback, dbInterfaceName ):
def matchPath( res ):
def open( res, mode ):
def publish( ):
def quantumPassedPercent( ):
def registerReadFileDescriptor( fileDescriptor, callback ):
def registerWriteFileDescriptor( fileDescriptor, callback ):
def reloadScript( fullReload ):
def scriptLogType( logType ):
def setAppFlags( flags ):
def time( ):

Callback functions

def onBaseAppReady( isBootstrap ):
def onBaseAppShutDown( state ):
def onCellAppDeath( addr ):
def onFini( ):
def onBaseAppData( key, value ):
def onBaseAppDataDel( key ):
def onGlobalData( key, value ):
def onGlobalDataDel( key ):
def onInit( isReload ):
def onLoseChargeCB( orderID, dbID, success, datas ):
def onReadyForLogin( isBootstrap ):
def onReadyForShutDown( ):
def onAutoLoadEntityCreate( entityType, dbID ):

Attributes

LOG_ON_ACCEPT 
LOG_ON_REJECT 
LOG_ON_WAIT_FOR_DESTROY 
LOG_TYPE_DBG 
LOG_TYPE_ERR 
LOG_TYPE_INFO 
LOG_TYPE_NORMAL 
LOG_TYPE_WAR 
NEXT_ONLY 
component  Read-only string
entities  Entities
baseAppData  GlobalDataClient
globalData  GlobalDataClient

Member functions documentation

def addWatcher( path, dataType, getFunction ):

Function description:
Interacts with the debug monitoring system, allowing the user to register a monitoring variable with the monitoring system.

Example:
>>> def countPlayers( ):
>>>     i = 0
>>>     for e in KBEngine.entities.values():
>>>     	if e.__class__.__name__ == "Avatar":
>>>     		i += 1
>>>     return i
>>>
>>> KBEngine.addWatcher( "players", "UINT32", countPlayers )


This function adds a watch variable under the "scripts/players" watch path. The function countPlayers is called when the watcher observes.

parameters:

path Create a monitored path.
dataType The value type of the monitor variable. Reference: Basic data types
getFunction This function is called when the observer retrieves the variable. This function returns a value representing a watch variable without arguments.

def address( ):

Function description:

Returns the address of the internal network interface.

def MemoryStream( ):

Function description:

Returns a new MemoryStream object.

The MemoryStreamobject stores binary information. This type is provided to allow the user to easily serialize and deserialize the Python base types following KBEngine underlying serialization rules.

For example, you can use this object to construct a network packet that KBEngine can parse.

Usage:
>>> s = KBEngine.MemoryStream()
>>> s
>>> b''
>>> s.append("UINT32", 1)
>>> s.pop("UINT32")
>>> 1


The types that MemoryStream currently supports are only basic data types. Reference: Basic data types

def charge( ordersID, dbID, byteDatas, pycallback ):

Function description:

Billing interface.

parameters:

ordersID string, order ID.
dbID uint64, the databaseID of the entity.
byteDatas bytes, with data, which is parsed and defined by the developer.
pycallback Billing callback.
Billing callback prototype: (When calling KBEngine.chargeResponse in interfaces, the callback is called if an order is set to callback)
 def on*ChargeCB(self, orderID, dbID, success, datas):
   ordersID: string, OrderID
   dbID: uint64, usually the databaseID of the entity.
   success: bool, whether the order succeeded    datas: bytes, with data, parsed and defined by the developer.

def createEntity( ):

Function description:

KBEngine.createEntityLocally alias.

def createEntityAnywhere( entityType, params, callback ):

Function description:

Create a new base Entity. The server can choose any Baseapp to create an Entity.

This method should be preferred over KBEngine.createEntityLocally so the server has the flexibility to choose a suitable Baseapp to create an entity.

The function parameters need to provide the type of entity created, and there is also a Python dictionary as a parameter to initialize the entities value.

The Python dictionary does not require the user to provide all of the properties, and the default values provided by the entity definition file ".def" are defaults.

Example:
params = {
	"name" : "kbe", # base, BASE_AND_CLIENT
	"HP" : 100,	# cell, ALL_CLIENT, in cellData
	"tmp" : "tmp"	# baseEntity.tmp
}

def onCreateEntityCallback(entity)
	print(entity)

createEntityAnywhere("Avatar", params, onCreateEntityCallback)

parameters:

entityType string, specifies the type of Entity to create. Valid entity types are listed in /scripts/entities.xml.
params optional parameter, a Python dictionary object. If a specified key is an Entity attribute, its value will be used to initialize the properties of this Entity. If the key is a Cell attribute, it will be added to the 'cellData' attribute of the Entity. This cellData' attribute is a Python dictionary and will be used later to initialize the attributes of the cell entity.
callback An optional callback function that is called when the entity is created. The callback function takes one argument, when the Entity is created successfully it is the entity's entityCall, on failure it is None.

returns:

Returns the entityCall of the Entity through the callback.

def createEntityRemotely( entityType, baseMB, params, callback ):

Function description:

Create a new Entity on the specified baseapp through the baseMB parameter.

KBEngine.createEntityAnywhere should be preferred over this method to allow the server to decide which is the most suitable Baseapp to create the entity on for load balancing purposes.

The function parameters need to provide the type of the created entity, and there is also a Python dictionary as a parameter to initialize the entity's value.

This Python dictionary does not require the user to provide all of the properties, and the default values provided by the entity definition file ". def" are defaults.

Example:
params = {
	"name" : "kbe", # base, BASE_AND_CLIENT
	"HP" : 100,	# cell, ALL_CLIENT, in cellData
	"tmp" : "tmp"	# baseEntity.tmp
}

def onCreateEntityCallback(entity)
	print(entity)

createEntityRemotely("Avatar", baseEntityCall, params, onCreateEntityCallback)

parameters:

entityType string, specifies the type of Entity to create. Valid entity types are listed in /scripts/entities.xml.
baseMB BaseEntityCall which is a base Entity EntityCall. The entity will be created on the baseapp process corresponding to this entity.
params Optional parameters, a Python dictionary object. If a specified key is an Entity attribute, its value will be used to initialize the properties of this Entity. If this key is a Cell attribute, it will be added to the Entity's 'cellData' attribute. This 'cellData' attribute is a Python dictionary and will be used later to initialize the attributes of the cell entity.
callback An optional callback function that is called when the entity is created. The callback takes one argument, on success it is an Entity实体的entityCall, on failure it is None.

returns:

Returns the Entity's entityCall through the callback.

def createEntityFromDBID( entityType, dbID, callback, dbInterfaceName ):

Function description:

Create an Entity by loading data from the database. The new Entity will be created on the Baseapp that called this function. If the Entity has been checked out from the database, a reference to this existing entity will be returned.

parameters:

entityType string, specifies the Entity type to load. Valid entity types are listed in /scripts/entities.xml.
dbID Specifies the database ID of the entity to create. The database ID of this entity is stored in the entity's databaseID attribute.
callback An optional callback function that is called when the operation is complete. The callback function has three parameters: baseRef, databaseID, and wasActive. If the operation is successful, baseRef will be an entityCall or a direct reference to the newly created Entity. The databaseID will be the database ID of the entity. wasActive will be True if baseRef is a reference to an already existing entity (checked out from the database). If the operation fails the three parameters will be baseRef - None, databaseID - 0, wasActive - False.
The most common reason for failure is that the entity does not exist in the database, but occasionally other errors such as timeouts or ID allocation failure.
dbInterfaceName string, optional parameter, specified by a database interface, and the "default" interface is used by default. Database interfaces are defined in kbengine_defaults.xml->dbmgr->databaseInterfaces.

def createEntityAnywhereFromDBID( entityType, dbID, callback, dbInterfaceName ):

Function description:

Create an Entity by loading data from the database. The server may choose any Baseapp to create the Entity.

Using this function will help BaseApps load balance.

If the entity has been checked out from the database, a reference to the existing Entity will be returned.

parameters:

entityType string, specifies the Entity type to load. Valid entity types are listed in /scripts/entities.xml.
dbID Specifies the database ID of the entity to create. The database ID of this entity is stored in the entity's databaseID attribute.
callback An optional callback function that is called when the operation is complete. The callback function has three parameters: baseRef, databaseID, and wasActive. If the operation is successful, baseRef will be an entityCall or a direct reference to the newly created Entity. The databaseID will be the database ID of the entity. wasActive will be True if baseRef is a reference to an already existing entity (checked out from the database). If the operation fails the three parameters will be baseRef - None, databaseID - 0, wasActive - False.
The most common reason for failure is that the entity does not exist in the database, but occasionally other errors such as timeouts or ID allocation failure.
dbInterfaceName string, optional parameter, specified by a database interface, and the "default" interface is used by default. Database interfaces are defined in kbengine_defaults.xml->dbmgr->databaseInterfaces.

returns:

The Entity's entityCall through the callback.

def createEntityRemotelyFromDBID( entityType, dbID, baseMB, callback, dbInterfaceName ):

Function description:

Load data from the database and create an Entity on the baseapp specified via the baseMB parameter.

If the entity has been checked out from the database, a reference to the existing Entity will be returned.

parameters:

entityType string, specifies the Entity type to load. Valid entity types are listed in /scripts/entities.xml.
dbID Specifies the database ID of the entity to create. The database ID of this entity is stored in the entity's databaseID attribute.
callback An optional callback function that is called when the operation is complete. The callback function has three parameters: baseRef, databaseID, and wasActive. If the operation is successful, baseRef will be an entityCall or a direct reference to the newly created Entity. The databaseID will be the database ID of the entity. wasActive will be True if baseRef is a reference to an already existing entity (checked out from the database). If the operation fails the three parameters will be baseRef - None, databaseID - 0, wasActive - False.
The most common reason for failure is that the entity does not exist in the database, but occasionally other errors such as timeouts or ID allocation failure.
dbInterfaceName string, optional parameter, specified by a database interface, and the "default" interface is used by default. Database interfaces are defined in kbengine_defaults.xml->dbmgr->databaseInterfaces.

returns:

Returns the Entity's entityCall through the callback.

def createEntityLocally( entityType, params ):

Function description:

Create a new Entity. The function parameters need to provide the type of the created entity, and there is also an optional Python dictionary as paramater to initialize the entity's values.

The Python dictionary does not require the user to provide all of the properties, and the default values provided by the entity definition file ".def" are defaults.

KBEngine.createEntityAnywhere should be preferred over this method to allow the server to decide which is the most suitable Baseapp to create the entity on for load balancing purposes.

It should be noted that this method returns the entity instantly without a callback, and is also guaranteed to return a direct reference to the Entity object, rather than its EntityCall. It is suitable to use this method over KBEngine.createEntityAnywhere when you need to manage the entities life cycle (such as control when destroy is called on the entity) or access the entities attributes from the creating entity, because as described in the EntityCall documentation, it is not possible to access attributes or call methods not listed in the entity's def file using the EntityCall. This method is also necessary to use when you need a direct reference to an entity (as it's not possible to get one on a different baseapp). Many functions take an EntityCall as a parameter, but some require a direct reference to the entity (such as Proxy.giveClientTo).

Example:
params = {
	"name" : "kbe", # base, BASE_AND_CLIENT
	"HP" : 100,	# cell, ALL_CLIENT, in cellData
	"tmp" : "tmp"	# baseEntity.tmp
}

baseEntity = createEntityLocally("Avatar", params)

parameters:

entityType string, specifies the type of entity to create. Valid entity types are listed in /scripts/entities.xml.
params optional parameter, a Python dictionary object. If a specified key is an Entity attribute, its value will be used to initialize the properties of this Entity. If the key is a Cell attribute, it will be added to the 'cellData' attribute of the Entity. This cellData' attribute is a Python dictionary and will be used later to initialize the attributes of the cell entity.

returns:

The newly created Entity.

def debugTracing( ):

Function description:

Outputs the Python extended object counter currently tracked by KBEngine.
Extended objects include: fixed dictionary, fixed array, Entity, EntityCall...
If the counter is not zero when the server is shut down normally, it means that the leak already exists and the log will output an error message.

ERROR cellapp [0x0000cd64] [2014-11-12 00:38:07,300] - PyGC::debugTracing(): FixedArray : leaked(128)
ERROR cellapp [0x0000cd64] [2014-11-12 00:38:07,300] - PyGC::debugTracing(): EntityCall : leaked(8)

def delWatcher( path ):

Function description:

Interacts with the debug monitoring system, allowing users to delete monitored variables in the script.

parameters:

path The path to the variable to delete.

def deleteEntityByDBID( entityType, dbID, callback, dbInterfaceName ):

Function description:

Deletes the specified entity (including the child table data generated by the attribute) from the database. If the entity is not checked out from the database, the deletion is successful. If the entity has been checked out from the database, KBEngine will fail to delete and return the Entity's entityCall in the callback.

parameters:

entityType string, specifies the type of Entity to delete. Valid entity types are listed in /scripts/entities.xml.
dbID Specifies the database ID of the entity to delete. The database ID of the entity is stored in the entity's databaseID attribute.databaseID属性。
callback An optional callback, with only one parameter. When the entity has not been checked out from the database it will be deleted successfully and the parameter will be True. If the entity has been checked out from the database then the parameter is the Entity's entityCall.
dbInterfaceName String, optional parameter, specifies a database interface. By default it uses the "default" interface. Database interfaces are defined by kbengine_defaults.xml->dbmgr->databaseInterfaces.

def deregisterReadFileDescriptor( fileDescriptor ):

Function description:

Unregisters the callback registered with KBEngine.registerReadFileDescriptor.

Example:
http://www.kbengine.org/assets/other/py/Poller.py

parameters:

fileDescriptor socket descriptor/file descriptor

def deregisterWriteFileDescriptor( fileDescriptor ):

Function description:

Unregisters the callback registered with KBEngine.registerWriteFileDescriptor.

Example:
http://www.kbengine.org/assets/other/py/Poller.py

parameters:

fileDescriptor socket descriptor/file descriptor.

def executeRawDatabaseCommand( command, callback, threadID, dbInterfaceName ):

Function description:

This script function executes a database command on the database, which is directly parsed by the relevant database.

Please note that using this function to modify entity data may not be effective because if the entity has been checked out, the modified data will still be archived by the entity and cause overwriting.
This function is strongly not recommended for reading or modifying entity data.

parameters:

command This database command will be different for different database configuration scenarios. For a MySQL database it is an SQL query.
callback

Optional parameter, callback object (for example, a function) called back with the command execution result.
This callback has 4 parameters: result set, number of rows affected, auto value, error message.
Example:
def sqlcallback(result, rows, insertid, error):     print(result, rows, insertid, error)

As the above example shows, the result parameter corresponds to the "result set", and the result set parameter is a row List. Each line is a list of strings containing field values. If the command execution does not return a result set (for example, a DELETE command), or the command execution encounters an error, the result set is None.

The rows parameter is the "number of rows affected", which is an integer indicating the number of rows affected by the command execution. This parameter is only relevant for commands that do not return results (such as DELETE).
This parameter is None if there is a result set return or if there is an error in the command execution.

The insertid is a long value, similar to an entity's databaseID. When successfully inserting data int a table with an auto long type field, it returns the data at the time of insertion.
More information can be found in mysql's mysql_insert_id() method. In addition, this parameter is only meaningful when the database type is mysql.

Error corresponds to the "error message", when the command execution encounters an error, this parameter is a string describing the error. This parameter is None when the command execution has not occurred.

threadID int32, optional parameter, specifies a thread to process this command. Users can use this parameter to control the execution order of certain commands (dbmgr is multi-threaded). The default is not specified. If threadId is the ID of an entity, it will be added to the entity's archive queue and written by the thread one by one.
dbInterfaceName string, optional parameter, specifies a database interface. By default it uses the "default" interface. Database interfaces are defined by kbengine_defaults.xml->dbmgr->databaseInterfaces.

def genUUID64( ):

Function description:

This function generates a 64-bit unique ID.
Note: This function depends on the baseapp server process startup parameter 'gus'. Please set the startup parameters to be unique.
In addition, if gus exceeds 65535, this function can only remain unique for the current process.

Usage:
Unique IDs can be generated on multiple service processes and do not conflict.
A room ID can be generated on multiple service processes and no uniqueness verification is required.

returns:

Returns a 64-bit integer.

def getResFullPath( res ):

Function description:

Get the absolute path of a resource.
Note: Resource must be accessible under KBE_RES_PATH.

parameters:

res string, the relative path of the resource

returns:

string, if there is an absolute path to the given resource, otherwise returns null.

def getWatcher( path ):

Function description:

Gets the value of a watch variable from the KBEngine debugging system.

Example: In the Python console of baseapp1:
>>>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 on the GUIConsole watcher page).

returns:

The value of the variable.

def getWatcherDir( path ):

Function description:

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

Example: In the Python console of baseapp1 enter:
>>>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 to this variable (can be viewd on the GUIConsole watcher page).

returns:

Monitors the list of elements in the directory (directory, variable name).

def getAppFlags( ):

Function description:

Get the flags of the current engine APP, Reference: KBEngine.setAppFlags.

returns:

KBEngine.APP_FLAGS_*

def hasRes( res ):

Function description:

Use this interface to determine if a relative path exists.
Note: Resource must be accessible under KBE_RES_PATH.

Example:

>>>KBEngine.hasRes("scripts/entities.xml")
True

parameters:

res string, the relative path of the resource

returns:

bool, True if relative path exists, otherwise False.

def isShuttingDown( ):

Function description:

Returns whether the server is shutting down.
After the onBaseAppShutDown(state=0) is called, this function returns True.

returns:

True if the server is shutting down, otherwise False.

def listPathRes( path, extension ):

Function description:

Get a list of resources in a resource directory
Note: Resources must be accessible under KBE_RES_PATH.

Example:

>>>KBEngine.listPathRes("scripts/cell/interfaces")
('/home/kbe/kbengine/demo/res/scripts/cell/interfaces/AI.py', '/home/kbe/kbengine/demo/res/scripts/cell/interfaces/New Text Document.txt')

>>>KBEngine.listPathRes("scripts/cell/interfaces", "txt")
('/home/kbe/kbengine/demo/res/scripts/cell/interfaces/New Text Document.txt')

>>>KBEngine.listPathRes("scripts/cell/interfaces", "txt|py")
('/home/kbe/kbengine/demo/res/scripts/cell/interfaces/AI.py', '/home/kbe/kbengine/demo/res/scripts/cell/interfaces/New Text Document.txt')

>>>KBEngine.listPathRes("scripts/cell/interfaces", ("txt", "py"))
('/home/kbe/kbengine/demo/res/scripts/cell/interfaces/AI.py', '/home/kbe/kbengine/demo/res/scripts/cell/interfaces/New Text Document.txt')

parameters:

res string, the relative path of the resource directory
extension string, optional parameter, file extension to filter by

returns:

Tuple, resource list.

def lookUpEntityByDBID( entityType, dbID, callback, dbInterfaceName ):

Function description:

Queries whether an entity is checked out of the database, and if the entity has been checked out of the database, KBEngine will return the Entity's entityCall in the callback.

parameters:

entityType string, specifies the type of Entity to query. Valid entity types are listed in /scripts/entities.xml.
dbID Specifies the database ID of the Entity to be queried. The database ID is stored in the entity's databaseID attribute.
callback A callback with one parameter, True when the entity is not checked out from the database, if it is checked out then it is the Entity's entityCall. False in any other case.
dbInterfaceName string, optional parameter, specifies a database interface. Uses the "default" interface by default. Database interfaces are defined in kbengine_defaults.xml->dbmgr->databaseInterfaces.

def matchPath( res ):

Function description:

Get the absolute path of a resource from its relative path.
Note: Resources must be accessible under KBE_RES_PATH.

Examples:

>>>KBEngine.matchPath("scripts/entities.xml")
'/home/kbe/kbengine/demo/res/scripts/entities.xml'

parameters:

res string, the relative path of the resource (including its name).

returns:

string, the absolute path of the resource.

def open( res, mode ):

Function description:

Use this function to open resources with their relative paths.
Note: Resource must be accessible under KBE_RES_PATH.

parameters:

res string, the relative path of the resource.
mode string, file operation mode:
w Open in write mode,
a Open in append mode (Start from EOF, create new file if necessary)
r+ Open
w+ in read/write mode Open in read/write mode (see w)
a+ Open in read/write mode (See a)
rb Opens
wb in binary read mode Opens in binary write mode (see w)
ab Opens in binary append mode (see a)
rb+ Opens in binary read and write mode (see r+)
wb+ Opens in binary read and write mode (see w+ )
ab+ opens in binary read/write mode (see a+)

def publish( ):

Function description:

This function returns the server's current release mode.

returns:

int8, 0: debug, 1: release, others can be customized.

def quantumPassedPercent( ):

Function description:

Returns the percentage of the current tick that takes one clock cycle.

returns:

Returns the percentage of the current tick that takes one clock cycle.

def registerReadFileDescriptor( fileDescriptor, callback ):

Function description:

Registers a callback function that is called when the file descriptor is readable.

Example:
http://www.kbengine.org/assets/other/py/Poller.py

parameters:

fileDescriptor socket descriptor/file descriptor.
callback A callback function with the socket descriptor/file descriptor as its only parameter.

def registerWriteFileDescriptor( fileDescriptor, callback ):

Function description:

Registers a callback function that is called when the socket descriptor/file descriptor is writable.

Example:
http://www.kbengine.org/assets/other/py/Poller.py

parameters:

fileDescriptor socket descriptor/file descriptor
callback A callback function with the socket descriptor/file descriptor as its only parameter.

def reloadScript( fullReload ):

Function description:

Reloads Python modules related to entity and custom data types. The current entity's class is set to the newly loaded class. This method should only be used for development mode and not for product mode. The following points should be noted:

1) The overloaded script can only be executed on Baseapp, and the user should ensure that all server components are loaded.

2) The custom type should ensure that the objects already instantiated in memory are updated after the script is reloaded. Here is an example:

for e in KBEngine.entities.values():
   if type( e ) is Avatar.Avatar:
      e.customData.__class__ = CustomClass
When this mehod completes, KBEngine.onInit( True ) is called.

parameters:

fullReload bool, optional parameter that specifies whether to reload entity definitions at the same time. If this parameter is False, the entity definition will not be reloaded. The default is True.

returns:

True if the reload succeeds, otherwise False.

def scriptLogType( logType ):

Function description:

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

def setAppFlags( flags ):

Function description:

Set the flags of the current engine APP.

KBEngine.APP_FLAGS_NONE // Default (not set)
KBEngine.APP_FLAGS_NOT_PARTCIPATING_LOAD_BALANCING //Do not participate in load balancing

Example:
KBEngine.setAppFlags(KBEngine.APP_FLAGS_NOT_PARTCIPATING_LOAD_BALANCING | KBEngine.APP_FLAGS_*)

def time( ):

Function description:

This method returns the current game time (number of cycles).

returns:

uint32, the time of the current game. This refers to the number of cycles. The period is affected by the frequency. The frequency is determined by the configuration file kbengine.xml or kbengine_defaults.xml->gameUpdateHertz.


Callback functions documentation

def onBaseAppReady( isBootstrap ):

Function description:

This callback function is called when the current Baseapp process is ready.
Note: This callback function must be implemented in the portal module (kbengine_defaults.xml->entryScriptFile).

parameters:

isBootstrap bool, True if this is the first Baseapp started

def onBaseAppShutDown( state ):

Function description:

The Baseapp shutdown procedure will call this function.
Note: This callback function must be implemented in the portal module (kbengine_defaults.xml->entryScriptFile).

parameters:

state If state is 0, it means that it is before all clients are disconnected, if state is 1, it means that it is before all entities are written to the database, if state is 2, it mean all entities have been written to the database.

def onCellAppDeath( addr ):

Function description:

This callback function will be called on the death of a cellapp.
Note: This callback function must be implemented in the portal module (kbengine_defaults.xml->entryScriptFile).

parameters:

addr Dead cellapp address.
tuple:(ip, port) Network byte order

def onFini( ):

Function description:

This callback function is called after the engine is officially shut down.
Note: This callback function must be implemented in the portal module (kbengine_defaults.xml->entryScriptFile).

def onBaseAppData( key, value ):

Function description:

This function is called back when KBEngine.baseAppData changes.
Note: This callback function must be implemented in the portal module (kbengine_defaults.xml->entryScriptFile).

parameters:

key The key of the changed data.
value The value of the changed data.

def onBaseAppDataDel( key ):

Function description:

This function is called back when KBEngine.baseAppData is deleted.
Note: This callback function must be implemented in the portal module (kbengine_defaults.xml->entryScriptFile).

parameters:

key Deleted data key

def onGlobalData( key, value ):

Function description:

This function is called back when KBEngine.globalData changes.
Note: This callback function must be implemented in the portal module (kbengine_defaults.xml->entryScriptFile).

parameters:

key The key of the changed data
value The value of the changed data

def onGlobalDataDel( key ):

Function description:

This function is called back when KBEngine.globalData is deleted.
Note: This callback function must be implemented in the portal module (kbengine_defaults.xml->entryScriptFile).

parameters:

key Deleted data key.

def onInit( isReload ):

Function description:

This function is called back after all scripts have been initialized after the engine started.
Note: This callback function must be implemented in the portal module (kbengine_defaults.xml->entryScriptFile).

parameters:

isReload bool, whether it was triggered after rewriting the loading script.

def onLoseChargeCB( orderID, dbID, success, datas ):

Function description:

This function is called back when KBEngine.chargeResponse is called in and the order is lost or unknown.
Note: This callback function must be implemented in the portal module (kbengine_defaults.xml->entryScriptFile).

parameters:

ordersID string, order ID.
dbID uint64, the database ID of the entity, see: Entity.databaseID.
success bool, is it successful?
datas bytes, with information

def onReadyForLogin( isBootstrap ):

Function description:

When the engine is started and initialized, it will always call this function to ask whether the script layer is ready. If the script layer is ready, loginapp allows the client to log in.
Note: This callback function must be implemented in the portal module (kbengine_defaults.xml->entryScriptFile).

parameters:

isBootstrap bool, True if this is the first Baseapp started.

returns:

If the return value is greater than or equal to 1.0, the script layer is ready; otherwise, return a value from 0 to less than 1.0.

def onReadyForShutDown( ):

Function description:

If this callback function is implemented in the script, it is called when the process is ready to exit.

You can use this callback to control when the process exits.

Note: This callback function must be implemented in the portal module (kbengine_defaults.xml->entryScriptFile).

returns:

bool if it returns True, it allows the process to exit. Returning other values will cause the process to ask again after a period of time.

def onAutoLoadEntityCreate( entityType, dbID ):

Function description:

Called when an automatically loaded entity is created. If the script layer implements this callback, the entity is created by the script layer, otherwise the engine defaults to create the entity using createEntityAnywhereFromDBID.

This callback is envoked because Entity.writeToDB was set to automatically load the entity.
Note: this callback takes precedence over onBaseAppReady execution and can be checked for onBaseAppReady when the entity is loaded.

parameters:

entityType string, specifies the type of entity to query. Valid entity types are listed in /scripts/entities.xml.
dbID specifies the database ID of the Entity to be queried. The database ID of this entity is stored in the entity's databaseID attribute.


Attributes documentation

LOG_ON_ACCEPT

Description:

This constant is returned by Proxy.onLogOnAttempt, and means that the new client is allowed to bind to a Proxy entity.
If the Proxy entity already has a client binding, the previous client will be kicked out.

LOG_ON_REJECT

Description:

This constant is returned by Proxy.onLogOnAttempt, which means that the current client is bound to the Proxy entity.

LOG_ON_WAIT_FOR_DESTROY

Description:

This constant is returned by Proxy.onLogOnAttempt. The current requesting client will wait until the Proxy entity is completely destroyed and the underlying layer will complete the subsequent binding process. Before this returns, Proxy.destroy or Proxy.destroyCellEntity should be invoked.

LOG_TYPE_DBG

Description:

The log output type is debug.
Set by scriptLogType.

LOG_TYPE_ERR

Description:

The log output type is error.
Set byscriptLogType.

LOG_TYPE_INFO

Description:

The log output type is general information.
Set by scriptLogType.

LOG_TYPE_NORMAL

Description:

The log output type is normal.
Set by scriptLogType.

LOG_TYPE_WAR

Description:

The log output type is warning.
Set by scriptLogType.

NEXT_ONLY

Description:

This constant is used for the Entity.shouldAutoBackup and Entity.shouldAutoArchive attributes and means that the entity is backed up automatically next time it is deemed acceptable, and then the attribute is automatically set to false (0).

component

Description:

This is the component that is running in the current Python environment. (So far) Possible values are 'cell', 'base', 'client', 'database', 'bot', and 'editor'.

entities

Description:

entities is a dictionary object that contains all the entities in the current process.
Debugging leaked entities (instances that call destroy without releasing memory, usually due to being referenced):
>>> KBEngine.entities.garbages.items()
[(1025, Avatar object at 0x7f92431ceae8.)]

>>> e = _[0][1] >>> import gc >>> gc.get_referents(e) [{'spacesIsOk': True, 'bootstrapIdx': 1}, ]

Debugging a leaked KBEngine-encapsulated Python object:
KBEngine.debugTracing

Types:

Entities

baseAppData

Description:

This attribute contains a dictionary-like object that is automatically synchronized across all BaseApps. When a value in the dictionary is modified, the change is broadcast to all BaseApps.

Example:
KBEngine.baseAppData[ "hello" ] = "there"


The rest of the BaseApps can access the following:
print KBEngine.baseAppData[ "hello" ]


Keys and values can be of any type, but these types must be encapsulated and unpacked on all target components.

When a value is changed or deleted, a callback function is called on all components. See: KBEngine.onBaseAppData and KBEngine.onDelBaseAppData.

Note: Only top-level value changes will be broadcast. If you have a value (such as a list) that changes an internal value (such as just changing a number), this information will not be broadcast.

Do not do the following:
KBEngine.baseAppData[ "list" ] = [1, 2, 3]
KBEngine.baseAppData[ "list" ][1] = 7
The local access is [1, 7, 3] and the remote access is [1, 2, 3].

globalData

Description:

This attribute contains a dictionary-like object that is automatically synchronized across all BaseApps and CellApps. When a value in the dictionary is modified, the change is broadcast to all BaseApps and CellApps.

example:
KBEngine.globalData[ "hello" ] = "there"


The other Baseapps and Cellapps can access the following:
print KBEngine.globalData[ "hello" ]


Keys and values can be of any type, but these types must be encapsulated and unpacked on all target components.

When a value is changed or deleted, a callback function is called on all components. See: KBEngine.onGlobalData and KBEngine.onGlobalDataDel.

Note: Only top-level value changes will be broadcast. If you have a value (such as a list) that changes an internal value (such as just changing a number), this information will not be broadcast.

Do not do the following:
KBEngine.globalData[ "list" ] = [1, 2, 3]
KBEngine.globalData[ "list" ][1] = 7
The local access is [1, 7, 3] and the remote access is [1, 2, 3].