속성 증가, 명령 및 객체 유형 2018-04-22
객체 속성 증가
속성 증가
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
자신의 역할 업데이트
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 = []
명령 추가
우리는 새로 추가된 속성을 얻기 위해 일반 사용자가 실행할 수 있는 명령을 추가해야 한다.
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)
from commands.command import CmdAbilities
// CharacterCmdSet
self.add(CmdAbilities())
특정 유형의 객체에 대한 명령 추가
# 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()]
객체 유형 추가
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 = "
It grumbles and says: '%s'" % choice(self.db.wise_texts)
return string + wisewords
@create/drop stone:wiseobject.WiseObject
@look stone
인용하다
Tutorial: Building Quick-start Tutorial: First Steps Coding Tutorial: Adding new commands Tutorial: Adding new objects
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSON
JSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다.
그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다.
저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.