Jump to content

Code help


Recommended Posts

Hello, I need help with how to do something properly... So you see, when it's raining my character loses sanity but when he goes under a tree I want it to stop so I did this...

if TheWorld.state.israining then
if inst:ListenForEvent("sheltered", function(inst, data) then
inst.components.sanity.dapperness = 0 * TUNING.DAPPERNESS_MED
else
inst.components.health.fire_damage_scale = 1
inst.components.sanity.dapperness = -TUNING.DAPPERNESS_MED
end
end
end)
	
inst:WatchWorldState("startrain", function()
if inst:ListenForEvent("sheltered", function(inst, data) then
inst.components.sanity.dapperness = 0 * TUNING.DAPPERNESS_MED
else
inst.components.talker:Say(GetString(inst, "ANNOUNCE_RAIN"))
inst.components.sanity.dapperness = -TUNING.DAPPERNESS_MED
inst.components.health.fire_damage_scale = 1
end
end)
end)

but it doesn't work... So, help would be greatly appreciated :)!

Link to comment
Share on other sites

You are mixing together setting up listeners with checking for values. Try this:

-- update stats based on rain & sheltered status
local function updateStats_Rain(inst)
    if TheWorld.state.israining then
        if inst.components.sheltered.sheltered then
            -- sheltered during the rain
            inst.components.sanity.dapperness = 0 * TUNING.DAPPERNESS_MED
        else
            -- rain without shelter
            inst.components.sanity.dapperness = -TUNING.DAPPERNESS_MED
        end
        inst.components.health.fire_damage_scale = 1
    else
        -- no rain
        inst.components.sanity.dapperness = 0 * TUNING.DAPPERNESS_MED
        inst.components.health.fire_damage_scale = 1.25
    end
end
    
-- Update stats if it's already raining while logging into server.
updateStats_Rain(inst)
    
-- Rain start while on server.
inst:WatchWorldState("startrain", function()
    inst.components.talker:Say(GetString(inst, "ANNOUNCE_RAIN"))
    updateStats_Rain(inst)
end)    
    
-- Rain end while on server.
inst:WatchWorldState("stoprain", function()
    inst.components.talker:Say(GetString(inst, "ANNOUNCE_STOPRAIN"))
    updateStats_Rain(inst)
end)
    
-- You're hiding.
inst:ListenForEvent("sheltered", function(inst, data)
    if data then
        inst.components.moisture.inherentWaterproofness = 999
        inst.components.temperature.inherentsummerinsulation = 10
        local enemy = FindEntity(inst, 30, function(guy) return guy.components.combat ~= nil and guy.components.combat:TargetIs(inst) end)
        if enemy == nil then
            inst:AddTag("notarget")
        end
    else
        inst:RemoveTag("notarget")
        inst.components.moisture.inherentWaterproofness = 0
        inst.components.temperature.inherentsummerinsulation = -72.5
    end
    updateStats_Rain(inst)
end)

 

Link to comment
Share on other sites

Hello Muche I just have 1 question you can maybe help with, that code you gave me 

local function updateStats_Rain(inst)
if TheWorld.state.israining then
if inst.components.sheltered.sheltered then
inst.components.sanity.dapperness = 0 * TUNING.DAPPERNESS_MED
else
inst.components.sanity.dapperness = 2 * -TUNING.DAPPERNESS_MED
end
inst.components.health.fire_damage_scale = 1
else
inst.components.sanity.dapperness = 0 * TUNING.DAPPERNESS_MED
inst.components.health.fire_damage_scale = 1.25
end
end
    
updateStats_Rain(inst)
    
inst:WatchWorldState("startrain", function()
inst.components.talker:Say(GetString(inst, "ANNOUNCE_RAIN"))
updateStats_Rain(inst)
end)    
    
inst:WatchWorldState("stoprain", function()
inst.components.talker:Say(GetString(inst, "ANNOUNCE_STOPRAIN"))
updateStats_Rain(inst)
end)

my character doesn't lose sanity when logging into server that it's already raining, it only activates when it starts raining on the server when you where already on it, is there a way to fix that or no? And thank you :).

Link to comment
Share on other sites

Could you try this:

local function updateStats_Rain(inst)
    print(string.format("[Adam|updateStats_Rain(%s)]#0 israining=%s, sheltered=%s, dapperness=%s", tostring(inst), tostring(TheWorld.state.israining), tostring(inst.components.sheltered.sheltered), tostring(inst.components.sanity.dapperness)))
    if TheWorld.state.israining then
        if inst.components.sheltered.sheltered then
            inst.components.sanity.dapperness = 0 * TUNING.DAPPERNESS_MED
            print(string.format("[Adam|updateStats_Rain(%s)]#1 sheltered in rain; israining=%s, sheltered=%s, dapperness=%s", tostring(inst), tostring(TheWorld.state.israining), tostring(inst.components.sheltered.sheltered), tostring(inst.components.sanity.dapperness)))
        else
            inst.components.sanity.dapperness = 2 * -TUNING.DAPPERNESS_MED
            print(string.format("[Adam|updateStats_Rain(%s)]#2 in rain without shelter; israining=%s, sheltered=%s, dapperness=%s", tostring(inst), tostring(TheWorld.state.israining), tostring(inst.components.sheltered.sheltered), tostring(inst.components.sanity.dapperness)))
        end
        inst.components.health.fire_damage_scale = 1
    else
        inst.components.sanity.dapperness = 0 * TUNING.DAPPERNESS_MED
        inst.components.health.fire_damage_scale = 1.25
        print(string.format("[Adam|updateStats_Rain(%s)]#3 no rain; israining=%s, sheltered=%s, dapperness=%s", tostring(inst), tostring(TheWorld.state.israining), tostring(inst.components.sheltered.sheltered), tostring(inst.components.sanity.dapperness)))
    end
end

print("[Adam|master_postinit] about to run updateStats_Rain...")
updateStats_Rain(inst)

inst:DoTaskInTime(0, function(inst)
    print("[Adam|master_postinit|TaskInTime_0] about to run updateStats_Rain...")
    updateStats_Rain(inst)
end)

inst:WatchWorldState("startrain", function()
    inst.components.talker:Say(GetString(inst, "ANNOUNCE_RAIN"))
    print("[Adam|master_postinit|startrain] about to run updateStats_Rain...")
    updateStats_Rain(inst)
end)

inst:WatchWorldState("stoprain", function()
    inst.components.talker:Say(GetString(inst, "ANNOUNCE_STOPRAIN"))
    print("[Adam|master_postinit|stoprain] about to run updateStats_Rain...")
    updateStats_Rain(inst)
end)

and then post logs from both scenarios, to see which branch is setting incorrect dapperness?

Edited by Muche
added an extra debug print
Link to comment
Share on other sites

I'm sorry but i'm kinda a noob so by post logs do you mean what happens in debug console? If so then I put the picture if not then can you tell me how I go to logs, so sorry for being so noobish :( and thank you for your help :D!

1.jpg

Link to comment
Share on other sites

The log is your friend when trying to debug anything. Sometimes it's good to know what is there (because you've added a debug print with values of some variables), sometimes what is supposed to be there and isn't (because you've added a debug print into a function and trying to find out if/when it is actually run).

Client_log (if you're the client or are hosting a single-level world) can be found here:
PC: Documents\Klei\DoNotStarveTogether\client_log.txt
Mac: ~/Documents/Klei/DoNotStarveTogether/client_log.txt
Linux: ~/.klei/DoNotStarveTogether/client_log.txt

Server_log (if you're hosting a multi-level world):
PC: Documents\Klei\DoNotStarveTogether\Cluster_{N}\Master\server_log.txt
PC: Documents\Klei\DoNotStarveTogether\Cluster_{N}\Caves\server_log.txt
where {N} is the number of the save slot.

Also I've added an extra debug print into updateStats_Rain function above.

Link to comment
Share on other sites

Okay I went in logs here's everything that happened with rain

 

[00:00:21]: setting     israining    true

[00:00:21]: setting     precipitation    rain    

[00:00:25]: [Adam|master_postinit] about to run updateStats_Rain...    

[00:00:25]: [Adam|updateStats_Rain(111501 - )]#2 in rain without shelter; israining=true, sheltered=false, dapperness=-0.11111111111111  

[00:00:26]: [Adam|master_postinit|TaskInTime_0] about to run updateStats_Rain...     

[00:00:26]: [Adam|updateStats_Rain(111501 - adam)]#2 in rain without shelter; israining=true, sheltered=false, dapperness=-0.11111111111111    

[00:00:26]: [Adam|updateStats_Rain(111501 - adam)]#1 sheltered in rain; israining=true, sheltered=true, dapperness=0    

[00:00:28]: [Adam|updateStats_Rain(111501 - adam)]#2 in rain without shelter; israining=true, sheltered=false, dapperness=-0.11111111111111    

Is this enough or should I post the whole log or look for something more specific :)? And thank you :D!

Link to comment
Share on other sites

I see no messages containing:

[Adam|updateStats_Rain(...)]#0
[Adam|master_postinit|startrain]

Could you also add the following into sheltered event listener (just before updateStats_Rain call):

print("[Adam|sheltered_event_listener] about to run updateStats_Rain...")

 

Edited by Muche
Link to comment
Share on other sites

Muche I figured out what the problem is, you see the rain code you gave me I edited it to make one for snow too 

-- You're hiding.
inst:ListenForEvent("sheltered", function(inst, data)
if data then
inst.components.moisture.inherentWaterproofness = 999
inst.components.temperature.inherentsummerinsulation = 10
local enemy = FindEntity(inst, 30, function(guy) return guy.components.combat ~= nil and guy.components.combat:TargetIs(inst) end)
if enemy == nil then
inst:AddTag("notarget")
end
else
inst:RemoveTag("notarget")
inst.components.moisture.inherentWaterproofness = 0
inst.components.temperature.inherentsummerinsulation = -72.5
end
print("[Adam|sheltered_event_listener] about to run updateStats_Rain...")
updateStats_Rain(inst)
updateStats_Snow(inst) --This is what's causing the problem, please tell me correct way to put this.
end)

I thought that the updateStats_Snow(inst) would go in the same place as the rain one but it seems they can't, do you know a way to allow them to coexist without causing problems for each other :)? And sorry for the trouble!

Link to comment
Share on other sites

Well, I don't know what the updateStats_Snow function is doing exactly and where it is being called, so I have no way of knowing why they conflict.

Maybe both updateStats function can combined into one updateStats_RainSnow?

Link to comment
Share on other sites

This is updatesnow is the same as rain 

-- Update snow & shelter status.
local function updateStats_Snow(inst)
if TheWorld.state.issnowing then
if inst.components.sheltered.sheltered then
-- Sheltered during snow.
inst.components.sanity.dapperness = 0 * TUNING.DAPPERNESS_MED
else
-- Snow without shelter.
inst.components.sanity.dapperness = 2 * -TUNING.DAPPERNESS_MED
end
inst.components.health.fire_damage_scale = 1.15
else
-- No snow.
inst.components.sanity.dapperness = 0 * TUNING.DAPPERNESS_MED
inst.components.health.fire_damage_scale = 1.25
end
end
   
-- Snow start while logging on server.
updateStats_Snow(inst)
inst:DoTaskInTime(0, function(inst)
updateStats_Snow(inst)
end)
    
-- Snow start while on server.
inst:WatchWorldState("startsnow", function()
inst.components.talker:Say(GetString(inst, "ANNOUNCE_SNOW"))
updateStats_Snow(inst)
end)    
    
-- Snow end while on server.
inst:WatchWorldState("stopsnow", function()
inst.components.talker:Say(GetString(inst, "ANNOUNCE_STOPSNOW"))
updateStats_Snow(inst)
end)

 

Oh no :shock:! I know why it doesn't work because updateStats_Snow checks if there's snow and if there isn't it removes negetive dapper so when it's raining updateStats_Snow removes the dapper because it's not snowing and updateStats_Rain removes the snow dapper because it's not raining, oh my god this's probably not fixable, right :(?

Link to comment
Share on other sites

Try this:

-- update stats based on rain, snow & sheltered status
local function updateStats_RainSnow(inst)
    print(string.format("[Adam|updateStats_RainSnow(%s)]#0 israining=%s, issnowing=%s, sheltered=%s, dapperness=%s", tostring(inst), tostring(TheWorld.state.israining), tostring(TheWorld.state.issnowing), tostring(inst.components.sheltered.sheltered), tostring(inst.components.sanity.dapperness)))
    if TheWorld.state.israining then
        if inst.components.sheltered.sheltered then
            -- sheltered during the rain
            inst.components.sanity.dapperness = 0 * TUNING.DAPPERNESS_MED
            print(string.format("[Adam|updateStats_RainSnow(%s)]#1 sheltered during rain; israining=%s, issnowing=%s, sheltered=%s, dapperness=%s", tostring(inst), tostring(TheWorld.state.israining), tostring(TheWorld.state.issnowing), tostring(inst.components.sheltered.sheltered), tostring(inst.components.sanity.dapperness)))
        else
            -- rain without shelter
            inst.components.sanity.dapperness = 2 * -TUNING.DAPPERNESS_MED
            print(string.format("[Adam|updateStats_RainSnow(%s)]#2 in rain without shelter; israining=%s, issnowing=%s, sheltered=%s, dapperness=%s", tostring(inst), tostring(TheWorld.state.israining), tostring(TheWorld.state.issnowing), tostring(inst.components.sheltered.sheltered), tostring(inst.components.sanity.dapperness)))
        end
        inst.components.health.fire_damage_scale = 1
    elseif TheWorld.state.issnowing then
        if inst.components.sheltered.sheltered then
            -- Sheltered during snow.
            inst.components.sanity.dapperness = 0 * TUNING.DAPPERNESS_MED
            print(string.format("[Adam|updateStats_RainSnow(%s)]#3 sheltered during snow; israining=%s, issnowing=%s, sheltered=%s, dapperness=%s", tostring(inst), tostring(TheWorld.state.israining), tostring(TheWorld.state.issnowing), tostring(inst.components.sheltered.sheltered), tostring(inst.components.sanity.dapperness)))
        else
            -- Snow without shelter.
            inst.components.sanity.dapperness = 2 * -TUNING.DAPPERNESS_MED
            print(string.format("[Adam|updateStats_RainSnow(%s)]#4 snow without shelter; israining=%s, issnowing=%s, sheltered=%s, dapperness=%s", tostring(inst), tostring(TheWorld.state.israining), tostring(TheWorld.state.issnowing), tostring(inst.components.sheltered.sheltered), tostring(inst.components.sanity.dapperness)))
        end
        inst.components.health.fire_damage_scale = 1.15
    else
        -- no rain or snow
        inst.components.sanity.dapperness = 0 * TUNING.DAPPERNESS_MED
        inst.components.health.fire_damage_scale = 1.25
        print(string.format("[Adam|updateStats_RainSnow(%s)]#5 no rain or snow; israining=%s, issnowing=%s, sheltered=%s, dapperness=%s", tostring(inst), tostring(TheWorld.state.israining), tostring(TheWorld.state.issnowing), tostring(inst.components.sheltered.sheltered), tostring(inst.components.sanity.dapperness)))
    end
end


-- Update stats if it's already raining or snowing while logging into server.
print("[Adam|master_postinit] about to run updateStats_Rain...")
updateStats_RainSnow(inst)

-- Update stats if it's already raining or snowing while logging into server - test doing it in the next frame if there is some funky conflict.
inst:DoTaskInTime(0, function(inst)
    print("[Adam|master_postinit|TaskInTime_0] about to run updateStats_RainSnow...")
    updateStats_RainSnow(inst)
end)


-- Rain start while on server.
inst:WatchWorldState("startrain", function()
    inst.components.talker:Say(GetString(inst, "ANNOUNCE_RAIN"))
    print("[Adam|master_postinit|startrain] about to run updateStats_RainSnow...")
    updateStats_RainSnow(inst)
end)
    
-- Rain end while on server.
inst:WatchWorldState("stoprain", function()
    inst.components.talker:Say(GetString(inst, "ANNOUNCE_STOPRAIN"))
    print("[Adam|master_postinit|stoprain] about to run updateStats_RainSnow...")
    updateStats_RainSnow(inst)
end)

-- Snow start while on server.
inst:WatchWorldState("startsnow", function()
    inst.components.talker:Say(GetString(inst, "ANNOUNCE_SNOW"))
    print("[Adam|master_postinit|startsnow] about to run updateStats_RainSnow...")
    updateStats_RainSnow(inst)
end)

-- Snow end while on server.
inst:WatchWorldState("stopsnow", function()
    inst.components.talker:Say(GetString(inst, "ANNOUNCE_STOPSNOW"))
    print("[Adam|master_postinit|stopsnow] about to run updateStats_RainSnow...")
    updateStats_RainSnow(inst)
end)
    
-- You're hiding.
inst:ListenForEvent("sheltered", function(inst, data)
    if data then
        inst.components.moisture.inherentWaterproofness = 999
        inst.components.temperature.inherentsummerinsulation = 10
        local enemy = FindEntity(inst, 30, function(guy) return guy.components.combat ~= nil and guy.components.combat:TargetIs(inst) end)
        if enemy == nil then
            inst:AddTag("notarget")
        end
    else
        inst:RemoveTag("notarget")
        inst.components.moisture.inherentWaterproofness = 0
        inst.components.temperature.inherentsummerinsulation = -72.5
    end
    print("[Adam|sheltered_event_listener] about to run updateStats_RainSnow...")
    updateStats_RainSnow(inst)
end)

 

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