增加对象属性
增加属性
- 修改角色类 mygame/typeclasses/characters.py
class Character(DefaultCharacter):
# [...]
def at_object_creation(self):
"""
Called only at initial creation. This is a rather silly
example since ability scores should vary from Character to
Character and is usually set during some character
generation step instead.
"""
#set persistent attributes
self.db.strength = 5
self.db.agility = 4
self.db.magic = 2
def get_abilities(self):
"""
Simple access method to return ability
scores as a tuple (str,agi,mag)
"""
return self.db.strength, self.db.agility, self.db.magic
- evennia reload
更新自己的角色
at_object_creation只有在对象第一次创建的时候才会执行,所以新增加修改的属性并不会体现在已有角色上。
# (you have to be superuser to use @py)
@py self.get_abilities()
<<< (None, None, None)
更新已有角色的方法:
@update self
@py self.get_abilities()
<<< (5, 4, 2)
批量更新已有对象
当你修改了某个类的初始化方法的时候,可以采用以下方式更新所有已存在的该类对象
// from evennia shell
from typeclasses.myclass import MyClass
# loop over all MyClass instances in the database
# and call .swap_typeclass on them
for obj in MyClass.objects.all():
obj.swap_typeclass(MyClass, run_start_hooks="at_object_creation")
或者
@py from typeclasses.myclass import MyClass;[obj.swap_typeclass(MyClass) for obj in MyClass.objects.all()];
不在db持久化的属性
But sometimes you want to create temporary properties (things that are not to be stored in the database but still always exist every time the object is created). Such properties can be initialized in the at_init method on the object. at_init is called every time the object is loaded into memory.
def at_init(self):
self.ndb.counter = 0
self.ndb.mylist = []
增加命令
我们需要增加一个普通用户可以执行的命令来获取刚增加的属性。
- 修改mygame/commands/command.py
lass CmdAbilities(Command):
"""
List abilities
Usage:
abilities
Displays a list of your current ability values.
"""
key = "abilities"
aliases = ["abi"]
lock = "cmd:all()"
help_category = "General"
def func(self):
"implements the actual functionality"
str, agi, mag = self.caller.get_abilities()
string = "STR: %s, AGI: %s, MAG: %s" % (str, agi, mag)
self.caller.msg(string)
- 修改mygame/commands/default_cmdsets.py
from commands.command import CmdAbilities
//然后在CharacterCmdSet类里增加
self.add(CmdAbilities())
- 这样,我们就可以使用abi或abilities命令了。
为某种类型对象增加命令
- 创建新的cmdSet
# file mygame/commands/mycmdsets.py
#[...]
from commands.command import CmdEcho
from evennia import CmdSet
#[...]
class MyCmdSet(CmdSet):
key = "MyCmdSet"
def at_cmdset_creation(self):
self.add(CmdEcho())
- 为某个对象增加命令集合
// 不能单独添加命令,只能以命令集的方式添加
// permanent如果为false的话,reload一下就没有了。
@py self.cmdset.add("mycmdsets.MyCmdSet", permanent=True)
- 为某类对象增加命令集合
# e.g. in mygame/typeclasses/objects.py
from evennia import DefaultObject
class MyObject(DefaultObject):
def at_object_creation(self):
"called when the object is first created"
self.cmdset.add("mycmdset.MyCmdSet", permanent=True)
- 更新已存在的对象,增加命令集合
@py from typeclasses.objects import MyObject; [o.cmdset.add("mycmdset.MyCmdSet") for o in MyObject.objects.all()]
增加对象类型
- typeclasses目录下增加wiseobject.py
from random import choice
from typeclasses.objects import Object
class WiseObject(Object):
"""
An object speaking when someone looks at it.
We assume it looks like a stone in this example.
"""
def at_object_creation(self):
self.db.wise_texts = [
"Stones have feelings too.",
"To live like a stone is to not have lived at all.",
"The world is like a rock of chocolate.",
]
def return_appearance(self, looker):
"""
Called by the look command.
We want to return a wisdom when we get looked at.
"""
string = super(WiseObject, self).return_appearance(looker)
wisewords = "\n\nIt grumbles and says: '%s'" % choice(self.db.wise_texts)
return string + wisewords
- evennia reload
- 创建一个测试对象
@create/drop stone:wiseobject.WiseObject
@look stone
引用
Tutorial: Building Quick-start
Tutorial: First Steps Coding
Tutorial: Adding new commands
Tutorial: Adding new objects