속성 증가, 명령 및 객체 유형 2018-04-22

5409 단어

객체 속성 증가


속성 증가

  • 캐릭터 클래스 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 = "

    It 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

    좋은 웹페이지 즐겨찾기