Jump to content

Recommended Posts

So. Whenever a character that emits light ( Example: A character with a lightbulb for a head ) dies and gets revived in DST, it comes back... without the light. At least, for me.

Here's what I did-- I'm probably forgetting something.
 

local function stuff(inst)	inst.entity:AddLight()    inst.Light:SetRadius(2.5)    inst.Light:SetFalloff(.5)    inst.Light:SetIntensity(0.3)    inst.Light:SetColour(245/255,255/255,245/255)	inst.Light:Enable(true)endlocal master_postinit = function(inst)	-- choose which sounds this character will play	inst.soundsname = "wilson"	inst:ListenForEvent("ms_respawnedfromghost", stuff)	stuff(inst)		end

Of course, this is in the prefabs file.


Oh and if it was possible, could anyone suggest code solution for being able to emit light -only- at night? That would be neat. I can't seem to nail one.

@Sannom

I can help you with the light on night-only. 

 

Make a function with these lines of code:

local function nightlight(inst)

if TheWorld.state.phase == "day" then
inst.Light:Enable(false)
elseif TheWorld.state.phase == "dusk" then
inst.Light:Enable(false)
elseif TheWorld.state.phase == "night" then
inst.Light:Enable(true)
inst.Light:SetRadius(2.5)
inst.Light:SetFalloff(0.5)
inst.Light:SetIntensity(.3)
inst.Light:SetColour(245/255,255/255,245/255)
end
end

 

And also a periodic task that will check constantly if it's day or night:

local timecheck = 1/5
inst:DoPeriodicTask(timecheck, function() nightlight(inst, timecheck) end)

 

That should do the trick :) It will also solve the issue where the light turns off when you revive, because the function will constantly be ran every 1/5 of a second.

 

Best regards

@Batmanwarrior, having a task run every 1/5 of a second could result in performance issues. There are a lot cleaner ways of handling this in DST.

 

@Sannom, for night only light emition simply watch for the world state for phase change.

 

Example:

local function PhaseChanged(inst, phase)    if phase == "night" then        inst.Light:Enabled(true)        -- DO OTHER LIGHT THINGS HERE.    else        -- WE DON'T CARE ABOUT CHECKING OTHER PHASE TYPES, WE ONLY CARE ABOUT NIGHT.        -- LIGHT WILL BE DISABLED ON ALL OTHER PHASES.        inst.Light:Enabled(false)    endendlocal fn(inst)inst:WatchWorldState("phase", PhaseChanged)return instend 

 

As far as your respawned from ghost function, you should check to see if there is a light already on the character instead of constantly adding additional lights, that is bad practice and could lead to memory leakage.

 

Example:

local function stuff(inst)     if not inst.Light then        inst.entity:AddLight()    end    inst.Light:SetRadius(2.5)    inst.Light:SetFalloff(.5)    inst.Light:SetIntensity(0.3)    inst.Light:SetColour(245/255,255/255,245/255)    inst.Light:Enable(true) end local master_postinit = function(inst)    -- choose which sounds this character will play    inst.soundsname = "wilson"     inst:ListenForEvent("ms_respawnedfromghost", stuff)        -- ADDED THIS LINE TO ENSURE WE HAVE RESPAWNED.    -- ITS PUSHED FROM THE HEALTH COMPONENT.    inst:ListenForEvent("respawn", stuff)    stuff(inst)         end 

 

I'm not saying that this will work after a revive, I'm simply stating that it might reduce further issues in the future.

@Sannom, All characters have light already (it's used when being a ghost). So you don't ever need to add light to a character, you just need to enable it and tweak its brightness/radius/etc.

 

And I would use an approach like Kzisor suggested, but I think using inst:WatchWorldState("startnight", somefunction) and "startday" would be the minimalist way to do it. As long as you also have it run the somefunction when the character spawns in and gets revived, it should be fine.

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
×
  • Create New...