View Full Version : NPC's in a RPG

16-07-2004, 05:50 PM
I'm currently doing some RPG components containing all the stuff you need to create RPG's like Dialog, Spell and Item handling, Abillities etc.
The components will be quite easy to use with any graphics engine.

I have a little nut to crack however. I'm not sure what technique is best to use to keep track of NPC's throughout the world. The way I was thinking, was that the player and NPC's uses the same structure to keep things simple. The structure contains stuff like health and position. The idea is to have the main player search for potential "targets" within a certan distance.

So, the problem is: how do I keep track of all the NPC's? I thought about using LinkedLists or something, but I could really use some advice from someone more experienced than me. Also, the components should be able to diside what gets drawn. Is this possible while still keeping the "independant graphics engine" principle, or should I just write a graphics engine aswell?

Harry Hunt
16-07-2004, 06:13 PM
Well, of course you could use linked lists, but I for my part love object orientation and it might make things a lot easier for you. (You could for example have a base object and two inherited objects, one for the player and one for the NPCs).
You might also want to look at TCollection/TCollectionItem which is what I normally use to keep track of enemies/NPCs. A linked list will probably outperform a TCollection, but so far, I never had any speed problems while using collections.

16-07-2004, 11:43 PM
Thanks for the suggestion :)

I need an additional advice: some of my components have to have access to each other, mainly my event handler which needs access to all components and vica versa. I can't figure out a way to do this without getting a circular reference. It won't do any good to add the unit below implementation since that would force me to do a global instance of the event handler (which isn't the end of the world, but it would be neat to avoid it).

17-07-2004, 12:15 AM
You could try to have a reference to a base class instead of the real class. It's a bit hard to explain so I'll just throw in the code:

TMyClass = class
FMyOtherClass: TObject;
procedure SetMyOtherClass(Value: TObject);



procedure TMyClass.SetMyOtherClass;
Assert(Value is TMyOtherClass, 'Value need to be a TMyOtherClass reference');
FMyOtherClass := TMyOtherClass(Value);

Sometimes it works good by doing it that way other times it don't, all depending on you own special needs.

As for a global reference. You could use a singleton (which can only have one instance) instead of it. In the end it's the same thing but it looks better and are probably a bit more stable.

17-07-2004, 01:35 AM
Thanks! :) It lets me link the components just fine. Only one slight problem: I can't access the class I've assigned, only it's parent class.
IE I can't access my TRPGItems class, only it's parent TComponent.