Making an Agent class

Hi, im geting a little confused with how to implement a sort of an Agent, that is, a class that receives events and is also executed periodicaly like if it was a task to update its AI.

My first try was something like this:

class Agent(DirectObject):

  def __init__(self,name):
    self.task = taskMgr.add(self.update, name)
    self.accept('Event Name1',self.handler1) 
    ...

  def update(task):
    do something

  def handler1():
    do something

The problem i see here is that ‘update’ being a method has an hidden argument self so won’t this mess with a task function first argument?

Also is there any way to inspect the messenger and manipulate all events addressed to this object before any of them is sent to one of the event handler functions? I was thinking about deleting some events or replace a sequence of key presses (a key combo) for example by another.

An alternative would be to have a method i was sure it would only be called after all event handlers have already been called.

Define your method like this:

def update(self, task):

This might be possible, but it sounds to me like a bad idea–better to go with your plan B, or to have a published interface method instead that accepts whatever events people want to send it, and then does whatever event translation you like after the event has been received. Should be a lot clearer.

David

I was thinking that i could create an “Update” event and send it to the object every X time. This way it would not be necessary to register the agent as a task, instead a single task to send an Update event every X time.

“to have a published interface method instead that accepts whatever events people want to send it, and then does whatever event translation you like after the event has been received.”

How does this trick works exactly? Could you give some details please?

Sure, if you like, but it doesn’t seem like this is an improvement–what’s the difference between registering the object as a task, and registering the object to listen for the “Update” event? It’s one call either way.

I just mean that rather than try to be clever with reworking the messenger dispatch table, just make your update() method have the smarts to decide what to do what it receives a given input parameter.

David

“Sure, if you like, but it doesn’t seem like this is an improvement–what’s the difference between registering the object as a task, and registering the object to listen for the “Update” event? It’s one call either way.”

I realy don’t know for sure. But im probably will have lots of Agents in a combat scene (maybe hundreds) and this would make the task list big. Would it be faster in this circunstances to use the event scheduller instead?

No. The task manager and the messenger are essentially equivalent constructs.

David