EntityComponent类
成员函数
def addTimer( self, initialOffset, repeatOffset=0, userArg=0 ):
def delTimer( self, id ):
回调函数
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 ):
属性
ownerID 只读 int32
owner 只读 ENTITYCALL
name 只读 string
className 只读 string
isDestroyed bool
成员函数文档
def addTimer( self, initialOffset, repeatOffset=0, userArg=0 ):
功能说明:
注册一个定时器,定时器由回调函数 onTimer 触发,回调函数将在"initialOffset"秒后被执行第1次,而后将每间隔"repeatOffset"秒执行1次,可设定一个用户参数"userArg"(仅限integer类型)。
onTimer 函数必须在entity的base部分被定义,且带有2个参数,第1个integer类型的是timer的id(可用于移除timer的"delTimer "函数),第2个是用户参数"userArg"。
例子:
# 这里是使用addTimer的一个例子
import KBEngine
class MyBaseEntity( KBEngine.Entity ):
def __init__( self ):
KBEngine.Entity.__init__( self )
# 增加一个定时器,5秒后执行第1次,而后每1秒执行1次,用户参数是9
self.addTimer( 5, 1, 9 )
# 增加一个定时器,1秒后执行,用户参数缺省是0
self.addTimer( 1 )
# Entity的定时器回调"onTimer"被调用
def onTimer( self, id, userArg ):
print "MyBaseEntity.onTimer called: id %i, userArg: %i" % ( id, userArg )
# if 这是不断重复的定时器,当不再需要该定时器的时候,调用下面函数移除:
# self.delTimer( id )
参数:
initialOffset
: float,指定定时器从注册到第一次回调的时间间隔(秒)。
repeatOffset
: float,指定第一次回调执行后每次执行的时间间隔(秒)。必须用函数delTimer 移除定时器,否则它会一直重复下去。值小于等于0将被忽略。
userArg
: integer,指定底层回调"onTimer"时的userArg参数值。
返回:
integer,该函数返回timer的内部id,这个id可用于delTimer移除定时器。
def delTimer( self, id ):
功能说明:
函数delTimer用于移除一个注册的定时器,移除后的定时器不再执行。只执行1次的定时器在执行回调后自动移除,不必要使用delTimer移除。 如果delTimer函数使用一个无效的id(例如已经移除),将会产生错误。
参数:
id
: integer,它指定要移除的定时器id。如果参数为字符串"All",则一次性移除所有的定时器。
回调函数文档
def onAttached( self, owner ):
功能说明:
如果这个函数在脚本中有实现,这个函数在该组件被附加的时候被调用。
参数:
owner
: 该组件的拥有者的实体对象。
def onDetached( self, owner ):
功能说明:
如果这个函数在脚本中有实现,这个函数在该组件被分离的时候被调用。
参数:
owner
: 该组件的拥有者的实体对象。
def onCreateCellFailure( self ):
功能说明:
如果这个函数在脚本中有实现,这个函数在cell实体创建失败的时候被调用。 这个函数没有参数。
def onDestroy( self ):
功能说明:
如果这个函数在脚本中有实现,这个函数在调用Entity.destroy()后,在实际销毁之前被调用。 这个函数没有参数。
def onGetCell( self ):
功能说明:
如果这个函数在脚本中有实现,这个函数在它获得cell实体的时候被调用。 这个函数没有参数。
def onLoseCell( self ):
功能说明:
如果这个函数在脚本中有实现,这个函数在它关联的cell实体销毁之后被调用。 这个函数没有参数。
def onPreArchive( self ):
功能说明:
如果这个函数在脚本中有实现,这个函数在该实体自动写入数据库之前被调用。这个回调在Entity.onWriteToDB 回调之前被调用。
如果该回调返回False,该归档操作中止。这个回调应该返回True使得操作继续。如果这个回调不存在,则归档操作继续进行。
def onRestore( self ):
功能说明:
如果这个函数在脚本中有实现,这个函数在Entity应用程序崩溃后在其它Entity应用程序上 重新创建该实体时被调用。 这个函数没有参数。
def onTimer( self, timerHandle, userData ):
功能说明:
这个函数当一个与此实体关联的定时器触发的时候被调用。 一个定时器可以使用Entity.addTimer函数添加。
参数:
timerHandle
: 定时器的id。
userData
: 传进Entity.addTimer的integer用户数据。
def onWriteToDB( self, cellData ):
功能说明:
如果这个函数在脚本中有实现,这个函数在实体数据将要写进数据库的时候被调用。
需要注意的是在该回调里调用writeToDB会导致无限循环。
参数:
cellData
: 包含将要存进数据库的cell属性。 cellData是一个字典。
属性文档
ownerID
说明:
ownerID是拥有者的实体ID。这个id是一个整型。 这个属性是只读的。
类型:
只读 int32
owner
说明:
owner是指明该组件的拥有者的实体对象
在使用时需要self.owner.xxx调用owner实体的对应方法
类型:
只读 ENTITYCALL
name
说明:
组件的名称。
类型:
只读 string
className
说明:
实体的类名。
类型:
只读 string
isDestroyed
说明:
如果该Entity实体已经被销毁了,这个属性为True。
类型:
bool
cell
说明:
cell是用于联系cell实体的ENTITYCALL 。这个属性是只读的,且如果这个base实体没有关联的cell时属性是None。
类型:
只读 ENTITYCALL
client
说明:
client是用于联系客户端的EntityCall。这个属性是只读的,且如果这个base实体没有关联的客户端时属性是None。
类型:
只读 ENTITYCALL