Jump to content

A Question about Sleep


FrimpDimp

Recommended Posts

I'm learning scripting from square one, and I feel like a neanderthal staring at my screen and trying to decipher most of the strings I see. Though I am chuffing along on a character mod, there is something I've not been able to find any information on. Are there any flags that using a straw roll/tent/mandrake to sleep would set off?

I want for the character to require sleep every 2-3 days, and to get a continuous sanity drain if they go past that limit. Does the game have any values set for having slept a night, that I could trigger a sanity drain if not met in a certain amount of time?

Alternatively, I'm willing to shill out $5-$10 USD for anyone who could help me build this character in general. Thanks for any consideration, cheers

Link to comment
Share on other sites

I'm not aware of any character specific events that trigger when you sleep, nor any variable that keeps a count of how long you've been without sleep... That's not to say they don't exist though.

There is other code that could in theory be used, like StateGraphInstance.GoToState with wakeup param. It can be utilized like the following in your characters prefab:

  

local StateWake_old = StateGraphInstance.GoToState
    StateGraphInstance.GoToState = function(statename,params)
        if params =="wakeup" then
            --Our code to reset sleep timer goes here
        end
    StateWake_old(statename,params)
    end

This isn't ideal though as StateGraphInstance.GoToState is ran when most anything in game changes state, so the if statement would be running many times per second. Also I believe the "wakeup" param is used when your character jumps into a wormhole or uses a teleport rod too.

 

Another way round is to have the various sleep items in the game push an event to the player when they're successfully used. You could do this using AddPrefabPostInit in modmain. As an example:

local function DidWeSleep(inst)     
    local p = GLOBAL.GetPlayer()   
    if inst and inst.components and p and p.prefab=="YourCharacterName" then        
        local SleepFunction_Ptr = inst.components.sleepingbag.onsleep   
        inst.components.sleepingbag.onsleep = function(inst)                   
            if inst.components.finiteuses then                       
                if inst.components.finiteuses:GetUses() == 1 and inst.prefab=="bedroll_furry" then                         
                    p:PushEvent("weslept")                  
                else                        
                    local function eventfunc (inst) inst:RemoveEventCallback("percentusedchange", eventfunc) p:PushEvent("weslept") end  
                    inst:ListenForEvent("percentusedchange",eventfunc)   
                end                                         
            else
                p:PushEvent("weslept")      
            end
	    SleepFunction_Ptr(inst, p)                                                       
        end   
    end
end


AddPrefabPostInit("tent", DidWeSleep)
AddPrefabPostInit("siestahut", DidWeSleep)
AddPrefabPostInit("bedroll_furry", DidWeSleep)
AddPrefabPostInit("bedroll_straw", DidWeSleep)

As a basic overview it's overwriting the items onsleep code with our function. Our function checks whether the item is a multiple use item, and if so has it listen for the "percentusedchange" event fired by finiteuses component. If the percentusedchange event is received we know the item has been used so we send a pushevent to our character. If it's not a finite use item (strawroll) we just send the pushevent. Once done we run the entities old onsleep function.

The only anomaly is the bedroll_furry which doesn't cause percentusedchange event to fire when on its last use, so that's specifically coded for.

Then in your character prefab you listen for the event:

local function slept(inst)
    --We just slept, adjust the last slept variable and reset sanity drain
end
    
inst:ListenForEvent("weslept", function() slept(inst) end)

 

You can add your own variable and update each time the sleep event is detected, then have a DoTaskInTime checking how long it's been since the last sleep.

Mandrakes didn't really fit into the above function, but they could be tackled in the same manner as above.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

Please be aware that the content of this thread may be outdated and no longer applicable.

×
  • Create New...