• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Lua unpause and pause os.time() ?

darkmu

Well-Known Member
Joined
Aug 26, 2007
Messages
274
Solutions
1
Reaction score
50
Location
Paraná,Brazil
I was always curious to know if there is any native function in LUA or if we need to come up with a logic so that I can pause os.time upon logout and return os.time upon login. I was thinking of creating a normal storage where it will be stored the value of os.time () and an os.time (), there whenever I log in and log out I write to them? Would that be the logic or has something better to do? Could you give me ideas and examples?

TFS 1.3
 
darkmu

You need to explain what you want to do, not how you want to do "something".

Lua os.time() gets the time from the operating system. You can mess with that with OS commands if you like, but it's extremely unlikely to be a good way to do anything in OT.

If you want to, you can track time in-game continuously, across game stops/start intervals.

Read this for some ideas and links:

A general comment: timestamps and intervals are different kinds of data:
  • Don't mix them up (it's easier than it sounds to make errors that way :)
  • Look for Time-related operations to get a current timestamp (probably os.time()), print a conveniently formatted timestamp, find the difference between timestamps (see the stackoverflow link), and to add an interval to a timestamp to create a new timestamp
Also if you want to make a parallel "game-time", you'll need to consider what can happen if the game (or the server) crashes.
When planning something like this, always assume there will (sooner or later) be a catastrophic power failure (mains power & "uninterruptible" power supplies all fail).
In particular, don't assume an OT API (e.g. I assume there's an onGameShutdown() or similar?) will always be called to save a timestamp on exit - if the game were to die abruptly for any reason, everything based on the accuracy of your internal game-time would misbehave.
 
Last edited:
1609724741982.png

The way I'm doing it is possible to pause time ??? In case I don't know why it is returning me negative ..

Lua:
function onSay(player, words, param)
    local split = param:split(",")
    
    if split[1] == nil then
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "[MasterTreino]: Os comandos disponiveis sao:\n !master tempo\n !master on \n!master off (NAO UTILIZAR O COMANDO ON E OFF POR ENQUANTO)")
        return
    end
    
    if split[1] == 'tempo' then
    
        if player:getStorageValue(15732) >= os.time() then
            player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Tempo Restante:  " .. showTimeLeft(player:getStorageValue(15732) - os.time(), true) .. ".")
            return
        else
            player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Nao existe nenhum master treino ativo, compre seu master treino no NPC GM na cidade VIP ou na STORE.")
            return
        end
    elseif  split[1] == 'on' then
        if player:getStorageValue(15742) > 0 then
            local tempoSobra = player:getStorageValue(15742)
            player:setStorageValue(15732, os.time() + tempoSobra)
            player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "O seu master treino foi ativado.")
            return
        else
            player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Nao existe nenhum master treino ativo, compre seu master treino no NPC GM na cidade VIP ou na STORE.")
            return
        end
    elseif split[1] == 'off' then
        if player:getStorageValue(15732) >= os.time() then
            player:setStorageValue(15742, player:getStorageValue(15732))
            
            player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "O seu master treino foi pausado.")
            player:setStorageValue(15732, 1)
            return
        else
            player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Nao existe nenhum master treino ativo, compre seu master treino no NPC GM na cidade VIP ou na STORE.")
            return
        end
    end
    
    --15742
    
    return true   
end
 
You cannot pause time but it is possible to achieve what you trying to. Can you post the NPC GM or the lines where you set the time in the storage?

Lua:
local config = {
    [40018] = {time = 8},
    [40019] = {time = 12},
    [40020] = {time = 24}
}

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local treino = config[item.itemid]
    
    if not treino then
        return false
    end
    
    if player:getStorageValue(15732) >= os.time() then
        player:sendCancelMessage("Ja existe um master treino em andamento, voce precisa esperar o tempo. Tempo Restante:  " .. showTimeLeft(player:getStorageValue(15732) - os.time(), true) .. ".")
        return true
    end
    
    player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "O seu treino terminara em ".. os.date("%d %B %Y %X", os.time() + treino.time * 3600) ..".")
    player:getPosition():sendMagicEffect(CONST_ME_TELEPORT)
    player:setStorageValue(15732, os.time() + treino.time * 3600)
    Item(item.uid):remove(1)   
    
    
    return true
end

GM NPC has nothing special .. he is an npc that sells only items.
 
View attachment 53280

The way I'm doing it is possible to pause time ??? In case I don't know why it is returning me negative ..

Lua:
function onSay(player, words, param)
    local split = param:split(",")
    
    if split[1] == nil then
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "[MasterTreino]: Os comandos disponiveis sao:\n !master tempo\n !master on \n!master off (NAO UTILIZAR O COMANDO ON E OFF POR ENQUANTO)")
        return
    end
    
    if split[1] == 'tempo' then
    
        if player:getStorageValue(15732) >= os.time() then
            player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Tempo Restante:  " .. showTimeLeft(player:getStorageValue(15732) - os.time(), true) .. ".")
            return
        else
            player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Nao existe nenhum master treino ativo, compre seu master treino no NPC GM na cidade VIP ou na STORE.")
            return
        end
    elseif  split[1] == 'on' then
        if player:getStorageValue(15742) > 0 then
            local tempoSobra = player:getStorageValue(15742)
            player:setStorageValue(15732, os.time() + tempoSobra)
            player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "O seu master treino foi ativado.")
            return
        else
            player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Nao existe nenhum master treino ativo, compre seu master treino no NPC GM na cidade VIP ou na STORE.")
            return
        end
    elseif split[1] == 'off' then
        if player:getStorageValue(15732) >= os.time() then
            player:setStorageValue(15742, player:getStorageValue(15732))
            
            player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "O seu master treino foi pausado.")
            player:setStorageValue(15732, 1)
            return
        else
            player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Nao existe nenhum master treino ativo, compre seu master treino no NPC GM na cidade VIP ou na STORE.")
            return
        end
    end
    
    --15742
    
    return true   
end

As Akela explained..

You need to explain what you want to do, not how you want to do "something".

--
We have no idea which values are

Tempo 2
Tempo normal

Because you didn't leave the prints in your script.
---

Learning how to be helped is just as important as asking questions you want to be helped on.

If you don't give us correct and useful information that describes the problem you're attempting to solve, it's extremely difficult to even begin to help you.

"stopping time" is a potential solution, but may not be the best solution to the problem.
Without knowing the actual problem you are trying to solve, we don't know what questions or answers to provide.

--
For instance..

Someone wants to turn water into a gem.

They don't understand the correlation between the two entities, but they try a few things and ask a public forum
"How do I stabilize the water using electric current?"

While someone could answer this question, it doesn't solve the initial problem of turning a water into a gem.

--
This is the exact scenario you're providing us in your post here.

Explain what you want to do, not how you want to do "something".
 
I was always curious to know if there is any native function in LUA or if we need to come up with a logic so that I can pause os.time upon logout and return os.time upon login. I was thinking of creating a normal storage where it will be stored the value of os.time () and an os.time (), there whenever I log in and log out I write to them? Would that be the logic or has something better to do? Could you give me ideas and examples?

TFS 1.3
If for some reason you manage to stop the os.time () function, which I doubt very much, it would not make sense since you would be affecting all players, this function is global and not local to each player.

I guess you want to start a counter on each player, saving this value to a storage, then you can do the following:

creaturescripts.xml
XML:
<event type="think" name="counterxd" script="counter.lua" />

counter.lua
Lua:
function onThink(player, interval)
    local storageTime = player:getStorageValue(666)
    if storageTime == -1 then
        player:setStorageValue(666, os.time())
    else
        player:setStorageValue(666, storageTime + (interval/1000))
    end
    return true
end

login.lua
Lua:
player:registerEvent("counterxd")

in this way each player will have a counter that is synchronized with os.time () if the storage is empty, since then the seconds will start to run for each player separately, this emulates exactly what you want to do, if not this what you want to do then i have no idea what you say

This does not mean that it is the best way to approach your problem, in fact this way of doing it is quite gross and ugly, unnecessary, there are many better ways to count the time without updating a local relog for each player
 
Last edited:
The way I read your code, 15732 is a timestamp, and 15742 is an interval.

Lua:
elseif split[1] == 'off' then
        if player:getStorageValue(15732) >= os.time() then
            player:setStorageValue(15742, player:getStorageValue(15732))
         
            player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "O seu master treino foi pausado.")
            player:setStorageValue(15732, 1)
            return

If that's right, haven't you set the interval to the timestamp here? And set the timestamp to "1"?
Assuming your environment has the timestamps returned from os.time() in "time units from Jan1, 1980" or a variant of that (i.e. an integer value) the code will run ok, but the value in both the interval field and the timestamp will be way off.

The next round of arithmetic and/or comparisons will return weird results :)
 
Last edited:
As Akela explained..

You need to explain what you want to do, not how you want to do "something".

--
We have no idea which values are

Tempo 2
Tempo normal

Because you didn't leave the prints in your script.
---

Learning how to be helped is just as important as asking questions you want to be helped on.

If you don't give us correct and useful information that describes the problem you're attempting to solve, it's extremely difficult to even begin to help you.

"stopping time" is a potential solution, but may not be the best solution to the problem.
Without knowing the actual problem you are trying to solve, we don't know what questions or answers to provide.

--
For instance..

Someone wants to turn water into a gem.

They don't understand the correlation between the two entities, but they try a few things and ask a public forum
"How do I stabilize the water using electric current?"

While someone could answer this question, it doesn't solve the initial problem of turning a water into a gem.

--
This is the exact scenario you're providing us in your post here.

Explain what you want to do, not how you want to do "something".

What I am wanting to do is a way to pause the os.time () count using the talkaction command as described in the code.

When he uses the command "!Master off", I would have to keep the value of os.time () in a variable and as soon as he uses the other command "! Master on" he would take this value and transform os.time () again. I would be pausing time only when he is training.
 
your idea must be very good and exclusive to not want to explain the mechanics behind what you want

Stopping time will not help you if you are going to deal with offline players and others online, you want to light a tobacco with two stones instead of borrowing a lighter
 
When he uses the command "!Master off", I would have to keep the value of os.time () in a variable and as soon as he uses the other command "! Master on" he would take this value and transform os.time () again. I would be pausing time only when he is training.
What you're looking for is called a stopwatch.
It's implemented by comparing two time values, not by stopping time:
Code:
local t1 = os.time()
-- do stuff
local t2 = os.time()
local diff = t2-t1
 
@darkmu
What he wants is something similar to the time of imbuement system, It is only consumed when it is in use and not over time, so now we can help you right?
So in this case you shouldn't play with os.time () you don't need that function at all, just set the number of seconds directly in the storage and with a repetition event you will decrease those seconds until they are over, If the player is offline then these seconds will not decrease, if you activate the command to pause the time they will not decrease either
 
So os.time won’t work here unless the logic is like

onUse - set storage ostime + time value(60 minutes for example)

On Logout
If storage >= os.time then
Set storage = storage - ostime (to set remaining storage)

On Login
If remaining storage >0 but <= ostime then
Set storage time remaining time + ostime

Script
If storage >= ostime then
Exp = exp x 3

so for example
Use scroll to give 60 mins exp boost
Set storage to ostime + 60 mins

script detects storage value on kill
Storage value >= ostime so
Exp = exp x3

when player logs out, let’s calculate remaining time
(Storage value was set to os time + 60, but now on logout storage value will be ostime + 32 mins for example)
Os time + 32 min - ostime = 32 min storage

when player logs in, let’s recalculate storage
if storage value > 0 <= ostime then
Time remaining = Storage value + os time

I hope that helps
 
Back
Top