Adding a timer to record total real time played to existing code

Started by Velorax, Jun 26, 2022, 07:14 AM

Previous topic - Next topic

Velorax

Hi.

So I'm looking to start a long series on a new world (potentially lasting over a year), but I wanted to use CC: Tweaked to keep track of the total time played on the world over many sessions. I have found code that will allow me to display the number of Minecraft days/years that have elapsed, but I'm having trouble finding code to work with this to record elapsed real time played on the world (this of course will run on startup, and continue the count  after restarting the game)

I am not familiar with lua, but have tried a few things by looking up commands and using my limited HTML knowledge to implement for and if loops but still no success. I will include the code I'm using for the game days passed.

Hope you can help.

local time = os.time()
local day = os.day()
local year = day/365
local dayfinal = day % 365

monitor01 = peripheral.wrap("back")
monitor01.setTextScale(3.4)

while true do
 monitor01.clear()
 monitor01.setCursorPos(8,1)
 time = os.time
 day = os.day
 monitor01.write(textutils.formatTime(time))
 monitor01.setCursorPos(8,2)
 monitor01.write("Year:")
 monitor01.write(tostring(math.floor(year)))
 monitor01.write(" Day:")
 monitor01.write(tostring(dayfinal))
 sleep (1)
end

Lupus590

If you only want to know the age of the world then you can do that with MC's commands, /time query gametime should work.
https://minecraft.fandom.com/wiki/Commands/time

If you want it on a monitor that you can look at, you have a few issues in your code. Before I explain these issues and walk you through fixing them, I want you to ignore everything you know about HTML. HTML is a markup language, and Lua is a programming language; markup and programming languages are not similar enough to be helpful in teaching each other. Lua and Javascript are more similar as they are both programming languages, if you know JS then that knowledge can be somewhat useful. I'm going to assume that you are not familiar with any programming language.

Right, explaining what's going wrong and how to fix it. If you just want working code then it's the last code block.

So a thing with programming languages, particularly procedural ones like Lua, they run the lines of code in order unless something like a while loop tells them otherwise. Math is done when it's encountered and not every time, this is not like an Exel spreadsheet when the value updates automatically. What does this mean? It means that your `year` and `dayfinal` variables need to be set on each loop, and thus the math needs to be inside the loop.

Next, I'm going to go through functions, you've done most of them well but you missed the brackets/parentheses on two lines which is also causing issues. You have to use the brackets to have the function do stuff, otherwise, Lua thinks you are doing something else with the function (which I'm not going to cover as it's a bit complex and you don't need to know right now). To put simply, you need to change `time = os.time` and `day = os.day` to `time = os.time()` and `day = os.day()`.

At this point, I believe that the code will work, but it can be cleaned up a bit. Lua doesn't care that there's a space between `sleep` and its brackets, but it does make the code look a little messy. When you wrap the monitor as `monitor01` you can make that a local variable. To reduce flickering on the monitor, you may want to do all your calculations before drawing to the screen. Finally, I'm not sure if you need the tostring and math.floor calls, but having them in probably is doing no harm.

With all the above changes your code might look like this.

local monitor01 = peripheral.wrap("back")
monitor01.setTextScale(3.4)

while true do
 local time = os.time()
 local day = os.day()
 local year = day/365
 local dayfinal = day % 365
 monitor01.clear()
 monitor01.setCursorPos(8,1)
 monitor01.write(textutils.formatTime(time))
 monitor01.setCursorPos(8,2)
 monitor01.write("Year:")
 monitor01.write(tostring(math.floor(year)))
 monitor01.write(" Day:")
 monitor01.write(tostring(dayfinal))
 sleep(1)
end

Velorax

Thank you for the fix and the explanation. Works a treat. It has been a little while since I last played Minecraft and didn't know about the /time query gametime command, will definitely be using that.

I will be teaching my self JS eventually and will probably move to Lua after. So will keep everything in your post in mind.

Thanks again.