Jump to content

[Dedicated Server] Working Nightvision component.

Recommended Posts

Okay as many people know Woodie is not in the game and that is mainly due to the fact that there has not been a way for night vision to be implemented into the game as of yet. I've taken my time in creating a night vision component which works on dedicated servers.


If you are creating and hosting the game through the Don't Starve Together client this component will not work.


The reason I am making this post is to specifically show how night vision can be accomplished and to allow modders to get their mods ready for dedicated servers.



With creating this particular component there needed to be some slight alterations to the grue component. The grue component does not need to be completely overwritten, simply use the following code in your modmain.lua file.



function GruePostInit(self)	self._CheckForStart = self.CheckForStart	self._OnUpdate = self.OnUpdate		function self:CheckForStart()		if self.inst:HasTag("nightvision") then			return not self.inst:HasTag("nightvision") and self:_CheckForStart()		else			return self:_CheckForStart()		end	end	function self:OnUpdate(dt)		if not self.sleeping and self.inst:HasTag("nightvision") then			self:Stop()            return        else        	self:_OnUpdate(dt)       	end   end	return selfendAddComponentPostInit("grue", GruePostInit) 


The next step was to set up the nightvision component. You can do it by settings it in the following fashion.


Assets = {	Asset("IMAGE", "images/colour_cubes/beaver_vision_cc.tex"),}local function Wilson(self)	self:AddComponent("nightvision")	self.components.nightvision:SetRadius(100)	self.components.nightvision:SetFalloff(.1)	self.components.nightvision:SetIntensity(.6)	self.components.nightvision:SetOverrideColourCube("images/colour_cubes/beaver_vision_cc.tex")	self.components.nightvision:SetColour(255/255,255/255,255/255)	return selfendAddPrefabPostInit("wilson", Wilson) 


This is just an example using Wilson. Adjust and add it to your prefab file instead.


local DEFAULT_COLOURCUBE = "images/colour_cubes/identity_colourcube.tex"local OVERRIDE_COLOURCUBE = nillocal function onphase(inst, phase)	if phase == "night" and inst:HasTag("nightvision") then		--if ThePlayer == inst then			inst.components.nightvision.enable:push()		--end	elseif phase == "day" and inst:HasTag("nightvision") then		--if ThePlayer == inst then			inst.components.nightvision.disable:push()		--end	endendlocal function onenterdark(inst, data)	if inst:HasTag("nightvision") then		--if ThePlayer == inst then			inst.components.nightvision.enable:push()		--end	endendlocal function onplayerspawn(inst)	if inst:HasTag("nightvision") and TheWorld.state.phase == "night"  then		--if ThePlayer == inst then			inst.components.nightvision.enable:push()		--end	endendlocal function onplayerentered(inst)	if inst:HasTag("nightvision") and TheWorld.state.phase == "night" then		--if ThePlayer == inst then			inst.components.nightvision.enable:push()		--end	endendlocal function onplayerrespawn(inst)	if inst:HasTag("nightvision") and TheWorld.state.phase == "night" then		--if ThePlayer == inst then			inst.components.nightvision.enable:push()		--end	endendlocal function onenable(inst)	if inst:HasTag("nightvision") then		if ThePlayer == inst then			-- Push the override even for the colour cube.			TheWorld:PushEvent("overridecolourcube", OVERRIDE_COLOURCUBE or DEFAULT_COLOURCUBE)			inst.components.nightvision._light.entity:SetParent(inst.entity)			inst.components.nightvision._light.Light:SetRadius(inst.components.nightvision.custom_radius)			-- Default value for the radius.			inst.components.nightvision._light.Light:SetFalloff(inst.components.nightvision.custom_falloff)		-- Default value for the fall off.			inst.components.nightvision._light.Light:SetIntensity(inst.components.nightvision.custom_intensity)	-- Default value for the intensity.			-- Enable the light.			inst.components.nightvision._light.Light:Enable(true)		end	endendlocal function ondisable(inst)	if inst:HasTag("nightvision") then		if ThePlayer == inst then			-- Push the override even for the colour cube.			TheWorld:PushEvent("overridecolourcube")			-- Disable the light.			inst.components.nightvision._light.Light:Enable(false)		end	endendlocal function custom_radius_dirty(inst)	if inst:HasTag("nightvision") then		inst.components.nightvision.custom_radius = inst.components.nightvision.net_custom_radius:value()	endendlocal function custom_falloff_dirty(inst)	if inst:HasTag("nightvision") then		inst.components.nightvision.custom_falloff = inst.components.nightvision.net_custom_falloff:value()	endendlocal function custom_intensity_dirty(inst)	if inst:HasTag("nightvision") then		inst.components.nightvision.custom_intensity = inst.components.nightvision.net_custom_intensity:value()	endendreturn Class(function(self, inst)	--		Required Variables		--self.inst = instself.inst:AddTag("nightvision")	-- Adds the tag so that we can determine if we are capable of nightvision.--		Netvar Eventself.enable = net_event(self.inst.GUID, "nightvision.enable")self.disable = net_event(self.inst.GUID, "nightvision.disable")--		Light information.self._light = CreateEntity()self._light.entity:AddTransform()self._light.entity:AddLight()self._light.Light:Enable(false)		-- Default value for when it's day/dusk.self.custom_radius = 100self.custom_falloff = .1self.custom_intensity = .6self.net_custom_radius = net_float(self.inst.GUID, "custom_radius", "nightvision.custom_radius")self.net_custom_falloff = net_float(self.inst.GUID, "custom_falloff", "nightvision.custom_falloff")self.net_custom_intensity = net_float(self.inst.GUID, "custom_intensity", "nightvision.custom_intensity")self._light:AddTag("FX")--		Event Listeners and State Watchersinst:ListenForEvent("nightvision.enable", onenable)inst:ListenForEvent("nightvision.disable", ondisable)inst:ListenForEvent("nightvision.custom_radius", custom_radius_dirty)inst:ListenForEvent("nightvision.custom_falloff", custom_falloff_dirty)inst:ListenForEvent("nightvision.custom_intensity", custom_intensity_dirty)--if TheWorld.ismastersim then	self.inst:WatchWorldState("phase", onphase)	self.inst:ListenForEvent("enterdark", onenterdark)	self.inst:ListenForEvent("ms_respawnedfromghost", onplayerrespawn)	TheWorld:ListenForEvent("ms_playerspawn", onplayerspawn)	TheWorld:ListenForEvent("playerentered", onplayerentered)--end--	This function is used to set the override colour cube for our nightvision component.function self:SetOverrideColourCube(path)	OVERRIDE_COLOURCUBE = pathendfunction self:SetRadius(amount)	self.custom_radius = amount	self.net_custom_radius:set(amount)endfunction self:SetFalloff(amount)	self.custom_falloff = amount	self.net_custom_falloff:set(amount)endfunction self:SetIntensity(amount)	self.custom_intensity = amount	self.net_custom_intensity:set(amount)endfunction self:SetColour(r, b, g)	self._light.Light:SetColour(r, b, g)endend) 


This particular component is a wrapper component for the Light, but with the ability to actually override the colour cube. Please do not ask me how the colour cube.tex files are created as I was not able to actually extract them into PNG format files to look; so I do not know. You can view the current colour cubes under images/colour_cubes in the Don't Starve Together folder.


I'll be attaching the entire sample project for those that wish to look it over and use it as they see fit.


Entire Project: 



Edited by Kzisor
Link to comment
Share on other sites

@DarkXero, the difference is when you host a dedicated server on your computer you are not technically the host/server when you join that server, you are considered a client.


When you host a non-dedicated server (hosting through the Don't Starve Together client) you are considered the host/server.


The subtle difference might not seem like it would make such a difference, but it in fact makes a huge difference.

Edited by Kzisor
Link to comment
Share on other sites

@DarkXero, performance wise there is a big difference for the host of the server. Instead of having no lag you actually can have some lag if you run a dedicated server; I think the reason behind this is that it's using the internet instead of defaulting to lan for host if ran on same computer.


As far as mods breaking or getting destroyed, the logical process just has to be different when creating the mods. Instead of worrying about whether the mod will work for both server and client, you only really need to make sure it works for the client. i.e., testing mods becomes faster for client side testing with dedicated servers.


The benefit from having host/server is that the "host/admin" isn't considered the server. This opens up the possibility of a large number of things to happen that cannot happen when the host/admin is considered the server. One such example is night vision for client only.

Link to comment
Share on other sites

@DarkXero, yes I was asked by Klei to help Beta test the dedicated server software and I obtained permission to make the bug report I filed about dedicated servers. I made this post directly after I made the bug report because I thought it would help modders understand there is a difference between dedicated server modding and non-dedicated server modding. I've noted those differences in the above posts.


Did I experience lag? Short answer yes, I will not provide more information without Klei's permission or until the dedicated servers are released to the public. This is out of respect and consideration to the developers working on the dedicated server software.

Link to comment
Share on other sites

@DarkXero, I sent PeterA several reports for issues with the windows dedicated server; he sent them over to the team and they were able to pinpoint and vaporize them. Hopefully the lag issue was one of them vaporized, but won't know until an update is pushed or release; which ever comes first.

Link to comment
Share on other sites

  • Developer

@Kzisor, and @DarkXero, thanks for this conversation! Having the ability to run a server locally should definitely help with testing client side mod code.

Regarding mods that only work for dedicated servers, and not for non-dedicated servers, I think we want to avoid making the distinction for users. If we can't come up with a solution though, this is a flag we could add to the modinfo to stop non-dedicated servers from running mods that aren't supported. Ideally the mods should work on any server dedicated or not.


With regards to the lag issue @Kzisor, I'll send you a message about your network setup I'm curious about what you were seeing vs what I was seeing in my home network setup. Mark hasn't addressed connecting locally yet, but I believe that will fix the issue.


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

  • Create New...