[Request] NPC Text thing.

I would like it if someone could make it so it displays the number of NPC kills they got
on the bottom left corner. and each time they kill a npc +1 pops up over the text and
moves up and fades away.

Thanks ~

Not tested, but should work for the server sided part.
The client side I can PM you, it will take a while though.
It would be better if you did the client side so you can make it look how you want.
I can help if you want though.
math.Approach() is good for fading if it is hooked to a think or timer. Preferable think hook.



-- Server sided code
local function CountNPCKills( npc, attacker, inflictor )
	
	if !attacker:IsPlayer() or !attacker:IsValid() then return end -- Make sure our attacker is a player and is valid.
	if !attacker:GetNWInt( "NPCKills" ) then !attacker:SetNWInt( "NPCKills", 0 ) end -- Make sure that there is a Networked Integer so both client and server can call it
	
	attacker:SetNWInt( "NPCKills", attacker:GetNWInt( "NPCKills" ) + 1 ) -- Add our kill
	
end
hook.Add( "OnNPCKilled", "NPCKillCounter", CountNPCKills )


Can I add you on steam?

Sure, bluekirby950

don’t use BlueKirbys. Mine has more features, and doesn’t use NetworkedVariables.

[lua]
if( SERVER ) then
hook.Add(“OnNPCKilled”, “KillCounter”, function(victim, attacker, wep)
if ( !victim:IsValid() || !attacker:IsValid() || !attacker:IsPlayer() ) then return; end

	if( attacker.NPCKillCount ) then
		attacker.NPCKillCount = attacker.NPCKillCount + 1;
	else
		attacker.NPCKillCount = 1;
	end
	
	umsg.Start("KillCountIncreased", attacker);
		umsg.Char( attacker.NPCKillCount );
		umsg.String( tostring( victim:GetClass() ) );
		umsg.String( tostring( wep:GetClass() ) );
	umsg.End();
end)

end

if( CLIENT ) then
LocalPlayer().NPCKillCount = 0;

usermessage.Hook("KillCountIncreased", function( data )
	hook.Call("PlayerKillCountChanged", GM, data:ReadChar(), data:ReadString(), data:ReadString());
end)

hook.Add("PlayerKillCountChanged", "ChangeNPCKillCount", function(kCount, victim, weapon)
	LocalPlayer().NPCKillCount = kCount;
	
	chat.AddText(Color(127, 127, 127), "You killed "..victim.."!");
end)

hook.Add("HUDPaint", "DrawKills", function()
	if( LocalPlayer().NPCKillCount ) then
		draw.SimpleText(tostring( LocalPlayer().NPCKillCount ), "TargetID", 2, 2, Color(0, 0, 0));
	end
end)

end[/lua]

I added you. >:P

[editline]16th November 2011[/editline]

Oh sick!, ima test this right now. hold on

Make sure to put it in lua/autorun. Not lua/autorun/client or lua/autorun/server. Just lua/autorun.

[editline]15th November 2011[/editline]

Also, if you need more features added, tell me and I will add them.

I added you c-unit

-snip- Rate me dumb

Adding features is what does make it better.

What I mean is he added unnecessary crap. Like why do you need the weapon you killed the entity with? NW variables are better for this because you can see other people’s kills too and not just yours.

And why would you have it call a hook? Why not make a new function?


 usermessage.Hook("KillCountIncreased", function( data )
        hook.Call("PlayerKillCountChanged", GM, data:ReadChar(), data:ReadString(), data:ReadString());
    end)
     
    hook.Add("PlayerKillCountChanged", "ChangeNPCKillCount", function(kCount, victim, weapon)
        LocalPlayer().NPCKillCount = kCount;
         
        chat.AddText(Color(127, 127, 127), "You killed "..victim.."!");
    end)

Blue Kirby, less networking is better. My set up is better for multiple reasons.

  • No NetworkedInt’s, which means there is less networking, which takes less of a performance toll.
  • More features means there is a lot more you can do with it.
  • Calling a hook and passing data is a good method, because it keeps the coding looking nice and you can reference that hook elsewhere too rather then just that file.

Nothing in my code is useless, besides networking the inflicter, since it will always return “Player”. Your’s just does less and will have much more networking.

Functions can be called anywhere.



if SERVER then
	
	local function NPCKillCounter( npc, attacker, inflictor )
		
		if !attacker:IsPlayer() or !attacker:IsValid() or !npc:IsValid() then return end
		if !attacker:GetNWInt( "NPCKillCounter" ) then attacker:SetNWInt( "NPCKillCounter", 0 ) end --NetworkedInteger so other players can call
		class = npc:GetClass()
		
		umsg.Start( "NPCKillCounter", attacker )
			umsg.String( class )
		umsg.End()
		
	end
	
	hook.Add( "OnNPCKilled", "NPCKillCounter", NPCKillCounter )
	
end

if CLIENT then
	
	local alpha = 0
	local nextfade = 0
	local class = "ERROR: NO CLASS"
	
	local function NPCKillCounter( classdata )
		class = classdata:ReadString()
		alpha = 255
		nextfade = CurTime() + 3
	end
	
	usermessage.hook( "NPCKillCounter", NPCKillCounter )
	
	hook.Add( "HUDPaint", "NPCKillCounter.hud", function()
		
		surface.SetFont( "Trebuchet22" )
		local width = surface.GetTextSize( "You killed "..class.."!" )
		local adjwidth = width / 2
		surface.SetTextColor( 200, 200, 200, alpha )
		surface.SetTextPos( ScrW() / 2 - adjwidth, ScrH() / 2 - 100 ) 
		surface.DrawText( "You killed "..class.."!" )
		
		width = surface.GetTextSize( "Kills: "..LocalPlayer():GetNWInt( "NPCKillCounter" ) )
		adjwidth = width / 2
		surface.SetTextPos( 0, ScrH() - 100 ) 
		surface.SetTextColor( 200, 200, 200, 255 )
		surface.DrawText( "Kills: "..LocalPlayer():GetNWInt( "NPCKillCounter" ) )
		
		if nextfade > CurTime() then return end
		
		alpha = math.Approach( alpha, 0, 3 )
		
	end )
	
end


Yeah, functions can be called anywhere. However yours cannot be, due to the fact that it’s local. So now you are doing even more networking, taking away features, and doing more calculations per frame.

There is no reason for mine to be called anywhere else. Your code is not perfect, stop pretending it is.

You just said ‘functions can be called anywhere’ but you were proven wrong and all you have to say is that his code isn’t perfect?

Functions can be called anywhere, when did I say mine could be? Rating kids dumb doesn’t do anything xD

It’s pretty obvious that you don’t know what you are talking about. My code is better for this situation, it’s more efficient , and cleaner. Enough said, no need to fight over this.

Just call LocalPlayer().NPCKillCount on client, (whatever player variable you use).NPCKillCount on server.