• 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!

Solved [TFS 1.2] Creaturescript onThink interval ignored

hellboy

Intermediate OT User
Joined
Apr 6, 2008
Messages
544
Solutions
6
Reaction score
121
Location
player:getTown()
I created script onThink in creature events and registered it inside onLogin

Code:
<event type="think" interval="5000" name="Hunger" script="hunger.lua" />

Code:
function onThink(creature, interval)
   print(interval)
   return true
end

And it print 1000 every second (it should print 5000 every 5 seconds).

It's my scripting failture or I should create bug report in github?
 
Solution
The interval parameter is not the interval you configured (it would be useless anyway), it is the interval from the last execution. You can store it with a storage value and check like this:
Code:
delay = player:getStorageValue(storage)
if delay >= 5000 then
    do something
    player:setStorageValue(storage, interval)
else
    player:setStorageValue(storage, delay + interval)
end
The interval parameter is not the interval you configured (it would be useless anyway), it is the interval from the last execution. You can store it with a storage value and check like this:
Code:
delay = player:getStorageValue(storage)
if delay >= 5000 then
    do something
    player:setStorageValue(storage, interval)
else
    player:setStorageValue(storage, delay + interval)
end
 
Last edited:
Solution
this is what i use for intervals in creature events from https://otland.net/threads/onthink-cid-interval-questions.163060/#post-1828505

Code:
function onThink(cid, interval)
   if cid:getStorageValue(storage) == 0 then cid:setStorageValue(storage, os.clock())    end
     if (os.clock() - cid:getStorageValue(storage)) >= 5 or (os.clock() - cid:getStorageValue(storage)) < 0 then
          --code here
         cid:setStorageValue(storage, os.clock())
         return true
     end
   end
     return false
end
 
yeah you are right and interval is not needed in the parameters

Code:
function onThink(player)
   local interval= 5 --time in seconds
   if player:getStorageValue(storage) == 0 then player:setStorageValue(storage, os.clock())    end
     if (os.clock() - player:getStorageValue(storage)) >= interval or (os.clock() - player:getStorageValue(storage)) < 0 then
          --code here
         player:setStorageValue(storage, os.clock())
         return true
     end
   end
     return false
end
 
os.clock is not reliable for you as it represents the CPU time for the program, not the system time value. This means that if you set a player's storage 3 hours after the server start, after restarting the server the event will only start to trigger roughly 3 hours later, when the CPU time reaches the same value.

Use os.time instead.
 
os.clock is not reliable for you as it represents the CPU time for the program, not the system time value. This means that if you set a player's storage 3 hours after the server start, after restarting the server the event will only start to trigger roughly 3 hours later, when the CPU time reaches the same value.

Use os.time instead.

thats why i had the
Code:
or (os.clock() - player:getStorageValue(storage)) < 0
on the second if but yeah time should be better and we can remove that OR statement.
 
Ok so use siply counter based on storage. It's simple work around and I wonder why I don't think about it before. Thanks all!

[Solved]
You didn't think about it before because your thinking was the correct one, setting an interval for the event to fire :p unfortunately it's not like that (yet)
 
Btw. I would like to make monsters think once every 100 milliseconds.
How can I do that?

I want to bring normal monster attacks to my monster AI too, but AI is based on onthink() function. Sadly it executes only once per second.

Or making the onthink execute every 100milliseconds is too strainy?
Should I make addEvent hits instead?

aka*
Code:
addEvent(dealDelayDMG, delay, combatTable)
Code:
target_in_distance = checkDistance(combatTable)
if target_in_distance then
dealDamage(combatTable)
end
 
Last edited:
In my opinion addEvent is correct ansver in your question. But don't write in this thread (create new :) ).

BTW remember about check in addEvent if monster still exist.
 
Btw. I would like to make monsters think once every 100 milliseconds.
How can I do that?

I want to bring normal monster attacks to my monster AI too, but AI is based on onthink() function. Sadly it executes only once per second.

Or making the onthink execute every 100milliseconds is too strainy?
Should I make addEvent hits instead?

aka*
Code:
addEvent(dealDelayDMG, delay, combatTable)
Code:
target_in_distance = checkDistance(combatTable)
if target_in_distance then
dealDamage(combatTable)
end
You need to modify source code creature.h like ninja said

Code:
 #define EVENT_CREATURE_THINK_INTERVAL 1000

to 100
 
Back
Top