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.

Legitch

How to detect daytime.

Recommended Posts

Legitch    0

Hey, I am new to the whole modding scene in Don't Starve so I have a bit of a predicament.

How do I make it so the game can detects when it's daytime and execute damage onto the character. Making a Vampire character getting hurt in the Sunlight using part of code from WX-78.

I believe this line might be involved with detecting when it rains, then executing the spark damage:

Quote

local function onisraining(inst, israining)
    if israining then
        if inst.spark_task == nil then
            inst.spark_task = inst:DoPeriodicTask(.1, dorainsparks, nil, .1)
...

If you can help me, thanks.

Share this post


Link to post
Share on other sites
neahxx    4

The following should work if you put it into your modded character's, master_postinit function. This detects whether or not it is day each time period, then acts accordingly.

local function damagedbydaylight(inst)
	if TheWorld.state.isday then
		inst.components.health:DoDelta(-1)
	end
end

inst:DoPeriodicTask(10, damagedbydaylight(inst))

But I recommend tweaking the following values to your liking.

inst.components.health:DoDelta(-1)  --The amount of damage

inst:DoPeriodicTask(10, damagedbydaylight(inst))  --The time until being damaged during day (In seconds)

 

I have a quick question though. Do you intend to do this for when it is day or just when the character is in the sunlight. If the later, then it will require putting additional code to check if the character is in the sunlight, rather than just checking if it is day.

Share this post


Link to post
Share on other sites
YakumoYukari    144

There's phasechanged worldstate in worldstate.lua
Worldstate is a component that is attached in TheWorld.
You can get the world's state(isday, israining, issummer, nightmarephase if cave) through this.

Let's check with the example you've found.
function onisraining(inst, israining) is called by the state changed by TheWorld. 
There's a method that let an entity(or component) to track worldstate; WatchWorldState(var, fn).
You can find this method in wx78, function onbecamerobot(inst)
 

inst:WatchWorldState("israining", onisraining)
onisraining(inst, TheWorld.state.israining)

So onisraining will be called every time raining state is changed by the method WatchWorldState("israining", onisraining).
And the second one is to set the initial state(unlike most common events, you have to get the initial state(value) of TheWorld manually).


By using this method, you can do what you want to.

inst:WatchWorldState("phase", onphasechanged)
onisraining(inst, TheWorld.state.phase)

onphasechanged will be called every time the phase(day, dusk, night) is changed.
But be careful that phase is not a boolean but a string "day", "dusk" or "night".

So onphasechanged would be defined like this

local function onphasechanged(inst, phase)
    if phase == "day" then
     --- damaging thing.
....


Note that there's cavephase to track overworld's phase. In the cave otherwise, phase is considered always "night".

Share this post


Link to post
Share on other sites