Cuberite Forum
Class cWorld; Function ScheduleTask; Only one Function Callback - Printable Version

+- Cuberite Forum (https://forum.cuberite.org)
+-- Forum: Plugins (https://forum.cuberite.org/forum-1.html)
+--- Forum: Plugin Discussion (https://forum.cuberite.org/forum-8.html)
+--- Thread: Class cWorld; Function ScheduleTask; Only one Function Callback (/thread-1691.html)

Pages: 1 2


Class cWorld; Function ScheduleTask; Only one Function Callback - VeryBlackMan - 12-23-2014

Hallo,

I want a ticker that calls a function every 5 minutes.
This is my Code.
Code:
function Initialize(Plugin)
  Plugin:SetName("MyPlugin")
  Plugin:SetVersion(1)
  local World = cRoot:Get():GetDefaultWorld()
  World:ScheduleTask(10, TaskFunction)
  return true
end

function TaskFunction(World)
  LOG(World:GetName())
end

But the Function TaskFunction is called only once.
Why this?


RE: Class cWorld; Function ScheduleTask; Only one Function Callback - Seadragon91 - 12-23-2014

Code:
function Initialize(Plugin)
    Plugin:SetName("MyPlugin")
    Plugin:SetVersion(1)
    local World = cRoot:Get():GetDefaultWorld()
    Callback = function(World)
        LOG(World:GetName())
        World:ScheduleTask(6000, Callback)
    end
    World:ScheduleTask(10, Callback)
    return true
end

This should work, had the same problem Wink

Edit: Sorry misunderstood your question.
You can not directly, add a function as parameter.


RE: Class cWorld; Function ScheduleTask; Only one Function Callback - NiLSPACE - 12-23-2014

The cWorld:ScheduleTask only schedules a function once. You have to reschedule it every time.


RE: Class cWorld; Function ScheduleTask; Only one Function Callback - VeryBlackMan - 12-23-2014

(12-23-2014, 07:06 PM)STR_Warrior Wrote: The cWorld:ScheduleTask only schedules a function once. You have to reschedule it every time.
Well, i've only the Hook Option with HOOK_WORLD_TICK?
http://mc-server.xoft.cz/LuaAPI/OnWorldTick.html

Here my Example Code:
Code:
function Initialize(Plugin)
  Plugin:SetName("MyPlugin")
  Plugin:SetVersion(1)
  cPluginManager.AddHook(cPluginManager.HOOK_TICK, OnTick)
  return true
end

function TaskFunction(World)
  LOG(World:GetName())
end

function OnTick(Time)
  local World = cRoot:Get():GetDefaultWorld()
  World:ScheduleTask(10,
    function()
      LOG("Execute")
    end
  )
end



RE: Class cWorld; Function ScheduleTask; Only one Function Callback - VeryBlackMan - 12-23-2014

Or a Call the Function recursively.
Is that the best Option, realy?
Code:
function Initialize(Plugin)
  Plugin:SetName("MyPlugin")
  Plugin:SetVersion(1)
  local World = cRoot:Get():GetDefaultWorld()
  TaskFunction(World)
  return true
end

function TaskFunction(World)
  World:ScheduleTask(100, TaskFunction)
  LOG(World:GetName())
end



RE: Class cWorld; Function ScheduleTask; Only one Function Callback - NiLSPACE - 12-23-2014

You can use the example by Seadragon91
function Initialize(Plugin)
    Plugin:SetName("MyPlugin")
    Plugin:SetVersion(1)
    local World = cRoot:Get():GetDefaultWorld()
    Callback = function(World)
        LOG(World:GetName())
        World:ScheduleTask(6000, Callback)
    end
    World:ScheduleTask(10, Callback)
    return true
end

You can reschedule it in the function itself.


RE: Class cWorld; Function ScheduleTask; Only one Function Callback - Seadragon91 - 12-23-2014

Small problem here. Using a /reload will crash the server, if a scheduler task is started.


RE: Class cWorld; Function ScheduleTask; Only one Function Callback - VeryBlackMan - 12-23-2014

(12-23-2014, 07:54 PM)Seadragon91 Wrote: Small problem here. Using a /reload will crash the server, if a scheduler task is started.

Right, the same problem I also have.


RE: Class cWorld; Function ScheduleTask; Only one Function Callback - VeryBlackMan - 12-23-2014

The Problem here is, if the plugin is reloaded, is still a Schedule Task open. I can't close the running Task before the Plugin is reloaded.

How can i solve this Problem?


RE: Class cWorld; Function ScheduleTask; Only one Function Callback - worktycho - 12-24-2014

It's a known bug in the server and requires changes to the lua/scheduler interface. The relevant issue is: https://github.com/mc-server/MCServer/issues/1556