InvisibleSoldiers wrote:Syntax: Select all
from listeners.tick import Repeat
from players.dictionary import PlayerDictionary
class CustomPlayer(Player):
def __init__(self, index):
super().__init__(index)
self.sex = 'female'
self.age = 16
self.is_dead = False
self.aging_timer = Repeat(aging, (self.index, ))
PLAYERS = PlayerDictionary(CustomPlayer)
def aging(index):
player = PLAYERS[index]
player.age += 1
if player.sex == 'female' and player.age == 74:
player.is_dead = True
elif player.sex == 'male' and player.age == 68:
player.is_dead = True
else:
print('Dead or Alive')
You could simply use something like this:
Syntax: Select all
from listeners.tick import Repeat
from players.dictionary import PlayerDictionary
class CustomPlayer(Player):
def __init__(self, index):
super().__init__(index)
self.sex = 'female'
self.age = 16
self.is_dead = False
self.aging_timer = Repeat(aging, (self, ))
class CustomPlayers(PlayerDictionary):
def on_automatically_removed(self, index):
self[index].aging_timer.stop()
PLAYERS = CustomPlayers(CustomPlayer)
def aging(player):
player.age += 1
if player.sex == 'female' and player.age == 74:
player.is_dead = True
elif player.sex == 'male' and player.age == 68:
player.is_dead = True
else:
print('Dead or Alive')
Or if your repeat needs to run infinitely, you could probably just use a delay to repeat it:
Syntax: Select all
def aging(player):
player.delay(interval, (player,))
aging(player)
Entity.delay takes care to cancel delays when the entities are deleted. Perhaps we could add something similar for repeats. Open an issue as a feature request on the repo if you'd like to see something like that.
InvisibleSoldiers wrote:And if the code will split into different files, this will lead to cyclic imports
If you request a function or a class before it is declared then yes, you will end with a cyclic import. A solution is often to move the import into local scope, or simply import the whole module like import x rather than the object through a from x import y, etc. But generally, if you have modules that needs to import each other they are often a good fit to form a module together.
InvisibleSoldiers wrote:Maybe with your early commits linked with entity instances caching i can simply will do it.
Yes, this would work, as the same object would be returned. However I would probably personally use a dictionary as demonstrated above regardless, as it is more elegant in my opinion as it allows you to pass the object around directly instead of instantiating/looking up the cache every times.
I don't have access to the backend, Ayuto is the one doing the public builds since our
buildbot was shut down last year. I'm sure he will do a new build in due time.