kquick/Thespian

initialize actor with internal state variables

Closed this issue · 4 comments

I found this issue from a few years ago: thespianpy#18 but I don't think it fits my case. How would I initialize a simple actor with its own id number and perhaps other state variables like:

class Agent(Actor):
    def __init__(self, myid):
        self.myid = myid
        super().__init__()
        print("Actor", self.myid, "started.")

    def receiveMessage(self, message, sender):
        if message["command"] == "greet":
            print("Agent received greet command from", sender, ".")
            print("Hello", message["name"])

What you have is fine (although I recommend standard python convention of adding *args, **kw to the __init__ signature and passing those to the base class initializer as in super().__init__(*args, **kw).

The Actor __init__() cannot perform any actor-related operations (e.g. calling self.send()) and as with the issue you referenced, it should defer initialization requiring external interactions or information to be handled by a received message instead.

You're welcome, and please feel free to re-open this issue if you have further difficulties or questions.

Might be my python knowledge, but I get an error with the following

class DateGroupActor(ActorTypeDispatcher):
  def __init__(self, *args, **kw):
    super().__init__(*args, **kw)
    self.seat_percent = self.createActor(SeatPercentage)

AttributeError: 'DateGroupActor' object has no attribute '_myRef'

I have also tried putting the self. attribute assignment before calling super, no difference.
as far as I can tell, Ive followed the above suggestion, any idea what is wrong?