How to make flashlight SWEP? Can anyone help? I really need it!

So… How to make flashlight SWEP. I think it’s self-describing and you know what i mean. Just like F button flashlight, but in a SWEP version. And also… It needs something like batteries power, i mean the ammo function that removes automatically ammo per two seconds when the flashlight is on.

Couldn’t you just take a flashlight model and then once they click turn on the player flashlight? Considering that the origin of the light comes from the hand last time I checked you could easily make it so it only allows the flashlight to work in certain conditions.

Useful stuff for you:
http://wiki.garrysmod.com/page/Player/Flashlight
http://wiki.garrysmod.com/page/Player/AllowFlashlight

No, the default flashlight has too big ‘spread’ and too small range. And also it should have something like ammo, with 100 magazine capacity and when it’s turned on it should take 1 ammo per 2 seconds.

[editline]7th May 2015[/editline]

Is it possible?

Player:Flashlight

Player:AllowFlashlight

WEAPON/Think

WEAPON/TakePrimaryAmmo
the rest is just some vars

You cannot alter the appearance of the flashlight.

That’s pretty bad :(So, i will try with default light.

[editline]7th May 2015[/editline]

Can you show me code, if you can? Sorry, i have bad memory to LUA :expressionless:

Instead of using the player light then couldn’t you use a dynamic light and then just disable the default light?

Seems like it would be better for what he wants.

Sorry haven’t seen the post in between, cuz i searched the links.

i think you could use env_projectedtexture with the input “SpotlightTexture” effects/flashlight001
and with some modification of the KeyValues FarZ

you can read it up here

Okay, okay okay thanks, but I AM SO HUGE LUA NOOB. So can you show me code if you can? You looks like a good lua helper.

[editline]7th May 2015[/editline]

Okay, okay okay thanks, but I AM SO HUGE LUA NOOB. So can you show me code if you can? You looks like a good lua helper. So can you? Please.

[editline]7th May 2015[/editline]

??? Why i duplicated the message?

http://forum.facepunch.com/showthread.php?t=1337945



local fleshlight = ents.Create( "env_projectedtexture" )
fleshlight:SetPos(LocalPlayer():GetPos())
fleshlight:SetAngles(LocalPlayer():GetAngles())
fleshlight:SetKeyValue( "nearz", 4 )
fleshlight:SetKeyValue( "farz", 750.0 )
fleshlight:SetKeyValue( "lightcolor", "255 255 255 255")
fleshlight:SetKeyValue( "lightfov", 75 )
fleshlight:Spawn()
fleshlight:Input( "SpotlightTexture", NULL, NULL, "effects/flashlight001")


About the flashlight, I know how to turn it off and everything else about the SWEP and functions…

Let’s say I create a command that check if the player has that item called “Stunstick”

If its true then…

And now comes the problem because I don’t know how to make light via a command.
I have a cind of example how I gave a prop light.

When the Item gets on the map the prop gets a Position.
Where the position is he will get the light.

This is in a cl_init.lua Client
Because I think it lags when it would be on init.lua because the server would then load it too and this lags so only the client loads it.


-- Called when the entity should think.
function ENT:Think()
    local dlight = DynamicLight( self:EntIndex() )
	if ( dlight ) then
		local r, g, b, a = self:GetColor()
		dlight.Pos = self:GetPos()
		dlight.r = 253
		dlight.g = 222
		dlight.b = 23
		dlight.Brightness = 0
		dlight.Size = 698
		dlight.Decay = 5
		dlight.DieTime = CurTime() + 0.1
	end   
end

But this does not explain how you can remove the light and add the light.
I want to know what light gets created for the flashlight and what happends with the light if you turn it off what code?


		if (attachment and attachment.Pos) then
			cam.Start3D( EyePos(), EyeAngles() );
				render.SetMaterial(self.ThirdPersonGlowSprite);
				render.DrawSprite( attachment.Pos, 8 + scale, 8 + scale, Color(255, 255, 255, 255 ) );
			cam.End3D();
		end;
	end;
end;


	SWEP.FirstPersonGlowSprite = Material("sprites/light_glow02_add_noz");
	SWEP.ThirdPersonGlowSprite = Material("sprites/light_glow02_add");

This does render the sprite at the position where the Stunstick normaly does damage and should glow.

But whatever this SWEP is in a shared.lua of course and its for client and server I guess.

If you turn on a flashlight the client sees his flashlight that he can move around. BUT other people see the flashlight facing the ground from the flashlight owner.
Is that a server flashlight or another client light, is it possible that it lags if I would it switch on and off.

As I know it does lag when its not a client light.

So, now lets say I type in /flicker

Then the stunstick should create a little lightblue light that turn on then again off and then again on and off and this it does for an amount of time then it stops and it should also play a sound and maybe create a sprite doing the same as the light does.

But of course I need to create a light and let the light disappear.

Can anyone help here please?

Edit:

I think I need this https://developer.valvesoftware.com/wiki/Env_projectedtexture
But how I would still do this?

I was looking into a custom flashlight as I need one for my game-mode. Is it AT ALL possible in gmod to get an Alan Wake kinda effect, where you see the beam, rather then just a light where it hits?

What actually gives the beam effect in these kinda games? Can you spawn light_spot in gmod like you can have in hammer?

Yes you can create any entity as you can create in hammer, an example is above where you also can set keyvalues, use that example and edit it to your needs.

So you can create the beam between yourself and the final spot?

So I use the env_projectedtexture?

And how I let it automaticaly 3x on and off?

How does the FLASHLIGHT code look for that?

Look at the Stop it Slender gamemode, and look at the camera swep, it got such a flashlight built in it.

Unknoe what vecfake should do, I think because of fake vector it will point on the hand for other people


local vecfake = Vector(0, 0, 16000)
function SWEP:DrawHUD()//DrawHUD

Creates and gets those entitys


	local light = Entity(0):GetDTEntity(0)
	local light_small = Entity(0):GetDTEntity(1)

Checks if its valid and says how to switch.
Sets also the position



	if light and IsValid(light) and light_small and IsValid(light_small) then
		
		local todraw = self:GetSwitch() and not self.Owner:KeyDown(IN_ATTACK2)
		
		if todraw then
			
			if light:IsEffectActive( EF_NODRAW ) then
				light:SetNoDraw(false)
			end
			
			local pos = EyePos()+EyeAngles():Right()*14+EyeAngles():Forward()*5
			
			light:SetPos(pos)
			light:SetAngles(((self.Owner:GetEyeTrace().HitPos - EyePos()):GetNormal()):Angle())
			
		else
			if not light:IsEffectActive( EF_NODRAW ) then
				light:SetNoDraw(true)
			end
			light:SetPos(vecfake)
		end

This creates the flashlight entity by SetDTEntity(ID,ent)
later it Gets an DTEntity(ID,ent) does it even override other ID’s?


function GM:CreateFlashLight()
	local ent = ents.Create("env_projectedtexture")
	if ent:IsValid() then
		ent:SetLocalPos(Vector(16000, 16000, 16000))
		ent:SetKeyValue("enableshadows", 1)
		ent:SetKeyValue("farz", 500)//1024
		ent:SetKeyValue("nearz", 8)
		ent:SetKeyValue("lightfov", 70)//60
		ent:SetKeyValue("lightcolor", "205 205 205 115")
		ent:Spawn()
		ent:Activate()
		ent:Input("SpotlightTexture", NULL, NULL, "effects/flashlight001")		

		game.GetWorld():SetDTEntity(0,ent)
	end
	//second one
	local ent = ents.Create("env_projectedtexture")
	if ent:IsValid() then
		ent:SetLocalPos(Vector(16000, 16000, 16000))
		ent:SetKeyValue("enableshadows", 1)
		ent:SetKeyValue("farz", 684)//1024
		ent:SetKeyValue("nearz", 8)
		ent:SetKeyValue("lightfov", 20)//60
		ent:SetKeyValue("lightcolor", "255 255 255 255")
		ent:Spawn()
		ent:Activate()
		ent:Input("SpotlightTexture", NULL, NULL, "effects/flashlight001")

		game.GetWorld():SetDTEntity(1,ent)
	end


So I did was not able to find how it makes those flicker stuff
SpotlightTexture? Whats about ent_light , what is if I want to create that?

I still don’t know how I make a client side light and a light for other ones who don’t have the light.


self.FirstPersonGlowSprite:SetFloat("$alpha", 0.7 + alpha);
				self.ThirdPersonGlowSprite:SetFloat("$alpha", 0.5 + alpha);
				
				if (attachment and attachment.Pos) then
					cam.Start3D( EyePos(), EyeAngles() );
						render.SetMaterial(self.ThirdPersonGlowSprite);
						render.DrawSprite( attachment.Pos, 8 + scale, 8 + scale, Color(255, 255, 255, 255 ) );
						
						self.FirstPersonGlowSprite:SetFloat("$alpha", 0.5 + alpha);
						
						for i = 1, 9 do
							local attachment = viewModel:GetAttachment( viewModel:LookupAttachment("spark"..i.."a") );
							
							if (attachment.Pos) then
								if (i == 1 or i == 2 or i == 9) then
									render.SetMaterial(self.ThirdPersonGlowSprite);
								else
									render.SetMaterial(self.FirstPersonGlowSprite);
								end;
								
								render.DrawSprite( attachment.Pos, 1, 1, Color(255, 255, 255, 255) );
							end;
						end;

I will use that attachment by modifing it so it creates then a light but, that for a plugin.

Let’s say the light should switch 3x times (on then off 3x) then it plays a sound, I know how to make playing sounds but the other one is still unknown.
This thing did now only explain how to create a light but how to (my questions) I still don’t know how? help.

EDIT:

Is there any special entity too for those lights?

I know I’m late (go ahead and rate me late), but you totally can with console commands. I’m just not sure if Lua is allowed to call those commands.

I want to create light and turn it off and on just with one command like a video clip where it auto turns off and on and it repeats everytime if you type it in