Jump to content

How do I get the current world and player day?


Recommended Posts

I'm trying to get the world's current day, and the total days that a player has survived. I've been digging in multiple files such as world.lua, clock.lua, etc, but I can't find what I'm looking for.

I would want to get a fresh value each time I call that function, so I can always have access to the updated survived days.

Is there a built-in function that can do this?

A bit of guidance is appreciated.

Link to comment
Share on other sites

you can use "WatchWorldState", this is a listener, like ListenForEvent. So you function is called everytime the requested event is happening, like "isday" or general all "cycles" of the day. and maybe more.
And then there is "TheWorld.state" where you can ask for TheWorld.state.isday or TheWorld.state.cycles.  Maybe the last is the current day, but I'm ot sure. Just search the game files for these 2 and you may find what you need.
For the days a player survived you simply look into "hounded.lua", this is spawning hounds every few days and depending on the days survived from all players, it will be more or less hounds. Eg you will find this function within:

local function GetAveragePlayerAgeInDays()
    local sum = 0
    for i, v in ipairs(_activeplayers) do
        sum = sum + v.components.age:GetAgeInDays()
    end
    return sum > 0 and sum / #_activeplayers or 0
end

You can copy it into your mod and replace _activeplayers with AllPlayers (in modmain put GLOBAL. in front of it)

Link to comment
Share on other sites

Thanks, the player.components.age:GetAgeInDays() correctly returned the player's age.

About the world's age, it's somewhat confusing. I did what you asked, and found this in hunter.lua:

local day = TheWorld.state.cycles
local num_bats = math.min(3 + math.floor(day/35), 6)

That would suggest that the 'day' variable is an integer, and can be compared to other numbers. However, when I call it in my modmain.lua, it returns a function. And when I use `tostring()` on the returned value, I get something like `function: 29D8790`. God knows what's happening? :D

Link to comment
Share on other sites

31 minutes ago, JackJohnsn said:

Thanks, the player.components.age:GetAgeInDays() correctly returned the player's age.

About the world's age, it's somewhat confusing. I did what you asked, and found this in hunter.lua:


local day = TheWorld.state.cycles
local num_bats = math.min(3 + math.floor(day/35), 6)

That would suggest that the 'day' variable is an integer, and can be compared to other numbers. However, when I call it in my modmain.lua, it returns a function. And when I use `tostring()` on the returned value, I get something like `function: 29D8790`. God knows what's happening? :D

directly within modmain it is not finally initializted. It should work within functions that are called during the game, eg in your ondeploy and so on. you can also use console to print(TheWorld.state.cycles) and see that it prints a number ingame

Edited by Serpens
Link to comment
Share on other sites

1 minute ago, JackJohnsn said:

Thanks for the note, although I used it inside the STORE action ( GLOBAL.ACTIONS.STORE.fn ) and the above happened. Also I assume that the correct way of calling it would be GLOBAL.TheWorld.state.cycles. I'll investigate more.

hm.. strange.  GLOBAL is only needed within modmain. And if you dont have it, but it is needed, you will get an error like "trying to call global xy"

Link to comment
Share on other sites

Well, I figured it out in a weird way. If I do this:
 

local TheWorld   = GLOBAL.TheWorld

GLOBAL.ACTIONS.BUILD.fn = function(act)
	local day = TheWorld.state.cycles -- This is nil, and throws fatal error
end

It won't work. But this works:

GLOBAL.ACTIONS.BUILD.fn = function(act)
	local day = GLOBAL.TheWorld.state.cycles -- This return the proper value
end

Probably some kind of lua syntax I'm not familiar with, but kinda weird since everything else works in the former method, except this.

Link to comment
Share on other sites

1 minute ago, JackJohnsn said:

Well, I figured it out in a weird way. If I do this:
 


local TheWorld   = GLOBAL.TheWorld

GLOBAL.ACTIONS.BUILD.fn = function(act)
	local day = TheWorld.state.cycles -- This is nil, and throws fatal error
end

It won't work. But this works:


GLOBAL.ACTIONS.BUILD.fn = function(act)
	local day = GLOBAL.TheWorld.state.cycles -- This return the proper value
end

Probably some kind of lua syntax I'm not familiar with, but kinda weird since everything else works in the former method, except this.

guess is this again because TheWorld does not exist yet when your file is loaded.

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