Jump to content

Countdown to nightfall - checking time of day periodically


Recommended Posts

For my very first DST mod, I would like my character to announce a countdown to nightfall.

Q1) How do I run my code periodically?

I know I can watch for change of phase with inst:WatchWorldState("phase", OnPhaseChanged) but that's not fine grained enough - is there something similar I can watch for change of segment? Or another way to run my code periodically?

Q2) How do I retrieve the time of day / segment?

Thanks

PS I'm a programmer, but if there's a mod that already does this, I am happy to simply use that!

Link to comment
Share on other sites

There are two ways to make tasks "after some time"

You can use the functions "DoTaskInTime" and "DoPeriodicTask"
These functions are associated with instances

Like

inst:DoPeriodicTask(10,function(inst)
  inst.components.talker:Say("Abacate")
end)

If you attach this function to your character prefab, every 10 seconds it will say "Abacate"

inst:DoTaskInTime(10,function(inst)
  inst.components.talker:Say("Abacate")
end)
This one will be just a single time, but after 10 seconds (you can make it periodically using some kind of recursive function)

About the second question, if you run

for k,v in pairs(TheWorld.state) do print(k,v) end

You will see several properties you can get from the WorldState

You can do something like store the last state, and if the state change to night, you say something
 

inst:DoPeriodicTask(1,function(inst)
  inst.was_night ~= TheWorld.state.isnight then
     if TheWorld.state.isnight   then -- To avoid announcing when leave the night

         inst.components.talker:Say("Here we go again")

     end
  end
  inst.was_night=TheWorld.state.isnight

end)

 

Edited by Gleenus
Link to comment
Share on other sites

Thanks, the code to print a list of property pairs was very useful.

Unfortunately I didn't really understand "attach this function to your character prefab"

In any case I'm not making a new character, I actually want to write a mod to affect all players on my server, so I tried this:

AddPlayerPostInit(
function(inst)
    inst:DoPeriodicTask(10,function(inst)
        inst.components.talker:Say("Abacate")
end)

But it didn't do anything, not even an error message :(

Apologies if I'm making obvious mistakes! I'm coming from C/C++ so LUA itself is different, but mostly the problem I'm having is that I can't find an explanation of all the DST functions and global vars like AddPlayerPostInit, inst:DoPeriodicTask, GLOBAL.TheWorld.ismastersim etc and how things hang together - could you point me at any resources please?

Link to comment
Share on other sites

14 hours ago, TobyKenobi said:

Unfortunately I didn't really understand "attach this function to your character prefab"


AddPlayerPostInit(
function(inst)
    inst:DoPeriodicTask(10,function(inst)
        inst.components.talker:Say("Abacate")
end)

 

This is exactly what "attaching to your character prefab means"

You just missing some points, but you are doing the right thing

AddPlayerPostInit("PREFAB NAME", function(inst)
    inst:DoPeriodicTask(10,function(inst)
		inst.components.talker:Say("Abacate")
	end)
end)


And about you coming from C++, well, I'm a Fortran 90 programmer :D, I understand you
 

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