Discord
Login
Community
DARK THEME

Is there a way to be able for a game to read the time and date using microscript?

 I'm making a game about building a factory, and I want to make it so that the factory will "stay running" while you're not playing the game. I'm planning on doing this by detecting the time and date of the last time the player quit the game and comparing it to the time and date of when the player enters the game again. If anyone knows how to do that, or a better way I could do it, please let me know!

Yes, of course man, using the system time and storage.set for your variables He's exactly what you need here is a simple example

init = function()
  // 1. Initial resources
  iron = 0
  production_rate = 5 // 5 iron per second
  
  // 2. Calculate time spent away
  local last_seen = storage.get("last_timestamp")
  
  if last_seen != 0 then
    local current_time = system.time()
    local time_away_ms = current_time - last_seen
    
    // Convert ms to seconds
    local seconds_away = floor(time_away_ms / 1000)
    
    // Add missing production
    local earned = seconds_away * production_rate
    iron += earned
    
    print("Welcome back! You were away for " + seconds_away + " seconds.")
    print("Your factory produced " + earned + " iron while you were gone.")
  else
    print("Welcome! This is your first time playing.")
  end
end

update = function()
  // 3. Normal production while playing (5 iron per second)
  iron += production_rate / 60
  
  // 4. Constant saving to track time even if the tab is closed
  storage.set("last_timestamp", system.time())
end

draw = function()
  screen.clear()
  screen.drawText("IRON: " + floor(iron), 0, 0, 20, "white")
end

I even recommend using system.time() I've already used it in one of my projects.

Thanks, I really appreciate it.

yw

Post a reply

Progress

Status

Preview
Cancel
Post
Validate your e-mail address to participate in the community