Flash Light Battery

I am currently working on a gamemode for my ‘Horror map’ server which essentially makes horror maps like gm_ghosthunt scarier. I have gotten the base of the gamemode working but need to add a flashlight battery that recharges overtime, just like the one from Half Life.
Thanks :slight_smile:

[lua]if !SERVER then return; end;

local flashlight_minimum = 1; – The minimum amount of battery a player must have to turn/keep on a flashlight
local flashlight_interval = 1; – The interval in seconds between a regen or drain
local flashlight_regen_per_interval = 1; – The amount the battery will regenerate per interval
local flashlight_drain_per_interval = 5; – The amount the battery will drain per interval
local flashlight_maximum = 100; – The highest battery level

hook.Add(“PlayerSwitchFlashlight”, “DisableFlashlightOnNoBattery”, function(ply, on)
if(ply._BatteryLevel <= flashlight_minimum)then
return not on;
end;
end);

hook.Add(“Think”, “DrainRegenBattery”, function()
for _, ply in pairs(player.GetAll())do
if(ply:FlashlightIsOn() and (!ply._NextBatteryDrain or ply._NextBatteryDrain < CurTime()))then
ply._BatteryLevel = math.Clamp((ply._BatteryLevel or flashlight_maximum) - flashlight_drain_per_interval, 0, flashlight_maximum);
ply._NextBatteryDrain = CurTime() + flashlight_interval;

		if(ply._BatteryLevel &lt;= flashlight_minimum)then
			ply:Flashlight(false);
		end;
	elseif(ply._BatteryLevel &lt; flashlight_maximum and !ply:FlashlightIsOn() and (!ply._NextBatteryRegen or ply._NextBatteryRegen &lt; CurTime()))then
		ply._BatteryLevel = math.Clamp((ply._BatteryLevel or flashlight_maximum) + flashlight_regen_per_interval, 0, flashlight_maximum);
		ply._NextBatteryRegen = CurTime() + flashlight_interval;
	end;
end;

end);[/lua]

Something like this should work, it’s only server-side and thus adds nothing on your screen to show how much of the battery you have left.

Okay, thats a great help.

Quick question, where abouts would I put this? Just making sure so I don’t screw up my server :slight_smile:

In a server-side part of your code. Or if you don’t want it in your gamemode you can put it in lua/autorun/server/filename.lua or lua/autorun/filename.lua either will work

Okay, thanks :slight_smile:

I finally got my gamemode playable and inserted the flashlight code into init.lua, when you join or load the gametype/new map you get this error:
Hook ‘DrainRegenBattery’ Failed: [gamemodes\horrorventures\gamemode\init.lua:70] attempt to compare nil with number

When you turn the flash light on you get this error:
Hook ‘DisableFlashlightOnNoBattery’ Failed: [gamemodes\horrorventures\gamemode\init.lua:56] attempt to compare nil with number

Any idea what the fix is?

Well when I uploaded the code and played it on my server I didn’t get the errors but the battery doesn’t seem to work.

Did you keep those local variables within scope? Show me the implementation and I’ll tell you what is wrong.

-Removed my super secret code-

My bad, I see what I did wrong now. I completely forgot to initialize the _BatteryLevel variable and started comparing it.

This should work, though I have not tested it:
[lua]if !SERVER then return; end;

local flashlight_minimum = 1; – The minimum amount of battery a player must have to turn/keep on a flashlight
local flashlight_interval = 1; – The interval in seconds between a regen or drain
local flashlight_regen_per_interval = 1; – The amount the battery will regenerate per interval
local flashlight_drain_per_interval = 5; – The amount the battery will drain per interval
local flashlight_maximum = 100; – The highest battery level

hook.Add(“PlayerInitialSpawn”, “SetFlashLightBattery”, function(ply)
ply._BatteryLevel = flashlight_maximum;
end);

hook.Add(“PlayerSwitchFlashlight”, “DisableFlashlightOnNoBattery”, function(ply, on)
if(ply._BatteryLevel <= flashlight_minimum)then
return not on;
end;
end);

hook.Add(“Think”, “DrainRegenBattery”, function()
for _, ply in pairs(player.GetAll())do
if(ply:FlashlightIsOn() and (!ply._NextBatteryDrain or ply._NextBatteryDrain < CurTime()))then
ply._BatteryLevel = math.Clamp(ply._BatteryLevel - flashlight_drain_per_interval, 0, flashlight_maximum);
ply._NextBatteryDrain = CurTime() + flashlight_interval;

		if(ply._BatteryLevel &lt;= flashlight_minimum)then
			ply:Flashlight(false);
		end;
	elseif(ply._BatteryLevel &lt; flashlight_maximum and !ply:FlashlightIsOn() and (!ply._NextBatteryRegen or ply._NextBatteryRegen &lt; CurTime()))then
		ply._BatteryLevel = math.Clamp(ply._BatteryLevel + flashlight_regen_per_interval, 0, flashlight_maximum);
		ply._NextBatteryRegen = CurTime() + flashlight_interval;
	end;
end;

end);[/lua]

Great :slight_smile:

[editline]19th May 2012[/editline]

It worked, how would I add some sort of flashlight battery level to the hud?