Raised This Month: $12 Target: $400
 3% 

[HOWTO] Make a NPC with extra features.


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Mini_Midget
Veteran Member
Join Date: Jan 2006
Location: It's a mystery.
Old 12-19-2010 , 09:39   [HOWTO] Make a NPC with extra features.
Reply With Quote #1

Seeing that there are a lot of posts in Twilight Suzuka's thread of How To: Make a perfect NPC. I decided to try and make my very own NPC using that tutorial but added extra features.
Some of these are:
  • Hit animations
  • Death animations
  • Idle animations
  • Bleed/spark effects
  • NPC voice speaking
  • And many more!

Before we start this tutorial we better explain what exactly is a NPC and why should we make one before how.

NPC stands for Non-Playable Character. As the name suggests it is simply an entity that is non-playable. We as coders have full control on how they interact and work.
Some examples of what a NPC could do are:
  • Bystanders that animates. (Pretty cool to make for Soccer Jam)
  • An item shop owner
  • Props for your mod. (Making lamp/light posts that could flicker)
  • Whatever you can think of!

Alright! Lets finally begin on making our very own NPC!

We start by including the necessary libraries to make our NPC

PHP Code:
#include <amxmodx>
#include <amxmisc>
#include <fakemeta>
#include <engine>
#include <hamsandwich> 

A bunch of global variables that our NPC will use.

PHP Code:
//Boolean of when NPC spawned 
new boolg_NpcSpawn[256]; 
//Boolean to check if NPC is alive or not 
new boolg_NpcDead[256]; 
//Classname for our NPC 
new const g_NpcClassName[] = "ent_npc"
//Constant model for NPC 
new const g_NpcModel[] = "models/barney.mdl"

//List of sounds our NPC will emit when damaged 
new const g_NpcSoundPain[][] =  

    
"barney/ba_pain1.wav"
    
"barney/ba_pain2.wav"
    
"barney/ba_pain3.wav" 


//Sounds when killed 
new const g_NpcSoundDeath[][] = 

    
"barney/ba_die1.wav"
    
"barney/ba_die2.wav"
    
"barney/ba_die3.wav" 


//Sounds when we knife our flesh NPC
new const g_NpcSoundKnifeHit[][] = 
{
    
"weapons/knife_hit1.wav",
    
"weapons/knife_hit2.wav",
    
"weapons/knife_hit3.wav",
    
"weapons/knife_hit4.wav"
}

new const 
g_NpcSoundKnifeStab[] = "weapons/knife_stab.wav";

//List of idle animations 
new const NPC_IdleAnimations[] = { 012311121821396365 };

//Sprites for blood when our NPC is damaged
new spr_blood_dropspr_blood_spray

//Player cooldown for using our NPC 
new Floatg_Cooldown[32];

//Boolean to check if we knifed our NPC
new boolg_Hit[32]; 

Now we have to precache our model and sounds or the game will crash! And also a command to spawn our NPC.

In this section we have to initiate some forwards for our NPC. These are where the extra features are called from.

This is also where we can load our saved configurations.

PHP Code:
public plugin_init()
{
    
register_plugin("NPC Plugin""1.1""Mazza");
    
register_clcmd("say /npc""ClCmd_NPC");
    
    
register_event("HLTV""Event_NewRound""a""1=0""2=0");
        
    
RegisterHam(Ham_TakeDamage"info_target""npc_TakeDamage");
    
RegisterHam(Ham_Killed"info_target""npc_Killed");
    
RegisterHam(Ham_Think"info_target""npc_Think");
    
RegisterHam(Ham_TraceAttack"info_target""npc_TraceAttack");
    
RegisterHam(Ham_ObjectCaps"player""npc_ObjectCaps");
    
    
register_forward(FM_EmitSound"npc_EmitSound"); 
}

public 
plugin_precache()
{
    
spr_blood_drop precache_model("sprites/blood.spr")
    
spr_blood_spray precache_model("sprites/bloodspray.spr")
    
    new 
i;
    for(
sizeof g_NpcSoundPain i++)
        
precache_sound(g_NpcSoundPain[i]);
    for(
sizeof g_NpcSoundDeath i++)
        
precache_sound(g_NpcSoundDeath[i]);

    
precache_model(g_NpcModel)
}

public 
plugin_cfg()
{
    
Load_Npc()


When we type "/npc" a menu will display with options of what to do.
Note: I am using the New AMXX Menu System to display the menu thanks to Emp`

PHP Code:
public ClCmd_NPC(id)
{
    
//Create a new menu
    
new menu menu_create("NPC: Main Menu""Menu_Handler");
    
    
//Add some items to the newly created menu
    
menu_additem(menu"Create NPC""1");
    
menu_additem(menu"Delete NPC""2");
    
menu_additem(menu"Save current NPC locations""3");
    
menu_additem(menu"Delete all NPC""4");
    
    
//Let the menu have an 'Exit' option
    
menu_setprop(menuMPROP_EXITMEXIT_ALL);
    
    
//Display our menu
    
menu_display(idmenu);
}

public 
Menu_Handler(idmenuitem)
{
    
//If user chose to exit menu we will destroy our menu
    
if(item == MENU_EXIT)
    {
        
menu_destroy(menu);
        return 
PLUGIN_HANDLED;
    }
    
    new 
info[6], szName[64];
    new 
accesscallback;
    
    
menu_item_getinfo(menuitemaccessinfocharsmax(info), szNamecharsmax(szName), callback);
    
    new 
key str_to_num(info);
    
    switch(
key)
    {
        case 
1:
        {
            
//Create our NPC
            
Create_Npc(id);
        }
        case 
2:
        {
            
//Remove our NPC by the users aim
            
new iEntbodyszClassname[32];
            
get_user_aiming(idiEntbody);
            
            if (
is_valid_ent(iEnt)) 
            {
                
entity_get_string(iEntEV_SZ_classnameszClassnamecharsmax(szClassname));
                
                if (
equal(szClassnameg_NpcClassName)) 
                {
                    
remove_entity(iEnt);
                }
                
            }
        }
        case 
3:
        {
            
//Save the current locations of all the NPCs
            
Save_Npc();
            
            
client_print(idprint_chat"[AMXX] NPC origin saved succesfully");
        }
        case 
4:
        {
            
//Remove all NPCs from the map
            
remove_entity_name(g_NpcClassName);
            
            
client_print(idprint_chat"[AMXX] ALL NPC origin removed");
        }
    }
    
    
//Keep the menu displayed when we choose an option
    
menu_display(idmenu);
    
    return 
PLUGIN_HANDLED;


Add some extra features for our NPC.

PHP Code:
public npc_TakeDamage(iEntinflictorattackerFloat:damagebits)
{
    
//Make sure we only catch our NPC by checking the classname
    
new className[32];
    
entity_get_string(iEntEV_SZ_classnameclassNamecharsmax(className))
    
    if(!
equali(classNameg_NpcClassName))
        return;
        
    
//Play a random animation when damanged
    
Util_PlayAnimation(iEntrandom_num(1317), 1.25);

    
//Make our NPC say something when it is damaged
    //NOTE: Interestingly... Our NPC mouth (which is a controller) moves!! That saves us some work!!
    
emit_sound(iEntCHAN_VOICEg_NpcSoundPain[random(sizeof g_NpcSoundPain)],  VOL_NORMATTN_NORM0PITCH_NORM)
}

public 
npc_Killed(iEnt)
{
    new 
className[32];
    
entity_get_string(iEntEV_SZ_classnameclassNamecharsmax(className))
    
    if(!
equali(classNameg_NpcClassName))
        return 
HAM_IGNORED;

    
//Player a death animation once our NPC is killed
    
Util_PlayAnimation(iEntrandom_num(2530))

    
//Because our NPC may look like it is laying down. 
    //The bounding box size is still there and it is impossible to change it so we will make the solid of our NPC to nothing
    
entity_set_int(iEntEV_INT_solidSOLID_NOT);

    
//The voice of the NPC when it is dead
    
emit_sound(iEntCHAN_VOICEg_NpcSoundDeath[random(sizeof g_NpcSoundDeath)],  VOL_NORMATTN_NORM0PITCH_NORM)

    
//Our NPC is dead so it shouldn't take any damage and play any animations
    
entity_set_float(iEntEV_FL_takedamage0.0);
    
//Our death boolean should now be true!!
    
g_NpcDead[iEnt] = true;
        
    
//The most important part of this forward!! We have to block the death forward.
    
return HAM_SUPERCEDE
}

public 
npc_Think(iEnt)
{
    if(!
is_valid_ent(iEnt))
        return;
    
    static 
className[32];
    
entity_get_string(iEntEV_SZ_classnameclassNamecharsmax(className))
    
    if(!
equali(classNameg_NpcClassName))
        return;
    
    
//We can remove our NPC here if we wanted to but I left this blank as I personally like it when there is a NPC coprse laying around
    
if(g_NpcDead[iEnt])
    {
        return;
    }
        
    
//Our NPC just spawned
    
if(g_NpcSpawn[iEnt])
    {
        static 
Floatmins[3], Floatmaxs[3];
        
pev(iEntpev_absminmins);
        
pev(iEntpev_absmaxmaxs);

        
//Draw a box which is the size of the bounding NPC
        
message_begin(MSG_BROADCASTSVC_TEMPENTITY)
        
write_byte(TE_BOX)
        
engfunc(EngFunc_WriteCoordmins[0])
        
engfunc(EngFunc_WriteCoordmins[1])
        
engfunc(EngFunc_WriteCoordmins[2])
        
engfunc(EngFunc_WriteCoordmaxs[0])
        
engfunc(EngFunc_WriteCoordmaxs[1])
        
engfunc(EngFunc_WriteCoordmaxs[2])
        
write_short(100)
        
write_byte(random_num(25255))
        
write_byte(random_num(25255))
        
write_byte(random_num(25255))
        
message_end();
        
        
//Our NPC spawn boolean is now set to false
        
g_NpcSpawn[iEnt] = false;
    }
    
    
//Choose a random idle animation
    
Util_PlayAnimation(iEntNPC_IdleAnimations[random(sizeof NPC_IdleAnimations)]);

    
//Make our NPC think every so often
    
entity_set_float(iEntEV_FL_nextthinkget_gametime() + random_float(5.010.0));
}

public 
npc_TraceAttack(iEntattackerFloatdamageFloatdirection[3], tracedamageBits)
{
    if(!
is_valid_ent(iEnt))
        return;
    
    new 
className[32];
    
entity_get_string(iEntEV_SZ_classnameclassNamecharsmax(className))
    
    if(!
equali(classNameg_NpcClassName))
        return;
        
    
//Retrieve the end of the trace
    
new Floatend[3]
    
get_tr2(traceTR_vecEndPosend);
    
    
//This message will draw blood sprites at the end of the trace
    
message_begin(MSG_BROADCAST,SVC_TEMPENTITY)
    
write_byte(TE_BLOODSPRITE)
    
engfunc(EngFunc_WriteCoordend[0])
    
engfunc(EngFunc_WriteCoordend[1])
    
engfunc(EngFunc_WriteCoordend[2])
    
write_short(spr_blood_spray)
    
write_short(spr_blood_drop)
    
write_byte(247// color index
    
write_byte(random_num(15)) // size
    
message_end()
}

public 
npc_ObjectCaps(id)
{
    
//Make sure player is alive
    
if(!is_user_alive(id))
        return;

    
//Check when player presses +USE key
    
if(get_user_button(id) & IN_USE)
    {        
        
//Check cooldown of player when using our NPC
        
static Floatgametime gametime get_gametime();
        if(
gametime 1.0 g_Cooldown[id])
        {
            
//Get the classname of whatever ent we are looking at
            
static iTargetiBodyszAimingEnt[32];
            
get_user_aiming(idiTargetiBody75);
            
entity_get_string(iTargetEV_SZ_classnameszAimingEntcharsmax(szAimingEnt));
            
            
//Make sure our aim is looking at a NPC
            
if(equali(szAimingEntg_NpcClassName))
            {
                
//Do more fancy stuff here such as opening a menu
                //But for this tutorial I will only display a message to prove it works
                
client_print(idprint_chat"Hello");
            }
            
            
//Set players cooldown to the current gametime
            
g_Cooldown[id] = gametime;
        }
    }
}

public 
npc_EmitSound(idchannelsample[], Float:volumeFloat:attnflagpitch)
{
    
//Make sure player is alive
    
if(!is_user_connected(id))
        return 
FMRES_SUPERCEDE;

    
//Catch the current button player is pressing
    
new iButton get_user_button(id);
                    
    
//If the player knifed the NPC
    
if(g_Hit[id])
    {    
        
//Catch the string and make sure its a knife 
        
if (sample[0] == 'w' && sample[1] == 'e' && sample[8] == 'k' && sample[9] == 'n')
        {
            
//Catch the file of _hitwall1.wav or _slash1.wav/_slash2.wav
            
if(sample[17] == 's' || sample[17] == 'w')
            {
                
//If player is slashing then play the knife hit sound
                
if(iButton IN_ATTACK)
                {
                    
emit_sound(idCHAN_WEAPONg_NpcSoundKnifeHit[random(sizeof g_NpcSoundKnifeHit)], volumeattnflagpitch);
                }
                
//If player is tabbing then play the stab sound
                
else if(iButton IN_ATTACK2)
                {
                    
emit_sound(id,CHAN_WEAPONg_NpcSoundKnifeStabvolumeattnflagpitch);
                }

                
//Reset our boolean as player is not hitting NPC anymore
                
g_Hit[id] = false;
                
                
//Block any further sounds to be played
                
return FMRES_SUPERCEDE
            
}
        }
    }
    
    return 
FMRES_IGNORED


In the event of a new round. We will reset our NPC properties.

PHP Code:
public Event_NewRound()
{
    new 
iEnt = -1;
    
    
//Scan and find all of the NPC classnames
    
while( ( iEnt find_ent_by_class(iEntg_NpcClassName) ) )
    {
        
//If we find a NPC which is dead...
        
if(g_NpcDead[iEnt])
        {
            
//Reset the solid box
            
entity_set_int(iEntEV_INT_solidSOLID_BBOX);
            
//Make our NPC able to take damage again
            
entity_set_float(iEntEV_FL_takedamage1.0);
            
//Make our NPC instanstly think
            
entity_set_float(iEntEV_FL_nextthinkget_gametime() + 0.01);
            
            
//Reset the NPC boolean to false
            
g_NpcDead[iEnt] = false;
        }    
        
        
//Reset the health of our NPC
        
entity_set_float(iEntEV_FL_health250.0);
    }

The method that actually creates our NPC.

PHP Code:
Create_Npc(idFloat:flOrigin[3]= { 0.00.00.0 }, Float:flAngle[3]= { 0.00.00.0 } )
{
    
//Create an entity using type 'info_target'
    
new iEnt create_entity("info_target");
    
    
//Set our entity to have a classname so we can filter it out later
    
entity_set_string(iEntEV_SZ_classnameg_NpcClassName);
        
    
//If a player called this function
    
if(id)
    {
        
//Retrieve the player's origin
        
entity_get_vector(idEV_VEC_originflOrigin);
        
//Set the origin of the NPC to the current players location
        
entity_set_origin(iEntflOrigin);
        
//Increase the Z-Axis by 80 and set our player to that location so they won't be stuck
        
flOrigin[2] += 80.0;
        
entity_set_origin(idflOrigin);
        
        
//Retrieve the player's  angle
        
entity_get_vector(idEV_VEC_anglesflAngle);
        
//Make sure the pitch is zeroed out
        
flAngle[0] = 0.0;
        
//Set our NPC angle based on the player's angle
        
entity_set_vector(iEntEV_VEC_anglesflAngle);
    }
    
//If we are reading from a file
    
else 
    {
        
//Set the origin and angle based on the values of the parameters
        
entity_set_origin(iEntflOrigin);
        
entity_set_vector(iEntEV_VEC_anglesflAngle);
    }

    
//Set our NPC to take damange and how much health it has
    
entity_set_float(iEntEV_FL_takedamage1.0);
    
entity_set_float(iEntEV_FL_health250.0);

    
//Set a model for our NPC
    
entity_set_model(iEntg_NpcModel);
    
//Set a movetype for our NPC
    
entity_set_int(iEntEV_INT_movetypeMOVETYPE_PUSHSTEP);
    
//Set a solid for our NPC
    
entity_set_int(iEntEV_INT_solidSOLID_BBOX);
    
    
    
//Create a bounding box for oru NPC
    
new Floatmins[3] = {-12.0, -12.00.0 }
    new 
Floatmaxs[3] = { 12.012.075.0 }

    
entity_set_size(iEntminsmaxs);
    
    
//Controllers for our NPC. First controller is head. Set it so it looks infront of itself
    
entity_set_byte(iEnt,EV_BYTE_controller1,125);
    
// entity_set_byte(ent,EV_BYTE_controller2,125);
    // entity_set_byte(ent,EV_BYTE_controller3,125);
    // entity_set_byte(ent,EV_BYTE_controller4,125);
    
    //Drop our NPC to the floor
    
drop_to_floor(iEnt);
    
    
// set_rendering( ent, kRenderFxDistort, 0, 0, 0, kRenderTransAdd, 127 );
    
    //We just spawned our NPC so it should not be dead
    
g_NpcSpawn[iEnt] = true;
    
g_NpcDead[iEnt] = false;
    
    
//Make it instantly think
    
entity_set_float(iEntEV_FL_nextthinkget_gametime() + 0.01)


The methods which load and save the locations of our NPC.

PHP Code:
Load_Npc()
{
    
//Get the correct filepath and mapname
    
new szConfigDir[256], szFile[256], szNpcDir[256];
    
    
get_configsdir(szConfigDircharsmax(szConfigDir));
    
    new 
szMapName[32];
    
get_mapname(szMapNamecharsmax(szMapName));
    
    
formatex(szNpcDircharsmax(szNpcDir),"%s/NPC"szConfigDir);
    
formatex(szFilecharsmax(szFile),  "%s/%s.cfg"szNpcDirszMapName);
        
    
//If the filepath does not exist then we will make one
    
if(!dir_exists(szNpcDir))
    {
        
mkdir(szNpcDir);
    }
    
    
//If the map config file does not exist we will make one
    
if(!file_exists(szFile))
    {
        
write_file(szFile"");
    }
    
    
//Variables to store when reading our file
    
new szFileOrigin[3][32]
    new 
sOrigin[128], sAngle[128];
    new 
Float:fOrigin[3], Float:fAngles[3];
    new 
iLineiLengthsBuffer[256];
    
    
//When we are reading our file...
    
while(read_file(szFileiLine++, sBuffercharsmax(sBuffer), iLength))
    {
        
//Move to next line if the line is commented
        
if((sBuffer[0]== ';') || !iLength)
            continue;
        
        
//Split our line so we have origin and angle. The split is the vertical bar character
        
strtok(sBuffersOrigincharsmax(sOrigin), sAnglecharsmax(sAngle), '|'0);
                
        
//Store the X, Y and Z axis to our variables made earlier
        
parse(sOriginszFileOrigin[0], charsmax(szFileOrigin[]), szFileOrigin[1], charsmax(szFileOrigin[]), szFileOrigin[2], charsmax(szFileOrigin[]));
        
        
fOrigin[0] = str_to_float(szFileOrigin[0]);
        
fOrigin[1] = str_to_float(szFileOrigin[1]);
        
fOrigin[2] = str_to_float(szFileOrigin[2]);
                
        
//Store the yawn angle
        
fAngles[1] = str_to_float(sAngle[1]);
        
        
//Create our NPC
        
Create_Npc(0fOriginfAngles)
        
        
//Keep reading the file until the end
    
}
}

Save_Npc()
{
    
//Variables
    
new szConfigsDir[256], szFile[256], szNpcDir[256];
    
    
//Get the configs directory.
    
get_configsdir(szConfigsDircharsmax(szConfigsDir));
    
    
//Get the current map name
    
new szMapName[32];
    
get_mapname(szMapNamecharsmax(szMapName));
    
    
//Format 'szNpcDir' to ../configs/NPC
    
formatex(szNpcDircharsmax(szNpcDir),"%s/NPC"szConfigsDir);
    
//Format 'szFile to ../configs/NPC/mapname.cfg
    
formatex(szFilecharsmax(szFile), "%s/%s.cfg"szNpcDirszMapName);
        
    
//If there is already a .cfg for the current map. Delete it
    
if(file_exists(szFile))
        
delete_file(szFile);
    
    
//Variables
    
new iEnt = -1Float:fEntOrigin[3], Float:fEntAngles[3];
    new 
sBuffer[256];
    
    
//Scan and find all of my custom ents
    
while( ( iEnt find_ent_by_class(iEntg_NpcClassName) ) )
    {
        
//Get the entities' origin and angle
        
entity_get_vector(iEntEV_VEC_originfEntOrigin);
        
entity_get_vector(iEntEV_VEC_anglesfEntAngles);
        
        
//Format the line of one custom ent.
        
formatex(sBuffercharsmax(sBuffer), "%d %d %d | %d"floatround(fEntOrigin[0]), floatround(fEntOrigin[1]), floatround(fEntOrigin[2]), floatround(fEntAngles[1]));
        
        
//Finally write to the mapname.cfg file and move on to the next line
        
write_file(szFilesBuffer, -1);
        
        
//We are currentlying looping to find all custom ents on the map. If found another ent. Do the above till there is none.
    
}
    


Miscellaneous method for setting animations for our NPC.

PHP Code:
stock Util_PlayAnimation(indexsequenceFloatframerate 1.0)
{
    
entity_set_float(indexEV_FL_animtimeget_gametime());
    
entity_set_float(indexEV_FL_framerate,  framerate);
    
entity_set_float(indexEV_FL_frame0.0);
    
entity_set_int(indexEV_INT_sequencesequence);


This is my first tutorial on these forums so any feedback is welcome.

Post any questions or comments about this tutorial and I'll do my best to answer them.

Once again, thanks for checking this tutorial out!!
Attached Files
File Type: sma Get Plugin or Get Source (npc.sma - 2466 views - 16.1 KB)
__________________
It's a mystery.

Last edited by Mini_Midget; 03-07-2012 at 00:13.
Mini_Midget is offline
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 12-19-2010 , 10:53   Re: [HOWTO] Make a NPC with extra features.
Reply With Quote #2

There is no reason to use RegisterHamFromEntity() here, also it should be register one time. You should use RegisterHam() + "info_target".
If you play with float, use engfunc( EngFunc_WriteCoord, instead of write_coord + floatround().
__________________
Arkshine is offline
bibu
Veteran Member
Join Date: Sep 2010
Old 12-19-2010 , 10:57   Re: [HOWTO] Make a NPC with extra features.
Reply With Quote #3

I really like this tutorial alot, especially that this is your first tutorial. You commented every line very well and I could understand everything. Some suggestion, it would be useful in the tutorial how to detect the touch of the NPC. And also giving the NPC a weapon, I know it is on the other tutorial explained but I found this tutorial much more advanced.

Thanks!

EDIT: A good example of saving the NPC on maps would be a cool feature.

Last edited by bibu; 12-19-2010 at 12:43.
bibu is offline
abdul-rehman
Veteran Member
Join Date: Jan 2010
Location: Khi, Pakistan
Old 12-19-2010 , 12:43   Re: [HOWTO] Make a NPC with extra features.
Reply With Quote #4

Dude, this is nice and useful, an awsome job you have done
__________________

My Plugins For ZP

Inactive due to College and Studies
abdul-rehman is offline
Send a message via Yahoo to abdul-rehman Send a message via Skype™ to abdul-rehman
meTaLiCroSS
Gaze Upon My Hat
Join Date: Feb 2009
Location: Viña del Mar, Chile
Old 12-19-2010 , 19:07   Re: [HOWTO] Make a NPC with extra features.
Reply With Quote #5

PHP Code:
        static Floatorigin[3], Floatmins[3], Floatmaxs[3]; 
        
pev(iEntpev_originorigin); 
        
pev(iEntpev_minsmins); 
        
pev(iEntpev_maxsmaxs); 
         
        
mins[0] += origin[0
        
mins[1] += origin[1
        
mins[2] += origin[2
        
maxs[0] += origin[0
        
maxs[1] += origin[1
        
maxs[2] += origin[2
Use "pev_absmin" and "pev_absmin" instead of adding his current mins and max value to his current origin.

Why using NPC_IdleAnimations with chars, when you can perfectly use it with simples integers?
__________________
Quote:
Originally Posted by joropito View Post
You're right Metalicross
meTaLiCroSS is offline
Seta00
The Seta00 user has crashed.
Join Date: Jan 2010
Location: Berlin
Old 12-19-2010 , 21:09   Re: [HOWTO] Make a NPC with extra features.
Reply With Quote #6

pev_absmin and pev_absmax **
Seta00 is offline
Mini_Midget
Veteran Member
Join Date: Jan 2006
Location: It's a mystery.
Old 12-20-2010 , 03:32   Re: [HOWTO] Make a NPC with extra features.
Reply With Quote #7

@Arkshine
Thanks for that! Didn't realize that function works with floats.
Do you mean I should not use RegisterHamFromEntity() at all or just once?
Something like this?
PHP Code:
public plugin_init()
{
    
register_plugin("NPC Plugin""1.0""Mazza");
    
register_clcmd("say /npc""clcmd_npc");
    
    
RegisterHam(Ham_TakeDamage"info_target""npc_TakeDamage");
    
RegisterHam(Ham_Killed"info_target""npc_Killed");
    
RegisterHam(Ham_Think"info_target""npc_Think");
    
RegisterHam(Ham_TraceAttack"info_target""npc_TraceAttack");

Would I still need to filter out the classname for each of them inside the forwards?

@bibu
I'm quite sure there is a tutorial on how the touch forward works. That's why I didn't really bother with it. If there is much requests for this feature, I'll write it up.
As you said, giving a NPC a weapon is already in the other tutorial so I found no need to simply just copy and paste it in here.

@meTaLiCroSS
Mmm... I had problems last night with those pev values... But fixed now.
Silly me! Was working on this quite late last night and wasn't thinking straight.
__________________
It's a mystery.
Mini_Midget is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 12-20-2010 , 03:41   Re: [HOWTO] Make a NPC with extra features.
Reply With Quote #8

Quote:
Originally Posted by Mini_Midget View Post
Would I still need to filter out the classname for each of them inside the forwards?
Yes.
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
abdul-rehman
Veteran Member
Join Date: Jan 2010
Location: Khi, Pakistan
Old 12-20-2010 , 04:24   Re: [HOWTO] Make a NPC with extra features.
Reply With Quote #9

Quote:
Originally Posted by Exolent[jNr] View Post
Yes.
Quote:
Originally Posted by Mini_Midget View Post
@Arkshine
Thanks for that! Didn't realize that function works with floats.
Do you mean I should not use RegisterHamFromEntity() at all or just once?
Something like this?
PHP Code:
public plugin_init()
{
    
register_plugin("NPC Plugin""1.0""Mazza");
    
register_clcmd("say /npc""clcmd_npc");
    
    
RegisterHam(Ham_TakeDamage"info_target""npc_TakeDamage");
    
RegisterHam(Ham_Killed"info_target""npc_Killed");
    
RegisterHam(Ham_Think"info_target""npc_Think");
    
RegisterHam(Ham_TraceAttack"info_target""npc_TraceAttack");

Would I still need to filter out the classname for each of them inside the forwards?

@bibu
I'm quite sure there is a tutorial on how the touch forward works. That's why I didn't really bother with it. If there is much requests for this feature, I'll write it up.
As you said, giving a NPC a weapon is already in the other tutorial so I found no need to simply just copy and paste it in here.

@meTaLiCroSS
Mmm... I had problems last night with those pev values... But fixed now.
Silly me! Was working on this quite late last night and wasn't thinking straight.
Both ov you confused me

The class name of an entity is what we set through pev_classname, right ?
So in order for registering the ham_forwards we will use "npc_ent" as the classname ??

And if we use info_target as the class name then if we check the classname in the forward, it would return "info_target" instead of "npc_ent"

BTW what is the actuall class name of the NPC ?? is it "npc_ent" or "info_target" ??
__________________

My Plugins For ZP

Inactive due to College and Studies
abdul-rehman is offline
Send a message via Yahoo to abdul-rehman Send a message via Skype™ to abdul-rehman
Mini_Midget
Veteran Member
Join Date: Jan 2006
Location: It's a mystery.
Old 12-20-2010 , 04:50   Re: [HOWTO] Make a NPC with extra features.
Reply With Quote #10

Yes. EV_SZ_classname/pev_classname is the name of our ent.
No. You have to pass "info_target" or you will run into this error.
Code:
[HAMSANDWICH] Failed to retrieve classtype for "npc_ent", hook for "npc_Think" not active.
I believe there is already an ent created named "info_target" at the start of every game. That's why we pass "info_target" into create_entity function.
Add this somewhere in your npc_Think method and you'll understand what we're talking about.
PHP Code:
    static className[32];
    
entity_get_string(iEntEV_SZ_classnameclassNamecharsmax(className))
    
    
client_print(0print_chat"Classname: %s"className); 
The actual classname for our NPC is "npc_ent".
__________________
It's a mystery.
Mini_Midget is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 03:50.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode