Freitag, 26. Februar 2010

How to initialize your agent

Since last week the first beta testers could access the stage environment of Zero X. I received some first feedbacks and could already fullfill one of the wishes.

In order to have a proper place to initialize the agent, a first callback method has been introduced. Right after the agent has been started after_start is called once.
module Demo

  class Example < Tournament::Agent

    def after_start
      # initialize
    end

    def think
     # called periodically
    end

  end

end
after_start is called just after the thread for the agent has been forked and one step before the think loop. Here's a simplificated code snippet from the game class, to see how it's implemented:
Thread.fork do
   agent.after_start
   begin
      agent.think
   end until stopped?
end
In your test cases the agent should now be created again before each test, as create_agent also calls after_start.
before :all do
  load_field_fixtures 'greenfields'
end
  
before :each do
  @agent = create_agent Demo::TheGood
end
There's also a test helper method create_agent_without_start, if you need to set test expectations before calling after_start.

Thanks to Tungmar for his feedback!

Keine Kommentare: