Med Pack and Ammo Pack Entity Help

Okay, so I am trying to make 2 custom entities. A MedPack That Dispenses 50 Health, that can be used 10 times, and every player has to wait 30 seconds to use it again, and an Ammo Pack, that does the same thing, except instead of health it gives ammo for all the weapons he has in his inventory. I can’t find a custom entity tutorial on google, so i look here for help.

Took me about 3 seconds to find that.

This doesnt show how to make one. Like setting up the shared.lua and stuff like that. Keep in mind this: http://www.element14.com/community/servlet/JiveServlet/showImage/38-15476-194774/i-have-no-idea-what-im-doing-dog.jpg?01AD=3mhiKJI5gTFhY6r3wTsJ_-Pq-WmyyjEOGeuiIAyVqWIHTALRNdD5jTA&01RI=FFEFAE50968B002&01NA=na

Just modify this: https://github.com/garrynewman/garrysmod/blob/master/garrysmod/lua/weapons/weapon_medkit.lua

No. Im trying to make one as you throw it down on the ground. Kinda like this. https://www.youtube.com/watch?v=dXJ0V5f-9qU (Best Vid I could find of throwing down medpack in bf3)

Then just make the SWEP release an HL2 health pack instead.

So, you can have a SWEP, and when you left/right click to spawn an entity in front of you and have it drop to the ground ( Wake the physics object ).

Look at ENT:Use / ENT:AcceptInput for when the player presses E on it, they will get health. You can either remove the thing then, or have it stay in the same spot, or have the original owner pick it up with ALT + E or so ( just give the player another swep with the proper value of charges left ).

Alternatively, you can just have your swep drop up to 10 then remove it; the swep never leave you.

Additionally, for the entity on the ground you could use ENT:Touch ( or OnTouch ) to give the health to the person.

Ok. Ill try and do that. But is there a way i can make it a different model?

[editline]13th June 2014[/editline]

Oh wait, but what about the ammo pack?

Of course.

When you spawn the entity/medpack to fall to the ground ( note, you can use the same entity for different levels of healing, etc ), just set the model and if you choose to code it to make it adjustable so you only need 1 entity for unlimited number of healing combinations, update the heal-amount ( serverside ).

You can make it use its own ammo, non-refillable, refillable, etc… There are ways to add custom ammo; you can even create your own ammo entity where when you ENT:Touch it, it replenishes your medkit; walk over bandages and pick them up to give +3 to your ammo count.

I want it so when i hit E on it it replenishes several different types of ammo. So I have the medkit working, so shouldnt i just create a custom entity that does that and make it so that the ammokit swep dispenses thatt instead of a medkit? and how would i make an entity and make it so it gives ammo for several dif. types of ammo when you click E and then dissapear?

Keep in mind that i am very new to LUA, and am trying to make sense of how to get that finite ammo source working for my medkit. (after i get this working and finite ammo it will be ready for release. My first addon release :D. With tons of help from you guys)

It would go something like this:

[lua]function ENT:Initialize( )
// Other stuff

// Important, set use type to simple so it doesn't spam.. 1 click = 1 use
self:SetUseType( SIMPLE_USE );

end

function ENT:Use( _p )
// Go through all weapons the player is carrying and give ammo for each ammo type
for k, _w in pairs( _p:GetWeapons( ) ) do
// Get the weapon table for the weapon ( not all weapons have this such as HL2 weapons )
local _tab = _w:GetTable( );

	// Make sure the tables and vars exists
	if ( !istable( _tab ) || !_tab.Primary || !_w.GetPrimaryAmmoType || !_tab.Primary.DefaultClip ) then continue; end

	// Ammo Type - If Primary.Ammo is not set set to none - we check above to make sure the function exists, but it doesn't mean it'll return a good value
	local _ammoType = _w:GetPrimaryAmmoType( ) || "none";

	// If the ammo type is "none" skip
	if ( _ammoType == "none" ) then continue; end

	// Max Ammo - if ClipMax isn't set, assume 60 is the max
	local _maxAmmo = _w.Primary.ClipMax || 60;

	// Max Ammo to give
	local _ammoRefill = 30;

	// Current Ammo Count
	local _currentAmmo = _p:GetAmmoCount( _ammoType );

	// Give the player some ammo for this weapon, if they can hold more
	if ( _currentAmmo < _maxAmmo ) then
		// Make sure to only give the player UP TO the max amount they can hold, no more
		_p:GiveAmmo( math.Clamp( _currentAmmo + _ammoRefill, 0, _maxAmmo  ), _ammoType );
	else
		// Skip the weapon, can't hold any more ammo
		continue; -- This technically isn't needed since it is the end of the for, but if something is added after which does something else with specific ammo you'll want it.. or you can just avoid giving ammo and won't need a continue but then the full for will be logically processed
	end
end

// Remove this medkit/resupply thing
SafeRemoveEntity( self );

end[/lua]

This should work; untested but it should work. Some SWEP variables may be different, you may need to adapt it. I put many more comments than typical to help.

With Health:
https://dl.dropboxusercontent.com/u/26074909/tutoring/entities/ammo_dispenser_entity.lua.html

remove .html to see the .lua file

Okay, I get no errors at all but the entity for the deployed ammo pack just sits still, doing nothing. When i click E on it nothing happens. How do i fix this? and also the model i have has multiple skins and i want it to have skin 1 how do i change that also?

You need to do more in Initialize instead of just setting the use-type. Can you show us the full code?

Sure.

init.lua



AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )  

include( "shared.lua" )

function ENT:Initialize( )
	// Other stuff
	self.Entity:SetModel("models/bloocobalt/l4d/items/w_eq_fieldkit.mdl")

	// Important, set use type to simple so it doesn't spam.. 1 click = 1 use
	self:SetUseType( SIMPLE_USE );
end

function ENT:Use( _p )
	// Go through all weapons the player is carrying and give ammo for each ammo type
	for k, _w in pairs( _p:GetWeapons( ) ) do
		// Get the weapon table for the weapon ( not all weapons have this such as HL2 weapons )
		local _tab = _w:GetTable( );

		// Make sure the tables and vars exists
		if ( !istable( _tab ) || !_tab.Primary || !_w.GetPrimaryAmmoType || !_tab.Primary.DefaultClip ) then continue; end

		// Ammo Type - If Primary.Ammo is not set set to none - we check above to make sure the function exists, but it doesn't mean it'll return a good value
		local _ammoType = _w:GetPrimaryAmmoType( ) || "none";

		// If the ammo type is "none" skip
		if ( _ammoType == "none" ) then continue; end

		// Max Ammo - if ClipMax isn't set, assume 60 is the max
		local _maxAmmo = _w.Primary.ClipMax || 60;

		// Max Ammo to give
		local _ammoRefill = 30;

		// Current Ammo Count
		local _currentAmmo = _p:GetAmmoCount( _ammoType );

		// Give the player some ammo for this weapon, if they can hold more
		if ( _currentAmmo < _maxAmmo ) then
			// Make sure to only give the player UP TO the max amount they can hold, no more
			_p:GiveAmmo( math.Clamp( _currentAmmo + _ammoRefill, 0, _maxAmmo  ), _ammoType );
		else
			// Skip the weapon, can't hold any more ammo
			continue; -- This technically isn't needed since it is the end of the for, but if something is added after which does something else with specific ammo you'll want it.. or you can just avoid giving ammo and won't need a continue but then the full for will be logically processed
		end
	end

	// Remove this medkit/resupply thing
	SafeRemoveEntity( self );
end


cl_init.lua



include('shared.lua')

function ENT:Draw()

    self.Entity:DrawModel()
 
end


shared.lua



ENT.Type = "anim"
ENT.Base = "base_gmodentity"
 
ENT.PrintName		= "Ammo Pack Deployed"
ENT.Author			= "Overki11"
ENT.Contact			= ""
ENT.Purpose			= ""
ENT.Information		= "Gives Ammo for All Weapons In Inventory"
ENT.Category		= "Other"

ENT.Spawnable			= true
ENT.AdminSpawnable		= true