While writing a letter in MS Word, I accidentally clicked on the help button and this animated little thing on a a bike jumped up and presented me with an input box. I’ve seen it a few times before, but this time I wondered if I could write a program that could animate characters in Delphi.
To make Merlin read what is in the memo, we would call the Speak() method:
Merlin.speak(memo.text, '')
Before we make Merlin speak, we have to be able to see him, so we call the Show() method:
Merlin.show();
So, our code should look something like this:
var Form1: TForm1; peedy,Merlin:IAgentCtlCharacterEx; implementation {$R *.dfm} procedure TForm1.FormShow(Sender: TObject); begin Agent1.characters.load('Merlin','merlin.acs'); Merlin:=Agent1.Characters.Character('Merlin'); Agent1.Connected:=true; end; procedure TForm1.Button1Click(Sender: TObject); begin Merlin.show(false); Merlin.speak(memo1.text, ''); end; procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean); begin Agent1.Characters.Unload('Merlin'); end;
Here's a test run of the application:
The code above is fine, insofar as getting Merlin to talk. But it is not very well coded. For example, our application will crash if the user presses button1 while Merlin is busy reading. So we need to make a provision for that. To handle checks like that we make use of the loadrequest method:
procedure TForm1.FormShow(Sender: TObject); begin loadrequest:=Agent1.characters.load('Merlin','merlin.acs'); if loadrequest.Status <> 0 then begin //could not load agent messagedlg('Merlin is already loaded',mtError,[mbOK],0); exit; end; Merlin:=Agent1.Characters.Character('Merlin'); Agent1.Connected:=true; end;
An alternative to the above code would be to use the try...except block:
try loadrequest:=Agent1.characters.load('Merlin','merlin.acs'); except on E:Exception do begin messagedlg(E.Message,mtError,[mbOK],0); exit; end;