NPC Support and className fix for the editor
by AIDan · 05/21/2002 (4:33 pm) · 16 comments
--- NPC SUPPORT 1/2---
What is a NPC here?
Maybe you understand it in another way, but this is how I do.
A Player is a humand person playing the game.
A Bot is an AI controlled character with the same opportunities as a player. In fact it simulates one.
A NPC is much more limited. In our game, it cannot enter vehicles, pickup flags, score or chat. AND it does not require a player slot, because it does not use a connection.
First, we have a default datablock with all the data similar for NPCs and Players.
Now we have two different datablocks.
I included an example why to use two different datablocks.
In our project, a NPC cannot pickup the flag, as you see, only the player/bot can.
An NPC can get a special name and can be equiped.
For adding the NPC to the game, we need a create function.
But that is not all. There is a bug within the editor which let it call the PlayerData::create function. Fix this now, but we are not finished, yet.
--- EDITOR FIX ---
EditorGui.cs
...
CHANGE:
TO:
--- NPC SUPPORT 2/2---
Let us have a look at the datablocks again.
Now, NPCs and players have two different classes, which also causes that they need different functions.
Copy all Armor:: functions and rename them to NPC::. I create a new 'server/scripts/npc.cs'.
Now remove all client stuff and if you do not want them to enter vehicles, you can remove the mount functions, too.
If you are making a RPG, you can expand you onAdd function a little bit.
Now, it should work. Modify your NPC:: functions as you need them.
A little hint: When you add a NPC and he/her is dropped to the ground, it might happen that he/her falls through the terrain. Use another kind of dropping instead.
Have fun.
What is a NPC here?
Maybe you understand it in another way, but this is how I do.
A Player is a humand person playing the game.
A Bot is an AI controlled character with the same opportunities as a player. In fact it simulates one.
A NPC is much more limited. In our game, it cannot enter vehicles, pickup flags, score or chat. AND it does not require a player slot, because it does not use a connection.
First, we have a default datablock with all the data similar for NPCs and Players.
datablock PlayerData(DefaultArmor) { };I recommend to remove the className (=Armor) field, if there is any, normally under emap. Same for cmdCategory (="Clients").Now we have two different datablocks.
datablock PlayerData(NPCArmor : DefaultArmor)
{
className= NPC; // NPC class
category= "NPC"; // Add to editor
cmdCategory= "NPCs";
name= "Black Hawk Leader";
has[Hammer]= true;
};
datablock PlayerData(PlayerArmor : DefaultArmor)
{
className= Armor;
cmdCategory= "Clients";
maxInv[Flag1]= 1;
maxInv[Flag2]= 1;
maxInv[Flag3]= 1;
};I included an example why to use two different datablocks.
In our project, a NPC cannot pickup the flag, as you see, only the player/bot can.
An NPC can get a special name and can be equiped.
For adding the NPC to the game, we need a create function.
function NPC::create(%datablock)
{
// Create the player object
%obj= new Player()
{
dataBlock= %datablock;
};
//%obj.setControlObject(%player); // <- AI here
return %obj;
}But that is not all. There is a bug within the editor which let it call the PlayerData::create function. Fix this now, but we are not finished, yet.
--- EDITOR FIX ---
EditorGui.cs
// ---------- SHAPES - add in all the shapes now...
...
CHANGE:
%this.addItem(%grp, %obj.getName(), %obj.getClassName() @ "::create(" @ %obj.getName() @ ");");TO:
if( (%obj.className !$="") && (%obj.getClassName !$=%obj.className) )
%this.addItem(%grp, %obj.getName(), %obj.className @ "::create(" @ %obj.getName() @ ");");
else %this.addItem(%grp, %obj.getName(), %obj.getClassName() @ "::create(" @ %obj.getName() @ ");");--- NPC SUPPORT 2/2---
Let us have a look at the datablocks again.
datablock PlayerData(NPCArmor : DefaultArmor)
{
className= NPC; // NPC class
category= "NPC"; // Add to editor
cmdCategory= "NPCs";
name= "Black Hawk Leader";
has[Hammer]= true;
};
datablock PlayerData(PlayerArmor : DefaultArmor)
{
className= Armor;
cmdCategory= "Clients";
maxInv[Flag1]= 1;
maxInv[Flag2]= 1;
maxInv[Flag3]= 1;
};Now, NPCs and players have two different classes, which also causes that they need different functions.
Copy all Armor:: functions and rename them to NPC::. I create a new 'server/scripts/npc.cs'.
Now remove all client stuff and if you do not want them to enter vehicles, you can remove the mount functions, too.
If you are making a RPG, you can expand you onAdd function a little bit.
function NPC::onAdd(%this,%obj)
{
%data= %obj.getDatablock();
// Vehicle timeout
%obj.mountVehicle= false;
// Default dynamic NPC stats
%obj.setRechargeRate(%this.rechargeRate);
%obj.setRepairRate(0);
if(%data.has[Hammer]) // Equip Character
%obj.incInventory(Hammer, %data.has[Hammer]);
if(%data.has[Mattock]) // Equip Character
%obj.incInventory(Mattock, %data.has[Mattock]);
if(%obj.name !$="") // If it is a special character,you can give him/her a name.
%obj.setShapeName(%obj.name);
%obj.startFade(2000, 0, false);
}Now, it should work. Modify your NPC:: functions as you need them.
A little hint: When you add a NPC and he/her is dropped to the ground, it might happen that he/her falls through the terrain. Use another kind of dropping instead.
Have fun.
About the author
#2
DefaultArmor includes basic data which is same for every player/bot and NPC. Like how high can it jump? Or how fast can it run. You can use another datablock, too, like HumanArmor for all human players/bots and NPCs.
The performance depends mainly on the polycount of your NPCs and the AI.
I could not get the AI work, yet. It looks like you have to write a new 'interface' to let the AI interact with the NPC's shape. I'm no programmer, though. Our programmr will look for it and when we got it work, we will share it.
gretings
Daniel
05/23/2002 (9:50 am)
HiDefaultArmor includes basic data which is same for every player/bot and NPC. Like how high can it jump? Or how fast can it run. You can use another datablock, too, like HumanArmor for all human players/bots and NPCs.
The performance depends mainly on the polycount of your NPCs and the AI.
I could not get the AI work, yet. It looks like you have to write a new 'interface' to let the AI interact with the NPC's shape. I'm no programmer, though. Our programmr will look for it and when we got it work, we will share it.
gretings
Daniel
#3
05/24/2002 (3:24 pm)
Ok, i thought the poly count had something to do with how many i can put, but i also thought that the AI calculations and stuff would make an impact also if you put to many, but seeing how there isn't realy AI it doesn't matter.
#4
{
// Vehicle timeout
%obj.mountVehicle= false;
// Default dynamic NPC stats
%obj.setRechargeRate(%this.rechargeRate);
%obj.setRepairRate(0);
//if(%obj.has[Hammer]) // Equip Character
// %obj.incInventory(Hammer, 1);
//if(%obj.has[Mattock]) // Equip Character
// %obj.incInventory(Mattock, 1);
if(%obj.name !$="") // If it is a special character,you can give him/her a name.
%obj.setShapeName(%obj.name);
}
%obj.startFade(2000, 0, false);
}
i think the middle } shouldn't be there.
05/24/2002 (3:39 pm)
function NPC::onAdd(%this,%obj){
// Vehicle timeout
%obj.mountVehicle= false;
// Default dynamic NPC stats
%obj.setRechargeRate(%this.rechargeRate);
%obj.setRepairRate(0);
//if(%obj.has[Hammer]) // Equip Character
// %obj.incInventory(Hammer, 1);
//if(%obj.has[Mattock]) // Equip Character
// %obj.incInventory(Mattock, 1);
if(%obj.name !$="") // If it is a special character,you can give him/her a name.
%obj.setShapeName(%obj.name);
}
%obj.startFade(2000, 0, false);
}
i think the middle } shouldn't be there.
#5
05/24/2002 (4:12 pm)
Hey, also you forgot to add a ; to the end of your datablocks after the }
#6
I fixed this.
05/26/2002 (9:18 pm)
Thx, these mistakes arised when posting the code in the forum.I fixed this.
#7
06/01/2002 (7:54 am)
There was a bug in the NPC::onAdd function. "has[ITEM]" is part of the datablock not the object itself.
#8
Perhaps there is something I am missing?
-J
06/05/2002 (1:17 am)
Thanks, I added this no problem... now to get them to walk around and stuff, looks like some hackage to the movement code? (client keystroke stuff is how it is currently setup for Player class)...Perhaps there is something I am missing?
-J
#9
I got the AI to work for it rather easily,
just change:
%obj= new Player()
to
%obj= new AIPlayer()
in the create function.
I tested it by doing things like this:
%obj.setAimObject( %col );
%obj.setMoveSpeed(1);
%obj.setMoveDestination( "500 500 500" );
in the onCollision function, and it worked prefect. The NPC takes off runnin, or backpeddles away from ya if you set the aim object :)
11/21/2002 (1:09 am)
Thanks for the npc addition! I got the AI to work for it rather easily,
just change:
%obj= new Player()
to
%obj= new AIPlayer()
in the create function.
I tested it by doing things like this:
%obj.setAimObject( %col );
%obj.setMoveSpeed(1);
%obj.setMoveDestination( "500 500 500" );
in the onCollision function, and it worked prefect. The NPC takes off runnin, or backpeddles away from ya if you set the aim object :)
#10
setMoveDestingation("x y z")
(I am working on implementing Dijkstra's algorithm for weighted graph nodes in interiors, hope I'm not doing the same as the GG staff...)
11/21/2002 (1:15 am)
Oh yeah... the setMoveDestination("500 500 500") was just a test.. you'll have to find a way to make it go where you wanted...setMoveDestingation("x y z")
(I am working on implementing Dijkstra's algorithm for weighted graph nodes in interiors, hope I'm not doing the same as the GG staff...)
#11
11/29/2002 (3:55 am)
As i'm working since 2 weeks with torque it is a little bit hard for me to understand where to go with this code. I know that the player properties depends from their class (eg. LihgtMaleHumanArmor). Please is somebodey so kind to explain me where I have to add and where I have to change the code to work?? In my planing game I need bots and npc's. Thank you for your help.....
#12
02/21/2003 (2:36 am)
Great work guys! Just what we needed for our project!
#13
With some wrenching I got this in.
Works great with the latest HEAD.
Added the RPG dialog to it with no problems.
I'm dropping scripted AI everywhere!
Killer work!
This resource has been assimilated!
Ari
07/14/2004 (9:12 pm)
Thanks Future Interactive,With some wrenching I got this in.
Works great with the latest HEAD.
Added the RPG dialog to it with no problems.
I'm dropping scripted AI everywhere!
Killer work!
This resource has been assimilated!
Ari
#14
I've added all the code from above into the file server\scripts\npc.cs and made the changes to common\editor\EditorGui.cs
12/04/2004 (3:16 pm)
How do I "add an NPC" ?I've added all the code from above into the file server\scripts\npc.cs and made the changes to common\editor\EditorGui.cs
#15
OK I just recently Added a NPC, How would I be able to make him talk? As in, I want him to say certain things..
08/10/2006 (9:05 pm)
Hey, DeveloperOK I just recently Added a NPC, How would I be able to make him talk? As in, I want him to say certain things..
#16
04/07/2008 (6:45 pm)
Im new to game developing. How do i simply place other nps's into the game and have them just be enemys i can shoot at (and them shoot back) and kill? 
Torque Owner Tyler Turner
datablock PlayerData(DefaultArmor) { };
does that mean copy that Datablock with all of its values and change the name to NPCArmor? Good tutorial though. Its just the start I needed to add NPC. also, how many can you add to a map before it slows down? does it do anythinking or does it just stand there?