Frt
2009-03-21 04:56:38
NPCs and how to use them
So you’ve been reading a bit about Reflection but you don’t really seem to get it all? Well, this tutorial should help you get the hang of NPCs as we look into the many functions we have when it comes to the NPCs.
The TNPC
When using NPCs you will work with a TNPC which stores all the information about the given NPC. The TNPC looks like this:
But hey, what use is this information if you don’t know how to use it? Let’s change that!
Finding a NPC
With Reflection comes a lot of functions to find and interact with NPCs but why don’t we start at the beginning:
That’s pretty useful when you are making your script, right? This way you can get all the information you want about the NPC.
But what if you already got the information and you just want to find it? Well, Reflection’s got it all! There is a lot of function for finding the NPC:
Now, let’s say that we want to talk to a NPC, how would we do that? Well, take a look:
Remember, you can use all of the information gained in the TNPC. If making an auto fighter, using the InFight information would be wise.
I hope you liked this tutorial and most important of all, learned something.
So you’ve been reading a bit about Reflection but you don’t really seem to get it all? Well, this tutorial should help you get the hang of NPCs as we look into the many functions we have when it comes to the NPCs.
The TNPC
When using NPCs you will work with a TNPC which stores all the information about the given NPC. The TNPC looks like this:
TNPC = record
Tile, MS, MM: TPoint;
Index, NPCType, Height, Animation, TurnDirection, Combat: Integer;
HPRatio: Integer;
Name: string;
InFight, IsMoving: Boolean;
end;As you can see we can retrieve a lot of information about them like if they are fighting or moving, where they are located, what their name or ID is. Even which way they are turning and what animation they are doing.But hey, what use is this information if you don’t know how to use it? Let’s change that!
Finding a NPC
With Reflection comes a lot of functions to find and interact with NPCs but why don’t we start at the beginning:
function GetNPCs:array of TNPC;This will return an array with all the NPCs nearby which can be pretty usefull for getting the NPCs ID. Now, let’s start using the function:procedure NPCsInArea;
var
i: integer;
N: array of TNPC; // Declares the TNPC array
begin
N := GetNPCs; // Retrieving all NPCs
for i:= 0 to High(N) do // Loops through all NPCs found
begin
Writeln(N[i].Name + ' Found, ID: ' + IntToStr(N[i].NPCType));
Writeln('Located at: ' + IntToStr(N[i].Tile.x) + ', ' + IntToStr(N[i].Tile.y));
Writeln('Fighting: ' + BoolToStr(N[i].InFight) + ', Moving: ' + BoolToStr(N[i].IsMoving));
Writeln('Animation: ' + IntToStr(N[i].Animation));
Writeln('');
end;
end;That’s pretty useful when you are making your script, right? This way you can get all the information you want about the NPC.
But what if you already got the information and you just want to find it? Well, Reflection’s got it all! There is a lot of function for finding the NPC:
Function FindNPC(Name: string; var NPC: TNPC):boolean;
Function FindNPCByID(ID: integer; var NPC: TNPC):boolean; As you can see FindNPC searches for the name while FindNPCByID searches for the ID.Now, let’s say that we want to talk to a NPC, how would we do that? Well, take a look:
procedure NPCTalk;
var
N, P: TNPC;
T: TPoint;
i: integer;
begin
while i < 5 do // Failsafe
begin
if FindNPC('Man', N) then // Searches for NPC named 'Man'
begin
if not TileOnMS(N.Tile, 0) then // If NPC isn't on the MainScreen
WalkToTile(N.Tile, 1, 0); // it walks to the NPC
FindNPCByID(N.NPCType, P); // Refreshes the Tile, decreasing chances to miss
T := TileToMS(P.Tile, 0); // Gets Mainscreen position
Mouse(T.x, T.y, 5, 5, False); // Clicks the NPC
if ChooseOption('Talk') then // Chooses option 'Talk'
begin
Writeln('Talking to NPC');
Break;
end else
Writeln('Could not talk to the NPC');
end else
Writeln('Could not find the NPC');
Inc(i);
end;
end;Piece of cake isn’t it?Remember, you can use all of the information gained in the TNPC. If making an auto fighter, using the InFight information would be wise.
I hope you liked this tutorial and most important of all, learned something.