Jump to content

(SOLVED) Day Count


Recommended Posts

2 hours ago, LordNiggle said:

is there a global variable that shows what day it currently is?

do you really need the day, or do you need the "days survived" which is eg. used for the hound waves and so on?

Ultroman: this looks like a netvar, so should work for clients (did not test)

Link to comment
Share on other sites

6 hours ago, Ultroman said:

I believe it's


TheWorld.components.clock._cycles:value()

If your code is in modmain, you'll need GLOBAL. in front of that. And I'm not sure this works on clients.

I made a test mod comprised of only the following:

local print = GLOBAL.print
local TheWorld.components.clock._cycles:value() = GLOBAL.TheWorld.components.clock._cycles:value()

local day = TheWorld.components.clock._cycles:value()
print(day)
 

When i load a new world with just this the mod crashes and says ":2: unexpected symbol near '.'". I have no idea which period the crash report is referring to.

Link to comment
Share on other sites

6 hours ago, Ultroman said:

You don't need to store the print function, and I don't know what you were trying to do with the second line. If you want to print the current day, just do this:


local day = TheWorld.components.clock._cycles:value()
print("Day: "..day)

 

Copypasted what you had to see if it worked and I was greeted with "00:08:26]: [string "../mods/Testl/modmain.lua"]:1: attempt to index global 'TheWorld' (a nil value)"

Link to comment
Share on other sites

It says "attempt to index global 'TheWorld' (a nil value)" ?

I just checked and in several of my own mods I use "GLOBAL.TheWorld", so I know that works.

Are you sure you're editing the files in the game's "mods" folder and not files in another folder? I always edit the files in another folder first and then move those edited files into my mod folder under the game's "mods" folder and sometimes I forget to move the files after an edit, which confuses me to think that my edit didn't make a difference.

Edited by Ultroman
Link to comment
Share on other sites

3 hours ago, Ultroman said:

It says "attempt to index global 'TheWorld' (a nil value)" ?

I just checked and in several of my own mods I use "GLOBAL.TheWorld", so I know that works.

Are you sure you're editing the files in the game's "mods" folder and not files in another folder? I always edit the files in another folder first and then move those edited files into my mod folder under the game's "mods" folder and sometimes I forget to move the files after an edit, which confuses me to think that my edit didn't make a difference.

Nope. put it in my mod's folder thats inside the "mods" folder. If u dont believe feel free to try it out yourself. put exactly what you copypasted into a modmain and see if it crashes.

Link to comment
Share on other sites

1 hour ago, Ultroman said:

It's not that I don't believe you. I'm just troubleshooting. I just tried it myself, and get the same problem you're reporting, which is weird since all my mods which do the same thing work fine...

Didnt mean to accuse you of that, shouldve worded what i was saying better, my bad.

Link to comment
Share on other sites

Ah! I know why...I'm an idiot. TheWorld is obviously not available while the mods are being loaded *facepalm*

All my mods use it within Add****PostInit, and they're all called after TheWorld exists. So do this:

AddPrefabPostInit("world", function(inst)
	local day = GLOBAL.TheWorld.components.clock._cycles:value()
	print("Day: "..day)
end)

 

But again, all your code will most likely be inside those kinds of PostInit functions, or within prefab files which are also loaded after the world has been loaded.

Link to comment
Share on other sites

30 minutes ago, Ultroman said:

Ah! I know why...I'm an idiot. TheWorld is obviously not available while the mods are being loaded *facepalm*

All my mods use it within Add****PostInit, and they're all called after TheWorld exists. So do this:


AddPrefabPostInit("world", function(inst)
	local day = GLOBAL.TheWorld.components.clock._cycles:value()
	print("Day: "..day)
end)

 

But again, all your code will most likely be inside those kinds of PostInit functions, or within prefab files which are also loaded after the world has been loaded.

So now clock is the nil variable?

bandicam 2019-09-24 19-58-35-181.png

Link to comment
Share on other sites

OK, I've figured it out, plus a bit more. The following (hidden) code works, but when the postinit for the world is called, it has not yet had its state updated, so for the first immediate print, it says we are on day 0, and for the delayed print it says we are on day 22 (which is correct).

Spoiler

AddPrefabPostInit("world", function(inst)
	if _G.TheWorld.state and _G.TheWorld.state.cycles then
		local day = _G.TheWorld.state.cycles
		print("Day: "..day.." on "..tostring(_G.TheNet and _G.TheNet:GetIsServer() and "server" or "client"))
	end
	
	inst:DoTaskInTime(5, function(inst)
		if _G.TheWorld.state and _G.TheWorld.state.cycles then
			local day = _G.TheWorld.state.cycles
			print("Delayed Day: "..day.." on "..tostring(_G.TheNet and _G.TheNet:GetIsServer() and "server" or "client"))
		end
	end)
end)

 

It works on both the client and the server, so this is the correct line to get the current day:

GLOBAL.TheWorld.state.cycles

 

Edited by Ultroman
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...