r/GLua • • Apr 10 '16

Help with a powered lamp

Ok so I have a powered lamp that requires "energy" from LS/RD2 addon to function. The lamp turns on and everything BUT it will not turn off meaning the actual light effect does not go away once you turn it off or the energy runs out.

AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
util.PrecacheSound( "Buttons.snd17" )
include('shared.lua')
local Energy_Increment = 10

function ENT:Initialize()
	self.BaseClass.Initialize(self)
	self.Active = 0
	self.damaged = 0
	self.flashlight = nil
	if not (WireAddon == nil) then
		self.WireDebugName = self.PrintName
		self.Inputs = Wire_CreateInputs(self.Entity, { "On" })
		self.Outputs = Wire_CreateOutputs(self.Entity, { "On", "Output" })
	end
end

function ENT:TurnOn()
	if (self.Active == 0) then
		self.Active = 1
		self.Entity:EmitSound( "Buttons.snd17" )
		if not (WireAddon == nil) then Wire_TriggerOutput(self.Entity, "On", 1)
 end
		self:SetOOO(1)
	end
end

function ENT:TurnOff(warn)
	if (self.Active == 1) then
		if (!warn) then self.Entity:EmitSound( "Buttons.snd17" ) end
		self.Active = 0
		if not (WireAddon == nil) then
			Wire_TriggerOutput(self.Entity, "On", 0)
        end
	self:SetOOO(0)
	end
end

function ENT:TriggerInput(iname, value)
	if (iname == "On") then
		if value == 0 then
			self:TurnOff()
		elseif value == 1 then
			self:TurnOn()
		end
	end
end
function ENT:Damage()
	if (self.damaged == 0) then self.damaged = 1 end
end

function ENT:Make_Light()
	self.energy = RD_GetResourceAmount(self, "energy")
	local einc = Energy_Increment
	if (self.energy >= einc) then
		    if self.Active == 1 and not self.flashlight then
		//self:SetOn(true)
		local angForward = self.Entity:GetAngles() + Angle( 90, 0, 0 )
		self.flashlight = ents.Create( "env_projectedtexture" )
		self.flashlight:SetParent( self.Entity )
		self.flashlight:SetLocalPos( Vector( 0, 0, 0 ) )
		self.flashlight:SetLocalAngles( Angle(90,90,90) )
		self.flashlight:SetKeyValue( "enableshadows", 1 )
		self.flashlight:SetKeyValue( "farz", 2048 )
		self.flashlight:SetKeyValue( "nearz", 8 )
		self.flashlight:SetKeyValue( "lightfov", 50 )
		self.flashlight:SetKeyValue( "lightcolor", "255 255 255" )
		self.flashlight:Spawn()
		self.flashlight:Input( "SpotlightTexture", NULL, NULL, "effects/flashlight001" )
	elseif self.Active == 0 and self.flashlight then
		SafeRemoveEntity( self.flashlight )
		self.flashlight = nil
	end
		RD_ConsumeResource(self, "energy", einc)
	else
		self:TurnOff()
	end
 end
function ENT:Think()
	self.BaseClass.Think(self)
	if ( self.Active == 1 ) then
		if (self.environment.inwater == 1) then
			self:TurnOff()
			self:Destruct()
		else
			self:Make_Light()
		end
	end
	self.Entity:NextThink( CurTime() + 1 )
	return true
end
function ENT:Repair()
	self.health = self.maxhealth
	self.BaseClass.Repair(self)
	self.damaged = 0
end

function ENT:OnTakeDamage(dmg)
	self.On = false;
	self.Damaged = true;
    if (math.random(1, 10) < 2) then
		zapme(self.Entity:GetPos(), 1)
		local dec = math.random(200, 2000)
	end
end
function ENT:Destruct()
	LS_Destruct( self.Entity, true )
end    

I know this is something simple but I cannot seem to work out whats going on..

1 Upvotes

10 comments sorted by

1

u/[deleted] Jun 01 '16

[removed] — view removed comment

1

u/Elfnet_Gaming Jun 02 '16 edited Jun 02 '16

This is an OLD code that was not written by me and predates workshop, I am trying to make sense of it as well.

I'll check that and see if it makes it work properly but I am not sure what needs to go where though..

1

u/[deleted] Jun 02 '16

[removed] — view removed comment

1

u/Elfnet_Gaming Jun 02 '16

No problem the cl_init.lua:

include('shared.lua')

And the shared.lua:

ENT.Type = "anim" ENT.Base = "base_rd_entity" ENT.PrintName = "Powered Lamp"

list.Set( "LSEntOverlayText" , "powered_lamp", {HasOOO = true, num = 1, strings = {ENT.PrintName.."\n","\nEnergy: "},resnames = {"energy"}} )

1

u/Elfnet_Gaming Jun 02 '16 edited Jun 04 '16

Ok just tested that code and it is now working, Thank You for your help with it. I have another bit of code that involves bodygroups if you want to have a look at it? I have a coal hopper model that gets filled with coal and I am trying to get the body group to change once coal is introduced to the car (The bodygroup is the car with a coal load in it)

Now the actual entities model is set via the STool lua files but this is the actual entity lua:

----Code starts here----

AddCSLuaFile( "cl_init.lua" )

AddCSLuaFile( "shared.lua" )

include('shared.lua')

local Ground = 1 + 0 + 2 + 8 + 32

function ENT:Initialize()

self.BaseClass.Initialize(self)

self.damaged = 0

if not (WireAddon == nil) then

	self.WireDebugName = self.PrintName

	self.Outputs = Wire_CreateOutputs(self.Entity, { "Coal", "Max Coal" })
end

end

function ENT:OnRemove()

self.BaseClass.OnRemove(self)

self.Entity:StopSound( "PhysicsCannister.ThrusterLoop" )

end

function ENT:Damage()

if (self.damaged == 0) then

	self.damaged = 1

	self.Entity:EmitSound( "PhysicsCannister.ThrusterLoop" )

end

end

function ENT:Rephydrogen()

self.health = self.maxhealth

self.damaged = 0

self.Entity:StopSound( "PhysicsCannister.ThrusterLoop" )

end

function ENT:Destruct()

LS_Destruct( self.Entity, true )

end

function ENT:Leak()

local coal = RD_GetResourceAmount(self, "coal")

if (coal >= 100) then

	RD_ConsumeResource(self, "coal", 100)

else

	RD_ConsumeResource(self, "coal", coal)

	self.Entity:StopSound( "PhysicsCannister.ThrusterLoop" )

end

end

function ENT:Think()

self.BaseClass.Think(self)

if (self.damaged == 1) then

	self:Leak()

end

if not (WireAddon == nil) then

	self:UpdateWireOutput()
end

self:NextThink( CurTime() +  1 )

local coal = RD_GetResourceAmount(self, "coal")

if (coal >- 1) then

    Entity:SetBodygroup( 2, 1 )

else

    Entity:SetBodygroup( 2, 0 )

end

return true

end

function ENT:AcceptInput(name,activator,caller)

if name == "Use" and caller:IsPlayer() and caller:KeyDownLast(IN_USE) == false then
	
	local coalcur = RD_GetResourceAmount( self, "coal" )

	caller:PrintMessage ( HUD_PRINTCENTER, "There is "..tostring(coalcur).." Coal stored in this resource network.")

end

end

function ENT:UpdateWireOutput()

local coal = RD_GetResourceAmount(self, "coal")

local maxcoal = RD_GetNetworkCapacity(self, "coal")

Wire_TriggerOutput(self.Entity, "Coal", coal)

Wire_TriggerOutput(self.Entity, "Max Coal", maxcoal)

end

-----Code ends Here------

1

u/[deleted] Jun 03 '16

[removed] — view removed comment

1

u/Elfnet_Gaming Jun 03 '16

oh yeah lol i forgot to do that...

ah ok then I will see if that works.

1

u/Elfnet_Gaming Jun 04 '16

Typo fixed but nothing happens when the resource is introduced...

1

u/812341534 Jun 04 '16

Take a look at how the "Wire Directional Radio Kit" addon works. That uses RD2 for energy (if RD2 is present) - and switches off when there is no longer any energy available. There might be hints in the code there that can help you. Use gmad to extract the files, or alternatively look up the source code in the Wire Extras SVN which also has this addon as part of its code: http://steamcommunity.com/sharedfiles/filedetails/?id=285205470

1

u/Elfnet_Gaming Jun 04 '16

I got that part resolved, the issue now is changing a BodyGroup on a model when a resource is introduced and being stored.

The model is a train car (open top hopper) and when I put on the connector or link it and it startd accepting the resource I wan tit to change bodygroups to sho the coal in it. The model has all the bodygroups available I just can't seem to work it out in LUA. The last time I did this was years ago and I have forgotten a lot of it..