Skip to content

EntityComponent Class

Member Functions

def addTimer( self, initialOffset, repeatOffset=0, userArg=0 ):

def delTimer( self, id ):

Callback Functions

def onAttached( self, owner ):

def onDetached( self, owner ):

def onCreateCellFailure( self ):

def onDestroy( self ):

def onGetCell( self ):

def onLoseCell( self ):

def onPreArchive( self ):

def onRestore( self ):

def onTimer( self, timerHandle, userData ):

def onWriteToDB( self, cellData ):

Properties

ownerID Read-only int32

owner Read-only ENTITYCALL

name Read-only string

className Read-only string

isDestroyed bool

cell Read-only CellEntityCall

client Read-only ClientEntityCall

Member Function Documentation


def addTimer( self, initialOffset, repeatOffset=0, userArg=0 ):

Description:

Registers a timer, which is triggered by the callback function onTimer. The callback function will be executed for the first time after "initialOffset" seconds, and then every "repeatOffset" seconds. You can set a user parameter "userArg" (integer only).

The onTimer function must be defined in the base part of the entity and should have 2 parameters: the first is the timer's id (integer, can be used to remove the timer with "delTimer"), and the second is the user parameter "userArg".

Example:

py
# Example of using addTimer
import KBEngine

class MyBaseEntity( KBEngine.Entity ):

    def __init__( self ):
        KBEngine.Entity.__init__( self )

        # Add a timer: first call after 5 seconds, then every 1 second, userArg is 9
        self.addTimer( 5, 1, 9 )

        # Add a timer: first call after 1 second, userArg defaults to 0
        self.addTimer( 1 )

    # Entity's timer callback "onTimer" is called
    def onTimer( self, id, userArg ):
        print "MyBaseEntity.onTimer called: id %i, userArg: %i" % ( id, userArg )
        # if this is a repeating timer, call the following function to remove it when no longer needed:
        #     self.delTimer( id )

Parameters:

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

repeatOffset: float, the interval (in seconds) between each callback after the first. Must be removed with delTimer, otherwise it will repeat indefinitely. Values less than or equal to 0 are ignored.

userArg: integer, the value of userArg parameter when the underlying "onTimer" is called.

Returns:

integer, the internal id of the timer, which can be used to remove the timer with delTimer.


def delTimer( self, id ):

Description:

The delTimer function is used to remove a registered timer. After removal, the timer will no longer execute. Timers that only execute once are automatically removed after the callback, so you don't need to remove them manually. If delTimer is called with an invalid id (e.g., already removed), an error will occur.

See Entity.addTimer for an example of using timers.

Parameters:

id: integer, specifies the timer id to remove. If the parameter is the string "All", all timers will be removed at once.

Callback Function Documentation


def onAttached( self, owner ):

Description:

If this function is implemented in the script, it is called when the component is attached.

Parameters:

owner: The entity object that owns this component.


def onDetached( self, owner ):

Description:

If this function is implemented in the script, it is called when the component is detached.

Parameters:

owner: The entity object that owns this component.


def onCreateCellFailure( self ):

Description:

If this function is implemented in the script, it is called when the cell entity creation fails. This function has no parameters.


def onDestroy( self ):

Description:

If this function is implemented in the script, it is called after Entity.destroy() is called, but before the actual destruction. This function has no parameters.


def onGetCell( self ):

Description:

If this function is implemented in the script, it is called when it obtains the cell entity. This function has no parameters.


def onLoseCell( self ):

Description:

If this function is implemented in the script, it is called after the associated cell entity is destroyed. This function has no parameters.


def onPreArchive( self ):

Description:

If this function is implemented in the script, it is called before the entity is automatically written to the database. This callback is called before the Entity.onWriteToDB callback.
If this callback returns False, the archive operation is aborted. This callback should return True to continue the operation. If this callback does not exist, the archive operation continues.


def onRestore( self ):

Description:

If this function is implemented in the script, it is called when the Entity application crashes and the entity is recreated on another Entity application. This function has no parameters.


def onTimer( self, timerHandle, userData ):

Description:

This function is called when a timer associated with this entity is triggered. A timer can be added using the Entity.addTimer function.

Parameters:

timerHandle: The id of the timer.

userData: The integer user data passed in Entity.addTimer.


def onWriteToDB( self, cellData ):

Description:

If this function is implemented in the script, it is called when the entity data is about to be written to the database.

Note that calling writeToDB in this callback will cause an infinite loop.

Parameters:

cellData: Contains the cell properties to be stored in the database. cellData is a dictionary.

Property Documentation


ownerID

Description:

ownerID is the entity ID of the owner. This id is an integer. This property is read-only.

Type:

Read-only int32


owner

Description:

owner indicates the entity object that owns this component.
When using, call the corresponding method of the owner entity via self.owner.xxx

Type:

Read-only ENTITYCALL


name

Description:

The name of the component.

Type:

Read-only string


className

Description:

The class name of the entity.

Type:

Read-only string


isDestroyed

Description:

If the Entity entity has been destroyed, this property is True.

Type:

bool


cell

Description:

cell is the ENTITYCALL used to contact the cell entity. This property is read-only, and if the base entity does not have an associated cell, the property is None.

Type:

Read-only ENTITYCALL


client

Description:

client is the EntityCall used to contact the client. This property is read-only, and if the base entity does not have an associated client, the property is None.

Type:

Read-only ENTITYCALL

文档内容由 Comblock® 提供