Jump to content

is there a way to give my character night vision above a specific sanity?


Recommended Posts

i'm new to coding, but one of my character's perks is that he has night vision; however, only when his sanity is above 70%. Is there a way to have night vision be toggled above a specific sanity? I'm not sure how to even begin with this.

ty in advance! 

Link to comment
Share on other sites

In your character's postinit.  If you want him to immediately start/spawn with it, also include the night vision thing turning on in the post init.

inst:ListenForEvent(
    "sanitydelta",
    function(data)
        if data
        then
            if data.newpercent > 0.7 and data.oldpercent <= 0.7
            then
                inst.components.playervision:ForceNightVision(true)
            elseif data.newpercent <= 0.7 and data.oldpercent > 0.7
            then
                inst.components.playervision:ForceNightVision(false)
            end
        end
    end
)
Edited by CarlZalph
Tabs to spaces.
Link to comment
Share on other sites

On 12/4/2018 at 6:17 PM, CarlZalph said:

In your character's postinit.  If you want him to immediately start/spawn with it, also include the night vision thing turning on in the post init.


inst:ListenForEvent(
    "sanitydelta",
    function(data)
        if data
        then
            if data.newpercent > 0.7 and data.oldpercent <= 0.7
            then
                inst.components.playervision:ForceNightVision(true)
            elseif data.newpercent <= 0.7 and data.oldpercent > 0.7
            then
                inst.components.playervision:ForceNightVision(false)
            end
        end
    end
)

hi, thanks for your help, but putting this code into the postinit seems to make the mod crash - "variable 'inst' not found" 

Link to comment
Share on other sites

14 hours ago, CarlZalph said:

'inst' refers to your player prefab.

hi, 

I apologize but I'm not quite understanding what you mean; I'm very new to coding. does this variable need to be declared somehow in the character name's prefab file?

thanks so much for your patience 

Link to comment
Share on other sites

2 hours ago, Ultroman said:

You probably want to do:


function(inst, data)

 

I put in 

    function(inst, data)
        if data
        then
            if data.newpercent > 0.7 and data.oldpercent <= 0.7
            then
                inst.components.playervision:ForceNightVision(true)
            elseif data.newpercent <= 0.7 and data.oldpercent > 0.7
            then
                inst.components.playervision:ForceNightVision(false)
            end
        end
    end
)

but then the server crashed, telling me that a name was expected after the word function. So i named it something and played around with it and got it not to crash, but the code didn't have any effect on the night vision.
 

I think I'll simply leave the night vision unaffected by sanity; it seems like too big a chore for a newbie coder like me. Thanks for taking the time to help me, though. I really appreciate it!

Link to comment
Share on other sites

That doesn't make any sense. It would be better if you posted the errors you get, because some times you shouldn't blindly listen to them.

Also, you didn't post your full code this time, so it looks like you removed the ListenForEvent line. We can help better, if we have the full mod and the full error logs.

Edited by Ultroman
Link to comment
Share on other sites

4 hours ago, Ultroman said:

That doesn't make any sense. It would be better if you posted the errors you get, because some times you shouldn't blindly listen to them.

Also, you didn't post your full code this time, so it looks like you removed the ListenForEvent line. We can help better, if we have the full mod and the full error logs.

ok! I'll go ahead and go step-by-step on everything I did, and include the files and error messages upon crashes.
This is the original code, with just the night vision from the thread you linked. I used that code, but changed it to use woodie's beaver form night vision instead of making it look like there is a full moon every night. Everything worked fine; no crashes, and the night vision showed up (tested it on a Lights Out world).

Spoiler

local MakePlayerCharacter = require "prefabs/player_common"


local assets = {
    Asset("SCRIPT", "scripts/prefabs/player_common.lua"),
}
local prefabs = {}

-- Custom starting inventory
local start_inv = {
}

-- When the character is revived from human
local function onbecamehuman(inst)
	-- Set speed when not a ghost (optional)
	inst.components.locomotor:SetExternalSpeedMultiplier(inst, "otus_speed_mod", 1.5)
end

local function onbecameghost(inst)
	-- Remove speed modifier when becoming a ghost
   inst.components.locomotor:RemoveExternalSpeedMultiplier(inst, "otus_speed_mod")
end

-- When loading or spawning the character
local function onload(inst)
    inst:ListenForEvent("ms_respawnedfromghost", onbecamehuman)
    inst:ListenForEvent("ms_becameghost", onbecameghost)

    if inst:HasTag("playerghost") then
        onbecameghost(inst)
    else
        onbecamehuman(inst)
    end
end

-- This initializes for both the server and client. Tags can be added here.
local common_postinit = function(inst) 
	-- Minimap icon
	inst.MiniMapEntity:SetIcon( "otus.tex" )
end

local NIGHTVISION_COLOURCUBES =
{
    day = "images/colour_cubes/ruins_light_cc.tex",
    dusk = "images/colour_cubes/ruins_dim_cc.tex",
    night = "images/colour_cubes/beaver_vision_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
	
-- This initializes for the server only. Components are added here.
local master_postinit = function(inst)
	-- choose which sounds this character will play
	inst.soundsname = "willow"
	
	-- Uncomment if "wathgrithr"(Wigfrid) or "webber" voice is used
    --inst.talker_path_override = "dontstarve_DLC001/characters/"
	
	-- Stats	
	inst.components.health:SetMaxHealth(150)
	inst.components.hunger:SetMax(150)
	inst.components.sanity:SetMax(200)
	
	-- Damage multiplier (optional)
    inst.components.combat.damagemultiplier = 1
	
	-- Hunger rate (optional)
	inst.components.hunger.hungerrate = 1 * TUNING.WILSON_HUNGER_RATE
	
	inst.OnLoad = onload
    inst.OnNewSpawn = onload
	
	
end

return MakePlayerCharacter("otus", prefabs, assets, common_postinit, master_postinit, start_inv)

 

  Now here's the code when I paste the first code given to me by CarlZalph (new code is in blue)

Spoiler

local MakePlayerCharacter = require "prefabs/player_common"


local assets = {
    Asset("SCRIPT", "scripts/prefabs/player_common.lua"),
}
local prefabs = {}

-- Custom starting inventory
local start_inv = {
}

-- When the character is revived from human
local function onbecamehuman(inst)
    -- Set speed when not a ghost (optional)
    inst.components.locomotor:SetExternalSpeedMultiplier(inst, "otus_speed_mod", 1.5)
end

local function onbecameghost(inst)
    -- Remove speed modifier when becoming a ghost
   inst.components.locomotor:RemoveExternalSpeedMultiplier(inst, "otus_speed_mod")
end

-- When loading or spawning the character
local function onload(inst)
    inst:ListenForEvent("ms_respawnedfromghost", onbecamehuman)
    inst:ListenForEvent("ms_becameghost", onbecameghost)

    if inst:HasTag("playerghost") then
        onbecameghost(inst)
    else
        onbecamehuman(inst)
    end
end

-- This initializes for both the server and client. Tags can be added here.
local common_postinit = function(inst) 
    -- Minimap icon
    inst.MiniMapEntity:SetIcon( "otus.tex" )
end

local NIGHTVISION_COLOURCUBES =
{
    day = "images/colour_cubes/ruins_light_cc.tex",
    dusk = "images/colour_cubes/ruins_dim_cc.tex",
    night = "images/colour_cubes/beaver_vision_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
  
  
inst:ListenForEvent(
    "sanitydelta",
    function(data)
        if data
        then
            if data.newpercent > 0.7 and data.oldpercent <= 0.7
            then
                inst.components.playervision:ForceNightVision(true)
            elseif data.newpercent <= 0.7 and data.oldpercent > 0.7
            then
                inst.components.playervision:ForceNightVision(false)
            end
        end
    end
)
    

-- This initializes for the server only. Components are added here.
local master_postinit = function(inst)
    -- choose which sounds this character will play
    inst.soundsname = "willow"
    
    -- Uncomment if "wathgrithr"(Wigfrid) or "webber" voice is used
    --inst.talker_path_override = "dontstarve_DLC001/characters/"
    
    -- Stats    
    inst.components.health:SetMaxHealth(150)
    inst.components.hunger:SetMax(150)
    inst.components.sanity:SetMax(200)
    
    -- Damage multiplier (optional)
    inst.components.combat.damagemultiplier = 1
    
    -- Hunger rate (optional)
    inst.components.hunger.hungerrate = 1 * TUNING.WILSON_HUNGER_RATE
    
    inst.OnLoad = onload
    inst.OnNewSpawn = onload
    
    
end

return MakePlayerCharacter("otus", prefabs, assets, common_postinit, master_postinit, start_inv)
 

This crashed the server upon attempting to enter, bringing up this error message

Spoiler

image.thumb.png.8ecd30982273d639fb1e46f4d080a421.png

So then I tried the same code, but I removed the :"inst: ListenForEvent" portion, along with the sanitydelta portion, and added "inst" before "data": 

Spoiler

local MakePlayerCharacter = require "prefabs/player_common"


local assets = {
    Asset("SCRIPT", "scripts/prefabs/player_common.lua"),
}
local prefabs = {}

-- Custom starting inventory
local start_inv = {
}

-- When the character is revived from human
local function onbecamehuman(inst)
    -- Set speed when not a ghost (optional)
    inst.components.locomotor:SetExternalSpeedMultiplier(inst, "otus_speed_mod", 1.5)
end

local function onbecameghost(inst)
    -- Remove speed modifier when becoming a ghost
   inst.components.locomotor:RemoveExternalSpeedMultiplier(inst, "otus_speed_mod")
end

-- When loading or spawning the character
local function onload(inst)
    inst:ListenForEvent("ms_respawnedfromghost", onbecamehuman)
    inst:ListenForEvent("ms_becameghost", onbecameghost)

    if inst:HasTag("playerghost") then
        onbecameghost(inst)
    else
        onbecamehuman(inst)
    end
end

-- This initializes for both the server and client. Tags can be added here.
local common_postinit = function(inst) 
    -- Minimap icon
    inst.MiniMapEntity:SetIcon( "otus.tex" )
end

local NIGHTVISION_COLOURCUBES =
{
    day = "images/colour_cubes/ruins_light_cc.tex",
    dusk = "images/colour_cubes/ruins_dim_cc.tex",
    night = "images/colour_cubes/beaver_vision_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
  
    function(inst, data)
        if data
        then
            if data.newpercent > 0.7 and data.oldpercent <= 0.7
            then
                inst.components.playervision:ForceNightVision(true)
            elseif data.newpercent <= 0.7 and data.oldpercent > 0.7
            then
                inst.components.playervision:ForceNightVision(false)
            end
        end
    end


    
-- This initializes for the server only. Components are added here.
local master_postinit = function(inst)
    -- choose which sounds this character will play
    inst.soundsname = "willow"
    
    -- Uncomment if "wathgrithr"(Wigfrid) or "webber" voice is used
    --inst.talker_path_override = "dontstarve_DLC001/characters/"
    
    -- Stats    
    inst.components.health:SetMaxHealth(150)
    inst.components.hunger:SetMax(150)
    inst.components.sanity:SetMax(200)
    
    -- Damage multiplier (optional)
    inst.components.combat.damagemultiplier = 1
    
    -- Hunger rate (optional)
    inst.components.hunger.hungerrate = 1 * TUNING.WILSON_HUNGER_RATE
    
    inst.OnLoad = onload
    inst.OnNewSpawn = onload
    
    
end

return MakePlayerCharacter("otus", prefabs, assets, common_postinit, master_postinit, start_inv)

That crashed the server, telling me the function needed a name: 

Spoiler

image.thumb.png.28f2aafaaa2134764456c69215aa1bcb.png

So I named it something random; I think it was something like "NightVisionSanity" or something like that: 

Spoiler

local MakePlayerCharacter = require "prefabs/player_common"


local assets = {
    Asset("SCRIPT", "scripts/prefabs/player_common.lua"),
}
local prefabs = {}

-- Custom starting inventory
local start_inv = {
}

-- When the character is revived from human
local function onbecamehuman(inst)
    -- Set speed when not a ghost (optional)
    inst.components.locomotor:SetExternalSpeedMultiplier(inst, "otus_speed_mod", 1.5)
end

local function onbecameghost(inst)
    -- Remove speed modifier when becoming a ghost
   inst.components.locomotor:RemoveExternalSpeedMultiplier(inst, "otus_speed_mod")
end

-- When loading or spawning the character
local function onload(inst)
    inst:ListenForEvent("ms_respawnedfromghost", onbecamehuman)
    inst:ListenForEvent("ms_becameghost", onbecameghost)

    if inst:HasTag("playerghost") then
        onbecameghost(inst)
    else
        onbecamehuman(inst)
    end
end

-- This initializes for both the server and client. Tags can be added here.
local common_postinit = function(inst) 
    -- Minimap icon
    inst.MiniMapEntity:SetIcon( "otus.tex" )
end

local NIGHTVISION_COLOURCUBES =
{
    day = "images/colour_cubes/ruins_light_cc.tex",
    dusk = "images/colour_cubes/ruins_dim_cc.tex",
    night = "images/colour_cubes/beaver_vision_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
  
    function NightVisionSanity(inst, data)
        if data
        then
            if data.newpercent > 0.7 and data.oldpercent <= 0.7
            then
                inst.components.playervision:ForceNightVision(true)
            elseif data.newpercent <= 0.7 and data.oldpercent > 0.7
            then
                inst.components.playervision:ForceNightVision(false)
            end
        end
    end
    
-- This initializes for the server only. Components are added here.
local master_postinit = function(inst)
    -- choose which sounds this character will play
    inst.soundsname = "willow"
    
    -- Uncomment if "wathgrithr"(Wigfrid) or "webber" voice is used
    --inst.talker_path_override = "dontstarve_DLC001/characters/"
    
    -- Stats    
    inst.components.health:SetMaxHealth(150)
    inst.components.hunger:SetMax(150)
    inst.components.sanity:SetMax(200)
    
    -- Damage multiplier (optional)
    inst.components.combat.damagemultiplier = 1
    
    -- Hunger rate (optional)
    inst.components.hunger.hungerrate = 1 * TUNING.WILSON_HUNGER_RATE
    
    inst.OnLoad = onload
    inst.OnNewSpawn = onload
    
    
end

return MakePlayerCharacter("otus", prefabs, assets, common_postinit, master_postinit, start_inv)

That didn't crash the server, but the sanity has no effect on the night vision: 

Spoiler

image.thumb.png.323846c6c5c17986368ec35333177319.png

I'm guessing it has something to do with the fact that "ListenForEvent" is not there, but every time I try to include it, it seems to crash the server... and being so new at this I have no clue how to place it into the code or use it.

Please let me know if you need anything else aside from the file text and error messages! 

Link to comment
Share on other sites

You can't just paste in code anywhere in the file, like you did with Carl's code-snippet. It has to be in the right place, in order to be in the right context. Go back to the code you had before you added Carl's code. Then add Carl's code at the bottom of your master_postinit function. That should work.

Link to comment
Share on other sites

14 minutes ago, Ultroman said:

You can't just paste in code anywhere in the file, like you did with Carl's code-snippet. It has to be in the right place, in order to be in the right context. Go back to the code you had before you added Carl's code. Then add Carl's code at the bottom of your master_postinit function. That should work.

 

Spoiler

inst:ListenForEvent(
    "sanitydelta",
    function(inst, data)
        if data
        then
            if data.newpercent > 0.7 and data.oldpercent <= 0.7
            then
                inst.components.playervision:ForceNightVision(true)
            elseif data.newpercent <= 0.7 and data.oldpercent > 0.7
            then
                inst.components.playervision:ForceNightVision(false)
            end
        end
    end
)

this, under the master_postinit, worked; thanks for your help. 

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...