Gmod character dynamics...

Hi guys!
I want to create a hunger dynamic which can be solved by using an entity with a can model and/or a “meat’d” dynamic where if you’re hit by a shotgun, if you don’t use a toilet paper entity in 10 seconds you die. could someone help me out with hooks or tutorials on things like this?
Thanks!

[lua]
function GM:EntityTakeDamage( ent, inflictor, attacker, amount )
if (ent:IsPlayer() and attacker:IsPlayer()) then
if attacker:GetActiveWeapon():GetClass() == “weapon_shotgun” then
timer.Create(“KillShotVictim”…ent:UniqueID(), 10, 1, function(ply)
ply:Kill()
end
end
end
end
[/lua]

//Entity
[lua]
function ENT:Use( activator, caller )

self:Remove()

if ( activator:IsPlayer() ) then
if timer.IsTimer("KillShotVictim"..activator:UniqueID()) then
	timer.Remove("KillShotVictim"..activator:UniqueID())
end

end

end
[/lua]

I would appreciate if you said thank you atleast. I’ve helped you two times and you havn’t replied back to say if it was helpful or not.

That script isn’t supposed to be exactly what you are looking for. Modify it to how you want it.

Thanks! Sorry I was getting a traffic message from the site.
Cya 'round!

Hey, I started on this for you last night. At the moment it only handles bleeding and it lacks effects but it sends your console a message when you bleed!

To add a new item you should use this function HHM.AddItem(name, class, model, points, uses) and the enumerations defined in the script as the ‘class’.

Anyways here you go:

Server-Side


[lua]
–_______________________________________–
–Define–

HHM = {}; – Hunger/Health Mod
HHM.ItemTab = {};
HHM.PlayerTab = {};

--Enums--

HHM.BANDAGE = 1;
HHM.FIRSTAID = 2;

HHM.FOOD = 3;
HHM.DRINK = 4;

--Vars--

HHM.BleedAmt = 1;

--Utilities--

function HHM.AddItem(name, class, model, points, uses)

HHM.ItemTab[name] = { Class = class, Points = points, Model = model, Uses = uses };

end

function HHM.QueryModelToItem(name, model)

--this

end

function HHM.IsItem(name)

if !HHM.ItemTab[name] then
	return false;
end
return true;

end

function HHM.GetPlayerInfo(ply)

return HHM.PlayerTab[ply:SteamID()];

end

function HHM.CreateItem(name, pos)

local ItemDef = HHM.ItemTab[name]; 
local ent = ents.Create("prop_physics");
	ent:SetPos(pos);
	ent:SetModel( ItemDef.Model );
	
ent:Spawn();
ent:SetVar("Info", { Name = name, Uses = ItemDef.Uses } );

end

--Items--

HHM.AddItem("Small Bandage", HHM.BANDAGE, "models/props_junk/garbage_bag001a.mdl", 10, 1)

--Meta Functions--

local PLAYER = FindMetaTable(“Player”);

function PLAYER:GetPlayerInfo()

return HHM.PlayerTab[self:SteamID()];

end

function PLAYER:SetHunger(int)

local info = self:GetPlayerInfo()
	info.Hunger = int;
	
HHM.PlayerTab[self:SteamID()] = info;

end

function PLAYER:Hunger()

return HHM.PlayerTab[self:SteamID()].Hunger;

end

function PLAYER:SetThirst(int)

local info = self:GetPlayerInfo()
	info.Thirst = int;
	
HHM.PlayerTab[self:SteamID()] = info;

end

function PLAYER:Thirst()

return HHM.PlayerTab[self:SteamID()].Thirst;

end

function PLAYER:IsBleeding()

if self:GetPlayerInfo().Bleed == true then
	return true;
end
return false;

end

function PLAYER:Bleed()

if self:IsBleeding() then

	print("Ouch, I'm bleeding!");
	self:SetHealth( self:Health() - HHM.BleedAmt );
	HHM.PlayerTab[self:SteamID()].Blood = HHM.GetPlayerInfo(self).Blood - ( HHM.GetPlayerInfo(self).BleedAmt );
	
	timer.Simple( 5, self.Bleed, self );
end

self:RefreshBloodStats();

end

function PLAYER:PickupItem(item)

local iteminfo = item:GetVar("Info");
local inv = HHM.PlayerTab[self:SteamID()].Inv;

item:Remove();

if !inv[iteminfo.Name] then 

	inv[iteminfo.Name] = iteminfo.Uses; 
	HHM.PlayerTab[self:SteamID()].Inv = inv;
	
else

	inv[iteminfo.Name] = inv[iteminfo.Name] + iteminfo.Uses; 
	HHM.PlayerTab[self:SteamID()].Inv = inv;

end

self:RefreshInv();

end

–Optimize this
function PLAYER:UseItem(itemname)

local plytab = HHM.PlayerTab[self:SteamID()];
local ItemInfo = HHM.ItemTab[itemname]


if !plytab.Inv[itemname] or plytab.Inv[itemname] <= 0 then self:RefreshInv(); return; end -- Player doesn't own the item, so he can't use this function.
	
	if ItemInfo.Class == HHM.BANDAGE then
		if plytab.BleedAmt <= 0 then HHM.PlayerTab[self:SteamID()].BleedAmt = 0; return; end
		HHM.PlayerTab[self:SteamID()].BleedAmt = plytab.BleedAmt - ItemInfo.Points;
		self:RefreshBloodStats();
		if plytab.BleedAmt < 100 then HHM.PlayerTab[self:SteamID()].Bleed = false; end
		--end effects
	elseif ItemInfo.Class == HHM.FIRSTAID then
		ply:SetHealth( ply:Health() + ItemInfo.Points );
		--start effects
	elseif ItemInfo.Class == HHM.FOOD then
		ply:SetHunger( ply:Hunger() - ItemInfo.Points );
		--start effects
	elseif ItemInfo.Class == HHM.DRINK then
		ply:SetThirst( ply:Thirst() - ItemInfo.Points );
		--start effects
	end
	
plytab.Inv[itemname] = plytab.Inv[itemname] - 1;
if plytab.Inv[itemname] <= 0 then plytab.Inv[itemname] = nil; end
HHM.PlayerTab[self:SteamID()].Inv = plytab.Inv;

self:RefreshInv();

end

function PLAYER:RefreshInv()

for key, value in pairs(HHM.PlayerTab[self:SteamID()].Inv) do
	umsg.Start( "HHM.RefreshInv", self );
	
		umsg.String( key );
		umsg.Long( value );

	umsg.End();
end

end

function PLAYER:RefreshBloodStats()

umsg.Start( "HHM.Bleed", self );

	umsg.Bool( HHM.GetPlayerInfo(self).Bleed );
	umsg.Long( HHM.GetPlayerInfo(self).BleedAmt );
	umsg.Long( HHM.GetPlayerInfo(self).Blood );


umsg.End();

end

--Hooks--

hook.Add(“KeyPress”, “HHM.KeyPress”, function(ply, key)
local tr = util.QuickTrace(ply:GetShootPos(), ply:GetAimVector()*120, {ply});

if key == IN_USE && IsValid(tr.Entity) && HHM.IsItem(tr.Entity:GetVar("Info").Name) then
	print("HHM- Item picked up!");
	ply:PickupItem(tr.Entity);
end

end);

hook.Add(“EntityTakeDamage”, “HHM.EntityTakeDamage”, function(ent, inflictor, attacker, amount, dmginfo)

local dmgtype = dmginfo:GetDamageType();


if ent:IsPlayer() then
		
	if dmgtype == DMG_CRUSH or dmgtype == DMG_BULLET or dmgtype == DMG_SLASH or dmgtype == DMG_BURN then
	
		HHM.PlayerTab[ent:SteamID()].BleedAmt = HHM.PlayerTab[ent:SteamID()].BleedAmt + amount;
		ent:RefreshBloodStats();
	
		if HHM.GetPlayerInfo(ent).Bleed == true then return; end --Just to assure we don't become redundant.
		HHM.PlayerTab[ent:SteamID()].Bleed = true;
		ent:Bleed();
		--Add Bleed type (large, medium, small wound)
	end
	
end

end);

hook.Add(“PlayerInitialSpawn”, “HHM.PlyInitSpawn”, function(ply)

print("HHM - Player info created!");
HHM.PlayerTab[ply:SteamID()] = { Bleed = false, BleedAmt = 0, Blood = 12000, Hunger = 100, Thirst = 100, Inv = {} };

end);

hook.Add( “PlayerDeath”, “HHM.PlyDeath”, function( ply, weapon, killer )

print("HHM - Player info reset!");
HHM.PlayerTab[ply:SteamID()] = { Bleed = false, BleedAmt = 0, Blood = 12000, Hunger = 100, Thirst = 100, Inv = {} };

end);

--Commands--

concommand.Add(“HHM.createitem”, function(ply, cmd, args)

local Argument = args;

if #args > 1 then
	Argument = table.concat( Argument, " " );
end


if ply:IsAdmin() then
	HHM.CreateItem( Argument, util.QuickTrace(ply:GetShootPos(), ply:GetAimVector()*120, {ply}).HitPos );
end

end);

concommand.Add(“HHM.useitem”, function(ply, cmd, args)

local Argument = args;

if #args > 1 then
	Argument = table.concat( Argument, " " );
end


ply:UseItem(Argument)

end);

[/lua]

Client-Side


[lua]

if CLIENT then
end

HHM = {};
HHM.ItemTab = {};
HHM.MYTAB = { Bleed = false, BleedAmt = 0, Blood = 12000, inv = {} };

--Hooks--

usermessage.Hook( “HHM.RefreshInv”, function(data)

--local Item, Amt = data:ReadString(), data:ReadLong();

HHM.MYTAB.inv[data:ReadString()] = data:ReadLong();

end);

usermessage.Hook( “HHM.Bleed”, function(data)

--local Bleed, BleedAmt, Blood = data:ReadBool(), data:ReadLong(), data:ReadLong();

HHM.MYTAB.Bleed, HHM.MYTAB.BleedAmt, HHM.MYTAB.Blood = data:ReadBool(), data:ReadLong(), data:ReadLong();

end);
/*
–Important:
NEXTLINE throws it and every other item after it to the next line.
*/
HHM.PanelClasses = {
[1] = {
NAME = “‘Debug’”, FUNC = function() return “”; end,
NEXTLINE = false
},
[2] = {
NAME = “BANDAGE”, FUNC = function() return HHM.GetAmount(“Small Bandage”); end,
NEXTLINE = false
},
[3] = {
NAME = “BLOOD”, FUNC = function() return HHM.MYTAB.Blood; end,
NEXTLINE = false
} ,
[4] = {
NAME = “BLEED- AMT/BOOL”, FUNC = function() return HHM.MYTAB.BleedAmt…"/"…tostring(HHM.MYTAB.Bleed); end,
NEXTLINE = true
}

};

hook.Add(“HUDPaint”,“HHM.DrawHud”, function()
–Ricka-ricka-refresh

local StartX, StartY = 1250, 900;
local x,y = StartX, StartY;
local NextX, NextY = -100, -50;

for i = 1, #HHM.PanelClasses do

	if HHM.PanelClasses*.NEXTLINE then
		x = StartX;
		y = y + NextY;
	end

draw.WordBox( 8, ScrW() - x, ScrH() - y, HHM.PanelClasses*.NAME..":"..HHM.PanelClasses*.FUNC(),"ScoreboardText",Color(255,255,0,0),Color(255,255,255,255));

	x = x + NextX;

end

end);

--Util--

function HHM.GetAmount(item)

return HHM.MYTAB.inv[item] or 0;

end


– CURSOR UTIL –

function gui.CursorInArea( x, y, w, h, mx, my )

if( MenuOn == false ) then return false end

local x2 = x + w;
local y2 = y + h;

local mx = mx;
local my = my;

if( not mx ) then

	mx, my = gui.MousePos();


end


if( mx > x and mx < x2 ) then


	if( my > y and my < y2 ) then
		
		return true;
	
	end

end

return false;

end

--Commands--

--Effects--

--Highlight items

[/lua]

Thanks so much man!

[editline]26th July 2012[/editline]

Hey i changed the file a bit, where should i put it in my gamemodes folder?
Eg. Content or something

Thanks :slight_smile:

You can put it in any server sided file in the sandbox gamemode located in garrysmod/gamemodes or in any custom gamemode that you’re running since I haven’t started on effects- because I have no idea what to do.

Hm… I’d like to see more of this… I like the idea of realistic injuries and survival aspects.

I updated my original post, I’m planning on adding onto this and expanding your idea. At the moment only bleeding works items are used and created through console commands so at the moment this is pretty barebones and client un-friendly.

I’d love to hear some feedback!

Instructions:

-Installation-
Server-Side- into an init.lua file located in the sandbox gamemode which is also located in garrysmod/gamemodes or in any custom gamemode that you’re running.

Client-Side- into an cl_init.lua

-Server Use-
hhm.CreateItem Small Bandage

hhm.UseItem Small Bandage

Hmm… You think maybe you could have them in a Swep? Like Bandages, Water, Food, etc.? I can try to dig up some current Sweps on garrysmod.org so you can use the view model of it.