Jump to content

How to detect daytime.


Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Edited by YakumoYukari
  • Like 2
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...