Sign in to follow this  
FrontierHero

How to give mod character night vision?

Recommended Posts

So, on the mod I've been working on, I've been wanting to add night vision. My character is an android, and his eyes work as lenses; they adjust to light. I wanted to be able to add this mechanic into the game, so I asked about this before, and got a response. So I added in what I was told to add, where I was told to put it in. I played the game to see if it would work, and unless it doesn't turn on automatically and you have to do something for it to work, the code is not allowing the character to have night vision.

I was told to add this into the character's .lua file, above the master_postit/master_postinit:

Spoiler

 

local NIGHTVISION_COLOURCUBES =
{
    day = "images/colour_cubes/ruins_light_cc.tex",
    dusk = "images/colour_cubes/ruins_dim_cc.tex",
    night = "images/colour_cubes/purple_moon_cc.tex",
    full_moon = "images/colour_cubes/purple_moon_cc.tex",
}

local function SetNightVision(inst, enable)
    if TheWorld.state.isnight or TheWorld:HasTag("cave") then
        inst.components.playervision:ForceNightVision(true)
        inst.components.playervision:SetCustomCCTable(NIGHTVISION_COLOURCUBES)
    else
        inst.components.playervision:ForceNightVision(false)
        inst.components.playervision:SetCustomCCTable(nil)
    end
end

-- This initializes for both the server and client. Tags can be added here.
local common_postinit = function(inst) 
    inst:WatchWorldState( "isday", function() SetNightVision(inst) end)
      inst:WatchWorldState( "isdusk", function() SetNightVision(inst) end)
      inst:WatchWorldState( "isnight", function() SetNightVision(inst)  end)
    inst:WatchWorldState( "iscaveday", function() SetNightVision(inst) end)
      inst:WatchWorldState( "iscavedusk", function() SetNightVision(inst) end)
      inst:WatchWorldState( "iscavenight", function() SetNightVision(inst)  end)
    
    SetNightVision(inst)
    end

 

is this wrong?

if so, what do I need to fix in order to provide night vision to the character?

thank you for your help!

Share this post


Link to post
Share on other sites
AkaiNight    28

try this 

local function NightVision(inst)
    if TheWorld.state.isnight then
        inst.Light:Enable(true)
        inst.Light:SetRadius(18)
        inst.Light:SetFalloff(0.75)
        inst.Light:SetIntensity(.7)
        inst.Light:SetColour(70/255,230/255,12/170)
	end
end

and put this into local master_postinit = function(inst)

inst:WatchWorldState("isnight", NightVision)

 

  • Like 1

Share this post


Link to post
Share on other sites
Leonardo Cox    622
9 hours ago, FrontierHero said:

So, on the mod I've been working on, I've been wanting to add night vision. My character is an android, and his eyes work as lenses; they adjust to light. I wanted to be able to add this mechanic into the game, so I asked about this before, and got a response. So I added in what I was told to add, where I was told to put it in. I played the game to see if it would work, and unless it doesn't turn on automatically and you have to do something for it to work, the code is not allowing the character to have night vision.

I was told to add this into the character's .lua file, above the master_postit/master_postinit:

  Hide contents

 

local NIGHTVISION_COLOURCUBES =
{
    day = "images/colour_cubes/ruins_light_cc.tex",
    dusk = "images/colour_cubes/ruins_dim_cc.tex",
    night = "images/colour_cubes/purple_moon_cc.tex",
    full_moon = "images/colour_cubes/purple_moon_cc.tex",
}

local function SetNightVision(inst, enable)
    if TheWorld.state.isnight or TheWorld:HasTag("cave") then
        inst.components.playervision:ForceNightVision(true)
        inst.components.playervision:SetCustomCCTable(NIGHTVISION_COLOURCUBES)
    else
        inst.components.playervision:ForceNightVision(false)
        inst.components.playervision:SetCustomCCTable(nil)
    end
end

-- This initializes for both the server and client. Tags can be added here.
local common_postinit = function(inst) 
    inst:WatchWorldState( "isday", function() SetNightVision(inst) end)
      inst:WatchWorldState( "isdusk", function() SetNightVision(inst) end)
      inst:WatchWorldState( "isnight", function() SetNightVision(inst)  end)
    inst:WatchWorldState( "iscaveday", function() SetNightVision(inst) end)
      inst:WatchWorldState( "iscavedusk", function() SetNightVision(inst) end)
      inst:WatchWorldState( "iscavenight", function() SetNightVision(inst)  end)
    
    SetNightVision(inst)
    end

 

is this wrong?

if so, what do I need to fix in order to provide night vision to the character?

thank you for your help!

This should give you nightvision once it turns night (or when you enter caves).

Is it not working?

Share this post


Link to post
Share on other sites
13 hours ago, FrontierHero said:

So, on the mod I've been working on, I've been wanting to add night vision. My character is an android, and his eyes work as lenses; they adjust to light. I wanted to be able to add this mechanic into the game, so I asked about this before, and got a response. So I added in what I was told to add, where I was told to put it in. I played the game to see if it would work, and unless it doesn't turn on automatically and you have to do something for it to work, the code is not allowing the character to have night vision.

I was told to add this into the character's .lua file, above the master_postit/master_postinit:

  Reveal hidden contents

 

local NIGHTVISION_COLOURCUBES =
{
    day = "images/colour_cubes/ruins_light_cc.tex",
    dusk = "images/colour_cubes/ruins_dim_cc.tex",
    night = "images/colour_cubes/purple_moon_cc.tex",
    full_moon = "images/colour_cubes/purple_moon_cc.tex",
}

local function SetNightVision(inst, enable)
    if TheWorld.state.isnight or TheWorld:HasTag("cave") then
        inst.components.playervision:ForceNightVision(true)
        inst.components.playervision:SetCustomCCTable(NIGHTVISION_COLOURCUBES)
    else
        inst.components.playervision:ForceNightVision(false)
        inst.components.playervision:SetCustomCCTable(nil)
    end
end

-- This initializes for both the server and client. Tags can be added here.
local common_postinit = function(inst) 
    inst:WatchWorldState( "isday", function() SetNightVision(inst) end)
      inst:WatchWorldState( "isdusk", function() SetNightVision(inst) end)
      inst:WatchWorldState( "isnight", function() SetNightVision(inst)  end)
    inst:WatchWorldState( "iscaveday", function() SetNightVision(inst) end)
      inst:WatchWorldState( "iscavedusk", function() SetNightVision(inst) end)
      inst:WatchWorldState( "iscavenight", function() SetNightVision(inst)  end)
    
    SetNightVision(inst)
    end

 

is this wrong?

if so, what do I need to fix in order to provide night vision to the character?

thank you for your help!

You should just be able to change this part

 inst:WatchWorldState( "iscavenight", function() SetNightVision(inst)  end)

to

 inst:WatchWorldState( "iscavenight", SetNightVision)

I always recommend adding a Print inside of the function you're calling to see if it's being called or not.

Example would be

local function SetNightVision(inst, enable)

Print "SetNightVision has been called"

    if TheWorld.state.isnight or TheWorld:HasTag("cave") then

Print "Nightvision is on!"
        inst.components.playervision:ForceNightVision(true)
        inst.components.playervision:SetCustomCCTable(NIGHTVISION_COLOURCUBES)
    else

Print "Nightvision is off!"
        inst.components.playervision:ForceNightVision(false)
        inst.components.playervision:SetCustomCCTable(nil)
    end
end

You can check these messages when you're in a game by going to the command console (press the ` key just below Esc)

Share this post


Link to post
Share on other sites
7 hours ago, AkaiNight said:

try this 

 

2 hours ago, RedHairedHero said:

You should just be able to change this part

 inst:WatchWorldState( "iscavenight", function() SetNightVision(inst)  end)

to

 inst:WatchWorldState( "iscavenight", SetNightVision)

I always recommend adding a Print inside of the function you're calling to see if it's being called or not.

Example would be

local function SetNightVision(inst, enable)

Print "SetNightVision has been called"

    if TheWorld.state.isnight or TheWorld:HasTag("cave") then

Print "Nightvision is on!"
        inst.components.playervision:ForceNightVision(true)
        inst.components.playervision:SetCustomCCTable(NIGHTVISION_COLOURCUBES)
    else

Print "Nightvision is off!"
        inst.components.playervision:ForceNightVision(false)
        inst.components.playervision:SetCustomCCTable(nil)
    end
end

You can check these messages when you're in a game by going to the command console (press the ` key just below Esc)

I added both of yours, and now night vision works, thank you! but is there still a way to take damage from the darkness? I don't take damage at night anymore, and if I leave it that way, it makes the mod way too OP and kinda defeats the challenge of trying to keep a fire going lol.

thank you both so much for your help!

6 hours ago, DarkKingBoo said:

This should give you nightvision once it turns night (or when you enter caves).

Is it not working?

nope, it was not working before, but i fixed it. thanks for asking!

Share this post


Link to post
Share on other sites
11 hours ago, FrontierHero said:

 

I added both of yours, and now night vision works, thank you! but is there still a way to take damage from the darkness? I don't take damage at night anymore, and if I leave it that way, it makes the mod way too OP and kinda defeats the challenge of trying to keep a fire going lol.

thank you both so much for your help!

nope, it was not working before, but i fixed it. thanks for asking!

Being attacked by Charlie even with nightvision seems to be a bit redundant to me. You could always make a piece of equipment that grants nightvision instead, that way you need to at least recraft it or refuel it and give up an equipment slot for it. That would make it way more balanced.

Share this post


Link to post
Share on other sites
15 hours ago, RedHairedHero said:

Being attacked by Charlie even with nightvision seems to be a bit redundant to me. You could always make a piece of equipment that grants nightvision instead, that way you need to at least recraft it or refuel it and give up an equipment slot for it. That would make it way more balanced.

oh, well then thank you; i may end up replacing the night vision with another perk then, and do what you suggested; thank you!

Share this post


Link to post
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
Sign in to follow this