LordNiggle Posted September 23, 2019 Share Posted September 23, 2019 (edited) is there a global variable that shows what day it currently is? Edited September 26, 2019 by LordNiggle Link to comment Share on other sites More sharing options...
Ultroman Posted September 23, 2019 Share Posted September 23, 2019 (edited) 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. Edited September 23, 2019 by Ultroman Link to comment Share on other sites More sharing options...
Serpens Posted September 23, 2019 Share Posted September 23, 2019 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 More sharing options...
LordNiggle Posted September 23, 2019 Author Share Posted September 23, 2019 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 More sharing options...
Serpens Posted September 23, 2019 Share Posted September 23, 2019 I'm not sure if you can store netvars within local ... try it without, so only print GLOBAL.TheWorld.components.clock._cycles:value() Link to comment Share on other sites More sharing options...
Ultroman Posted September 23, 2019 Share Posted September 23, 2019 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) Link to comment Share on other sites More sharing options...
LordNiggle Posted September 23, 2019 Author Share Posted September 23, 2019 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 More sharing options...
Ultroman Posted September 23, 2019 Share Posted September 23, 2019 Then do local day = GLOBAL.TheWorld.components.clock._cycles:value() print("Day: "..day) Link to comment Share on other sites More sharing options...
LordNiggle Posted September 23, 2019 Author Share Posted September 23, 2019 9 minutes ago, Ultroman said: Then do local day = GLOBAL.TheWorld.components.clock._cycles:value() print("Day: "..day) same exact error message Link to comment Share on other sites More sharing options...
Ultroman Posted September 24, 2019 Share Posted September 24, 2019 Damn, try this, then. My memory isn't so good between the two games. local day = GLOBAL.GetWorld().components.clock._cycles:value() print("Day: "..day) Link to comment Share on other sites More sharing options...
LordNiggle Posted September 24, 2019 Author Share Posted September 24, 2019 9 hours ago, Ultroman said: Damn, try this, then. My memory isn't so good between the two games. local day = GLOBAL.GetWorld().components.clock._cycles:value() print("Day: "..day) same outcome as last time Link to comment Share on other sites More sharing options...
Ultroman Posted September 24, 2019 Share Posted September 24, 2019 (edited) 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 September 24, 2019 by Ultroman Link to comment Share on other sites More sharing options...
LordNiggle Posted September 24, 2019 Author Share Posted September 24, 2019 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 More sharing options...
Ultroman Posted September 24, 2019 Share Posted September 24, 2019 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... Link to comment Share on other sites More sharing options...
LordNiggle Posted September 24, 2019 Author Share Posted September 24, 2019 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 More sharing options...
Ultroman Posted September 24, 2019 Share Posted September 24, 2019 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 More sharing options...
LordNiggle Posted September 24, 2019 Author Share Posted September 24, 2019 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? Link to comment Share on other sites More sharing options...
Ultroman Posted September 25, 2019 Share Posted September 25, 2019 OK, I'm at the computer now, so I'll figure this out. Perhaps the clocl-component isn't added until after the world has been created. Link to comment Share on other sites More sharing options...
Ultroman Posted September 25, 2019 Share Posted September 25, 2019 (edited) 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 September 25, 2019 by Ultroman Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now