Sign in to follow this  
Followers 0
mouseboyx

lua os.clock() for timing recurring events?

Is it safe to use os.clock() in an lua script for timing a recurring event in a script?

If the server runs constantly will os.clock() have an overflow point?  Could it keep counting seconds or milliseconds similar to the unix timestamp?  Obviously this would be a pain to test, I didn't see information on this through a few google searches.

I have this weapons spawning script where every 30 seconds the weapons are spawned again.  It would make more sense to count the ticks and reset a variable rather than depend on os.clock() so it's easy to fix the problem if there is one, I'm just curious about os.clock() 

 

I had this script fail, due to an undetermined cause, although I'm unsure whether any of the other scripts were also still working.  The information I could find about os.clock() says that it's mostly used for script benchmarking purposes.

api_version = "1.12.0.0"
items={
        { "weap", "weapons\\sniper rifle\\sniper rifle", 90.899, -159.633, 1.704, 1.587 ,0},
       }
function OnScriptLoad()

    register_callback(cb['EVENT_TICK'],"OnTick")
     register_callback(cb["EVENT_GAME_START"], "OnGameStart")

end


function OnGameStart()
    if (items) then
        for i = 1,#items,1 do 
            spawn_object(items[i][1], items[i][2], items[i][3], items[i][4], items[i][5], items[i][6])
            items[i][7]=tonumber(os.clock())+30
        end

    end
end

function OnTick()
    time = os.clock()
    for i = 1,#items,1 do 
        if (time>=items[i][7]) then
            spawn_object(items[i][1], items[i][2], items[i][3], items[i][4], items[i][5], items[i][6])
            items[i][7]=tonumber(os.clock())+30
        end
    end
end

 

Share this post


Link to post
Share on other sites

Tiddy-bits:

Internally lua uses C's clock() function:

static int os_clock (lua_State *L) {
  lua_pushnumber(L, ((lua_Number)clock())/(lua_Number)CLOCKS_PER_SEC);
  return 1;
}

(lua_Number is typically double)

Script failure aside, cppreference indicates the function should not be used as a wall clock as you desire, because clock() approximates CPU time. Additionally,

Quote

The value returned by clock() may wrap around on some non-conforming implementations. For example, on such an implementation, if clock_t is a signed 32-bit integer and CLOCKS_PER_SEC is 1000000, it will wrap after about 2147 seconds (about 36 minutes).

As clock_t and CLOCKS_PER_SEC are implementation-defined, it would be difficult to immediately decide if this the issue you're running into without knowing the specific compiler version and flags used.

Takka and mouseboyx like this

Share this post


Link to post
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
Sign in to follow this  
Followers 0
  • Recently Browsing   0 members

    No registered users viewing this page.