Jump to content

【beginner needs help】how to make effects while the character falls asleep


Recommended Posts

I added a Light component to my character and i want turn the light off while my character is asleep(using a tent or sleepbag).

I tried to listen for the events“onwakeup” and “gotosleep” written in components-sleeper but nothing happened.

 

Link to comment
Share on other sites

Please upload your mod or at least the relevant files, it'd be a million times easier to help you. My first hunch is that you're not turning the light off client-side, since it exists both on the server and client, but tha'ts just me assuming since I don't know anything about your code.

Link to comment
Share on other sites

45 minutes ago, ClumsyPenny said:

Please upload your mod or at least the relevant files, it'd be a million times easier to help you. My first hunch is that you're not turning the light off client-side, since it exists both on the server and client, but tha'ts just me assuming since I don't know anything about your code.

well firstly i  wrote these

local function onsleep(inst)
    inst.Light:Enable(false)
 
end
 
local function onwake(inst)
    inst.Light:Enable(true)
end

then I just added these under master_postinit

    inst:ListenForEvent("gotosleep",onsleep)
    inst:ListenForEvent("onwakeup",onwake)

but those things doesn't work 

 

to make sure if i got something wrong then i wrote this

local function healthCheck(inst,data)
    if data.newpercent < 0.75 then
        inst.Light:Enable(false)
    end
end

then i attached this function to the "healthdelta" event to test if it can turn off the light

then it just worked. the light was turned off.

so i'm wondering if "gotosleep" or "onwakeup" get pushed while player uses a tent or sleepbag

Link to comment
Share on other sites

3 hours ago, 5Y2h6YCa said:

so i'm wondering if "gotosleep" or "onwakeup" get pushed while player uses a tent or sleepbag

I looked into it, turns out that they're indeed used when falling asleep through the "sleeper" component, but players don't use that, they use "grogginess". The correct events for "grogginess" are "knockedout" and "cometo", respectively. I didn't know that either, you learn something new every day!

I will say again though, Light exists on the client too and your code is only running server side. You haven't ran into issues with that because you're likely hosting without Caves, meaning you're both client and server. If another player were to join or if you were to host with Caves, the Light wouldn't turn off visually (for either the player or you with Caves). If you don't wanna deal with the networking that comes with it, I recommend spawning and removing a prefab that only acts as light to turn it on and off, similarly to how the Lantern does it.

Link to comment
Share on other sites

14 minutes ago, ClumsyPenny said:

I looked into it, turns out that they're indeed used when falling asleep through the "sleeper" component, but players don't use that, they use "grogginess". The correct events for "grogginess" are "knockedout" and "cometo", respectively. I didn't know that either, you learn something new every day!

I will say again though, Light exists on the client too and your code is only running server side. You haven't ran into issues with that because you're likely hosting without Caves, meaning you're both client and server. If another player were to join or if you were to host with Caves, the Light wouldn't turn off visually (for either the player or you with Caves). If you don't wanna deal with the networking that comes with it, I recommend spawning and removing a prefab that only acts as light to turn it on and off, similarly to how the Lantern does it.

OHHHHHHH thanks for that. I'll check that later.

Link to comment
Share on other sites

Posted (edited)
1 hour ago, ClumsyPenny said:

I looked into it, turns out that they're indeed used when falling asleep through the "sleeper" component, but players don't use that, they use "grogginess". The correct events for "grogginess" are "knockedout" and "cometo", respectively. I didn't know that either, you learn something new every day!

I will say again though, Light exists on the client too and your code is only running server side. You haven't ran into issues with that because you're likely hosting without Caves, meaning you're both client and server. If another player were to join or if you were to host with Caves, the Light wouldn't turn off visually (for either the player or you with Caves). If you don't wanna deal with the networking that comes with it, I recommend spawning and removing a prefab that only acts as light to turn it on and off, similarly to how the Lantern does it.

I've just done few checks.

Those two events get pushed only when the character are literally "knocked out" (for example,cooking mandrake with a campfire), it's still not working while using a tent.

Truly learned something new though.

Edited by 5Y2h6YCa
Link to comment
Share on other sites

27 minutes ago, 5Y2h6YCa said:

Those two events get pushed only when the character are literally "knocked out" (for example,cooking mandrake with a campfire), it's still not working while using a tent.

Apologies, I literally missed you specifying that you meant sleeping as in using a Tent or Roll. My bad! But I think at least it'll still be useful for your character's light to turn off when knocked out.

Now, to address your problem once again: Tents and Rolls use the "sleepingbag" component, and players use the "sleepingbaguser" component to interact with them. Unfortunately, none of these have events being pushed and "sleepingbaguser" surprisingly doesn't have functions to use either. You'll have to resort to hooking into functions, I think something like this, goes in master_postinit:

if inst.components.sleepingbaguser ~= nil then
	local _old_dosleep = inst.components.sleepingbaguser.DoSleep
	
	inst.components.sleepingbaguser.DoSleep = function(self, bed, ...)
		-- Your code for turning off the light here
		
		_old_dosleep(self, bed, ...)
	end
	
	local _old_dowakeup = inst.components.sleepingbaguser.DoWakeUp
	
	inst.components.sleepingbaguser.DoWakeUp = function(self, nostatechange, ...)
		-- Your code for turning on the light here
		
		_old_dowakeup(self, nostatechange, ...)
	end
end

 

Link to comment
Share on other sites

6 hours ago, ClumsyPenny said:

Apologies, I literally missed you specifying that you meant sleeping as in using a Tent or Roll. My bad! But I think at least it'll still be useful for your character's light to turn off when knocked out.

Now, to address your problem once again: Tents and Rolls use the "sleepingbag" component, and players use the "sleepingbaguser" component to interact with them. Unfortunately, none of these have events being pushed and "sleepingbaguser" surprisingly doesn't have functions to use either. You'll have to resort to hooking into functions, I think something like this, goes in master_postinit:

if inst.components.sleepingbaguser ~= nil then
	local _old_dosleep = inst.components.sleepingbaguser.DoSleep
	
	inst.components.sleepingbaguser.DoSleep = function(self, bed, ...)
		-- Your code for turning off the light here
		
		_old_dosleep(self, bed, ...)
	end
	
	local _old_dowakeup = inst.components.sleepingbaguser.DoWakeUp
	
	inst.components.sleepingbaguser.DoWakeUp = function(self, nostatechange, ...)
		-- Your code for turning on the light here
		
		_old_dowakeup(self, nostatechange, ...)
	end
end

 

Got it. thanks for help again.

  • GL Happy 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...