Jump to content

[Scripting] Client side light?


Recommended Posts

@Woodside235, Yes. You'll want to remove the grue component (Charlie), and then make sure the code for lighting is running on the client only. I think otherwise the process for creating light is similar. You can ensure something runs only on the client like this:

if not TheWorld.ismastersim then    --client-only codeend
Link to comment
Share on other sites

@rezecib, I think I've narrowed it down to how it needs to work. I just have a few more things I need to set up in order for it to work completely. One problem I am having to nail down is the host obtaining night vision gives everyone night vision. This probably wouldn't be an issue if we use dedicated servers, however, if the host is an actual player then they will give everyone night vision.

 

Client-Side Nightvision Theory:

Set up a nightvision component which acts as the light source, separate from the character instance. On the client machine run the code for adding the component and enable the effect. Upon enabling nightvision send a RPC to the server notifying they have entered light to disable the grue.

 

Server-Side Nightvision Theory:

In the same nightvision component, set up a new variable which is the night vision instance which will be sent to the client machine. Upon enabling or disabling the nightvision if it's on the server side, disable it for that instance and send a RPC to the server notifying they have entered darkness to enable the grue again.

 

 

This is all theory, but I have a semi-working concept which has the light enabled on the client machine and not on the server side. I'm going to work on the RPC's to see if I can get a fully working concept.

 

Alternate solution:

Remove the grue component from the player with nightvision on the server side. This still has issues with the server giving the light to all players though. That is something I don't know how to fix.

Edited by Kzisor
Link to comment
Share on other sites

host obtaining night vision gives everyone night vision.
Hmm... Non-networked host light should definitely be doable, seeing as that's how all the lights worked before they got networked. Although for those, they also provided protection from Charlie, even though clients couldn't see the light...

 

Well, in any case, we'll get to see a proper solution when Woodie is released.

Link to comment
Share on other sites

@rezecib, the issue arises with the LightWatcher component which I cannot find at all. I have not been able to find the Light component either, so it seems those are API directly from the C code.

 

Here is a semi-working solution, host getting nightvision still seems to give clients protection.

 

script/components/nightvision.lua

return Class(function(self, inst)	self.inst = inst	self.nightvision = CreateEntity()	self.nightvision.entity:AddLight()	self.nightvision.Light:Enable(false)	self.enabled = false		function self:Enable()		self.nightvision.Light:Enable(true)		self.enabled = true	end	function self:Disable()		self.nightvision.Light:Enable(false)		self.enabled = false	end    function self:SetRadius(amount)    	self.nightvision.Light:SetRadius(amount)    end    function self:SetFalloff(amount)    	self.nightvision.Light:SetFalloff(amount)    end    function self:SetIntensity(amount)    	self.nightvision.Light:SetIntensity(amount)    end    function self:SetColour(r, b, g)    	self.nightvision.Light:SetColour(r, b, g)    endend) 

 

modmain.lua

local function PhaseChange(inst, phase)	if phase == "night" and inst.components.nightvision then				if GLOBAL.ThePlayer == inst then			inst.components.nightvision:Enable()		end		if GLOBAL.TheWorld.ismastersim then			inst.components.nightvision.enabled = true		end	elseif inst.components.nightvision then		if GLOBAL.ThePlayer == inst then			inst.components.nightvision:Disable()		end		if GLOBAL.TheWorld.ismastersim then			inst.components.nightvision.enabled = false		end	endendWilson = function(self)	self:AddComponent("nightvision")    self:WatchWorldState("phase", PhaseChange)	self.components.nightvision:SetRadius(100)    self.components.nightvision:SetFalloff(.5)    self.components.nightvision:SetIntensity(.5)    self.components.nightvision:SetColour(0/255,40/255,255/255)	self:ListenForEvent("keypressed", OnKeyPressed)		return selfendAddPrefabPostInit("wilson", Wilson)function GruePostInit(self)	self._CheckForStart = self.CheckForStart	self._OnUpdate = self.OnUpdate		function self:CheckForStart()		if self.inst.components.nightvision then			return self.inst.components.nightvision.enabled and self:_CheckForStart()		else			return self:_CheckForStart()		end	end	function self:OnUpdate(dt)		if not self.sleeping and self.inst.components.nightvision and self.inst.components.nightvision.enabled then			print("Prefab: "..self.inst.prefab.." does have nightvision.")			self:Stop()            return        else        	print("Prefab: "..self.inst.prefab.." does not have nightvision.")        	self:_OnUpdate(dt)       	end   end	return selfendAddComponentPostInit("grue", GruePostInit) 

 

 

I think the major issue lies in the event "enterlight" which is called upon the host enabling the Light component/class. There seems to be a data variable being sent, however, nothing in the data variable is being used by any instance that is listening for the "enterlight" event.

 

I guess we will have to wait and see how Klei fixes nightvision they might have a custom solution to the problem.

Edited by Kzisor
Link to comment
Share on other sites

@Woodside235, charlie should not attack if you have it enabled. However, charlie will attack if you do not have it enabled. 

 

The server must also be running this mod with it enabled for it to work properly.

 

So apparently I am an idiot and forgot to update the code before posting. It's been updated with the proper code. I was actually modifying the code as I was typing my post so hopefully the edited code works like it's suppose to for clients.

 

NOTE: THIS DOES NOT FIX THE ISSUE WITH CLIENTS GETTING PROTECTION WHEN HOSTS HAVE PROTECTION FORM CHARLIE.

Edited by Kzisor
Link to comment
Share on other sites

Woodside235, bugs are to be expected from semi-working code. The code I posted was never mean't to be used yet as it's not fully functional and bug free. By using it, you're doing so under your own risk and until Klei supports true nightvision we probably won't have fully working code.

 

However, I've got some new ideas on how to make it possibly work.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
  • Create New...