Tutorial *49*
Today we gona learn how to make a PlayerID function. It's a pretty good tutorial, cause you can use this function do do more than just check a players name (for example making a medic or something else cool). But Today we will do a PlayerID.
The first thing we need to do is to make a new file called "playerid.qc". Now, place this into that file:


void() PlayerID =

{

	local	vector	src;

	string	trackp;



	src = self.origin + v_forward*10;

	src_z = self.absmin_z + self.size_z * 0.7;

	traceline (src, src + v_forward*1024, FALSE, self);	// We are tracing a line from your view and to the first object that we hit (note the limit of 1024 units)

	if (trace_ent != world && trace_ent.origin != world.origin )   // Check so this object is not the world

	{

		if (trace_ent.classname == "player" && trace_ent.health > 0)  // If it's not the world, is it a player?

		{



				trackp = ftos(trace_ent.health);    // ftos (Float To String), or else we can't display the health with centerprint

				centerprint(self, "Name: ",trace_ent.netname, "\nHealth: ", trackp);  // Print the player's netname (name) and health

		}

	}

};

Close that file. Now that was pretty damn simple, don't you think? Now we need to run this function. First, open up the "defs.qc" file, and at the very bottom, add this line:


.float	idupdate;

Now close that file, and open up the "client.qc" file. Go down to the "ClientConnect" function and add there, after the line bprint (PRINT_HIGH, " entered the game\n");


self.idupdate = 0;

This set's the idupdate variable to 0 when you connect. Now goto the "PlayerPreThink" function, and after the line "local float r;" add this:


if (self.idupdate < time)

{

	self.idupdate = time + 1;

	PlayerID();

}

What this does, is simply to check if the time is later than self.idupdate, if it is, run the PlayerID function, and set the idupdate to 1 second more than the current time. This will make it update the PlayerID once every second. If you try to compile now, it will not work, because we have the Playerid.qc file after the client.qc file. Then goto the top of client.qc file and just under the line "void (vector org, entity death_owner) spawn_tdeath;" add


void() PlayerID;

Now, before you compile, be sure to check the "progs.src" in notepad, and that it says "playerid.qc" at the end of it. Compile, and enjoy your new PlayerID function :)


 
Not logged in
Sign up
Login:
Passwd: