Jump to content

[SOLVED] Night vision help?


Recommended Posts

Im trying to get night vision for my custom character and am having trouble making it so that if an instance ( for me its inst.strength == "hungrypig") is true, then it activates night vision. I was using the link below as reference, but I'm not sure how to convert the sanity part, into what I need it to be. Any help would be wonderful, thank you

 

edit: the code that I was looking at was the one below

 

local function UpdateNightVision(inst)
	local phase = TheWorld.state.phase
	local sanitypercent = inst.replica.sanity:GetPercentNetworked()
	local enable = phase == "night" and sanitypercent < 0.2
	if enable ~= inst.components.playervision.forcenightvision then
		inst:DoTaskInTime(enable and 0 or 1, function(inst)
			inst.components.playervision:ForceNightVision(enable)
			inst.components.playervision:SetCustomCCTable(enable and {} or nil)
		end)
	end
end

local function OnInitNightVision(inst)
	if TheWorld.ismastersim or inst.HUD then
		inst:WatchWorldState("phase", UpdateNightVision)
		inst:ListenForEvent("sanitydelta", UpdateNightVision)
		UpdateNightVision(inst)
	end
end

local function common_postinit(inst)
	inst:DoTaskInTime(0, OnInitNightVision)
end

 

 

Edited by LlamaDom
Link to comment
Share on other sites

You will need to use a netvar to transmit the data from the server to the client.

In your case it's probably a net_string that will be used, have a look at this post for more information:

If you need more help, let me know :)

  • Like 1
Link to comment
Share on other sites

14 hours ago, Monti18 said:

You will need to use a netvar to transmit the data from the server to the client.

In your case it's probably a net_string that will be used, have a look at this post for more information:

If you need more help, let me know :)

im having a bit of trouble understanding what to do exactly...im not that fluent in coding so I only understand chunks of this

Link to comment
Share on other sites

I wrote a short explanation of netvars here, you can have a quick look at it to understand it better hopefully.

Netvars need to be added to the client and the server, that's why you will need to add them to the common_postinit of your character prefab.

net_strength = net_string(inst.GUID,"strength","strengthdirty")
net_strength:set("normal")

You can add this to your common postinit to add a net string to your character. I don't know how your base form is called, just change normal to what it is called.

Then you need to add to whatever functions that change inst.strength also a line that changes net_strength the same way as I showed before.

Now your variable strength is synchronized between the server and the client, you can now change the function.

local function UpdateNightVision(inst)
	local phase = TheWorld.state.phase
	local has_strength = inst.net_strength:value()
	local enable = phase == "night" and has_strength  == "hungrypig"
	if enable ~= inst.components.playervision.forcenightvision then
		inst:DoTaskInTime(enable and 0 or 1, function(inst)
			inst.components.playervision:ForceNightVision(enable)
			inst.components.playervision:SetCustomCCTable(enable and {} or nil)
		end)
	end
end

local function OnInitNightVision(inst)
	if TheWorld.ismastersim or inst.HUD then
		inst:WatchWorldState("phase", UpdateNightVision)
		inst:ListenForEvent("strengthdirty", UpdateNightVision)
		UpdateNightVision(inst)
	end
end

local function common_postinit(inst)
	inst:DoTaskInTime(0, OnInitNightVision)
end

You check the value of net_strength and see if it's hungrypig or not and then enable or disable nightvision. The same goes if you change net_strength, as it pushes the event strengthdirty, which is listened to by the server and client, which then also calls UpdateNightVision.

You could achieve the same by using a net_bool as you only have one thing that should change, but perhaphs you want to add more things to your character, then a net_string or a net_smallbyte is better. Performancewise the net_string is worse, but I don't think it will make that big of a difference for you.

If something is unclear or you have more questions, ask me!

  • Like 1
Link to comment
Share on other sites

1 hour ago, Monti18 said:

Then you need to add to whatever functions that change inst.strength also a line that changes net_strength the same way as I showed before.

How would I add? just like the function itself that I use to change inst.strength?

Link to comment
Share on other sites

This is an example:

local function changestrength(inst)
	inst.strength = "hungrypig"
  	net_strength:set("hungrypig")
end

Whenever you change inst.strength, just add another line to it where you change net_strength to the same value.

  • Like 1
Link to comment
Share on other sites

On 7/5/2021 at 12:27 AM, Monti18 said:

Netvars need to be added to the client and the server, that's why you will need to add them to the common_postinit of your character prefab.


net_strength = net_string(inst.GUID,"strength","strengthdirty")
net_strength:set("normal")

So for this, it would be the folowing correct?

local function common_postinit(inst)
	    inst:DoTaskInTime(0, OnInitNightVision)
            net_strength = net_string(inst.GUID,"strength","strengthdirty")
            net_strength:set("fullpig")      --fullpig is the normal form
end

Along with this, if I have more than 2 form (I have 3 forms) would I have to do the 

On 7/5/2021 at 2:32 AM, Monti18 said:

local function changestrength(inst) inst.strength = "hungrypig" net_strength:set("hungrypig") end

to all of them? would this make a difference?

 

 

 

When I did the following to my script, the game didnt load, ie, after the charater select screen, the game doesnt load.

Link to comment
Share on other sites

Oh I made an error, its:

local function common_postinit(inst)
	    inst:DoTaskInTime(0, OnInitNightVision)
            inst.net_strength = net_string(inst.GUID,"strength","strengthdirty")
            inst.net_strength:set("fullpig")      --fullpig is the normal form
end

It depends on what you want, if you only need to send data to the client for the nightsight, as you probably do, you only need to add it to the form you want.

The function to change it is also

local function changestrength(inst) 
  inst.strength = "hungrypig" 
  inst.net_strength:set("hungrypig") 
end

I forgot that you need to save it to a variable for it to work.

  • Like 1
  • Thanks 1
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...