I am just wondering is this true?
Ohh thxs Orrr2 guys for your Source Code, although it's hard to understand (for me that is) it's a great help
The other levels are updated slower so the timers also advance slower there. To avoid unnecessary load on CPU, during each frame only three levels are updated and the logic goes like this:Mysterious wrote:Hi guys. Sorry for answering so late Computer went down.
Ok thank you for that info, will keep this in mind. I have noticed things are different with Timers. I don't know if I am correct here but they are time wise different on each level. It could just be me but not sure. It feels as though they get slower each level. Aren't they supposed to be the same, I mean a timer is a timer eg: 1 to 60 Seconds so they should be all the same? I think that's what you mentioned Mysblade.
Mysblade thxs for the additional Village objects that go with the Setand for your answer Antti
Code: Select all
gGlobalTimers = {};
gTimerToGlobalMap = {};
--
-- Create a global timer that will tick at the same rate regardless of the level the player is on.
-- Note: The timer is running by default but can be stopped (paused) or destroyed later.
--
function createTimer(id, interval, tickFunc)
if ( gGlobalTimers[id] ~= nil ) then
print("A timer with ID <" .. id .. "> already exists, cannot create another.");
end
local levelCount = getMaxLevels();
local globalTimerInfo = {
timerId = id,
timers = {},
lastTick = getStatistic("play_time"),
isRunning = true,
tickFuncs = {},
destroyFuncs = {},
destroyDelay = 0,
};
if ( type(tickFunc) == "function" ) then
table.insert(globalTimerInfo.tickFuncs, tickFunc);
elseif ( type(tickFunc) == "table" ) then
globalTimerInfo.tickFuncs = tickFunc;
end
for lvl = 1, levelCount do
local timer_id = "gt_" .. id .. "_" .. lvl;
local timer = spawn("timer", lvl, 1, 1, 1, timer_id);
timer:setTimerInterval(interval);
timer:addConnector("activate", "global_timers", "tick");
timer:activate();
table.insert(globalTimerInfo["timers"], timer_id);
gTimerToGlobalMap[timer_id] = id;
end
gGlobalTimers[id] = globalTimerInfo;
end
--
-- Add a tick function to a global timer
--
function addTimerTickFunc(id, tickFunc)
if ( gGlobalTimers[id] == nil ) then
print("Cannot add tick func to global timer " .. id .. " without creating it first");
return false;
end
if ( type(tickFunc) ~= "function" ) then
print("Tried to add tick func to timer, but given " .. typeof(tickFunc) .. " instead.");
return false;
end
table.insert(gGlobalTimers[id].tickFuncs, tickFunc);
return true;
end
--
-- Add a destoy callback function to a global timer
--
function addTimerDestroyFunc(id, destroyFunc)
if ( gGlobalTimers[id] == nil ) then
print("Cannot add destroy func to global timer " .. id .. " without creating it first");
return false;
end
if ( type(destroyFunc) ~= "function" ) then
print("Tried to add tick func to timer, but given " .. typeof(destroyFunc) .. " instead.");
return false;
end
table.insert(gGlobalTimers[id].destroyFuncs, destroyFunc);
return true;
end
--
-- Start a timer that has previously be paused.
--
function startTimer(id)
if ( gGlobalTimers[id] == nil ) then
print("Cannot start global timer " .. id .. " without creating it first");
return false;
end
gGlobalTimers[id]["isRunning"] = true;
return true;
end
--
-- Stop a running global timer but don't destroy it (so it can be resumed)
--
function stopTimer(id)
if ( gGlobalTimers[id] == nil ) then
print("Cannot stop global timer " .. id .. " without creating it first");
return false;
end
gGlobalTimers[id]["isRunning"] = false;
return true;
end
--
-- Destroy the given global timer, removing it completely from the world
--
function destroyTimer(id, delay)
if ( gGlobalTimers[id] == nil ) then
print("Cannot destroy global timer " .. id .. " without creating it first");
return false;
end
if ( delay ~= nil and delay > 0 ) then
gGlobalTimers[id].destroyDelay = delay;
return false;
end
for i,timer_id in ipairs(gGlobalTimers[id]["timers"]) do
local timer = findEntity(timer_id);
gTimerToGlobalMap[timer_id] = nil;
if ( timer ~= nil ) then
timer:destroy();
end
end
gGlobalTimers[id] = nil;
for i,func in ipairs(globalTimer.destroyFuncs) do
func(dt);
end
end
--
-- Internal 'tick' method for the timers
--
function tick(sender)
if ( sender.level ~= party.level ) then
return;
end
globalTimer = gGlobalTimers[gTimerToGlobalMap[sender.id]];
if ( globalTimer == nil ) then
return;
end
local time = getStatistic("play_time");
local dt = time - globalTimer.lastTick;
globalTimer.lastTick = time;
for i,func in ipairs(globalTimer.tickFuncs) do
func(dt);
end
if ( globalTimer.destroyDelay > 0 ) then
globalTimer.destroyDelay = globalTimer.destroyDelay - dt;
if ( globalTimer.destroyDelay <= 0 ) then
destroyTimer(globalTimer.timerId);
end
end
end
Code: Select all
-- Sample script for using Global Timers
-- Note: This should really be called from a function instead of just being in a script
-- entity, otherwise it will only work if the global_timers script is loaded first.
function tickA(dt)
print("Timer 1 Tick A: " .. dt);
end
function tickB(dt)
print("Timer 1 Tick B: " .. dt);
end
if ( findEntity("global_timers") ~= nil ) then
-- Start timer
global_timers.createTimer("my_timer_id", 0.10);
global_timers.addTimerTickFunc("my_timer_id", tickA);
global_timers.addTimerTickFunc("my_timer_id", tickB);
global_timers.addTimerDestroyFunc("my_timer_id", function() print("Timer Destroyed!"); end);
-- Destroy in 5 seconds
global_timers.destroyTimer("my_timer_id", 5);
else
print("Global Timers not installed");
end
Code: Select all
local t = timers:create('my_constant_timer')
t:setConstant(true) --this makes the timer constant
t:addConnector("activate", "script", "tick");
t:activate()