[SOLVED] How do i make a timer in lua?
#1
Hi everyone,i learned the cuberite api and lua but i still don't know how to make a timer?

for example i have a timer set to 1 sec to spawn mob every 1 sec and / or create a timer that executes every 5 seconds for 5 times and then it stops the timer.
Reply
Thanks given by:
#2
Use cWorld:ScheduleTask for that.
Something like this:
-- One second is 20 ticks
local callback;
callback = function(a_World)
	-- Spawn a mob
	
	-- Reschedule the callback again.
	a_World:ScheduleTask(20, callback)
end

world:ScheduleTask(20, callback)
Reply
Thanks given by:
#3
Where should i put this? in Initialize?

also how would i get the current world?
Reply
Thanks given by:
#4
You should put it wherever you want the task to be started - if it is by a reaction to a player command, then the player command handler is the right place. If it should be done, say, if a player is killed, then the HOOK_KILLED is the right place.

As for the current world, "current for whom?" Each player has a world assigned to them, accessible via cPlayer:GetWorld(). Similarly, entities have a cEntity:GetWorld().
Reply
Thanks given by:
#5
(10-29-2016, 04:05 AM)xoft Wrote: You should put it wherever you want the task to be started - if it is by a reaction to a player command, then the player command handler is the right place. If it should be done, say, if a player is killed, then the HOOK_KILLED is the right place.

As for the current world, "current for whom?" Each player has a world assigned to them, accessible via cPlayer:GetWorld(). Similarly, entities have a cEntity:GetWorld().

There is no task waiting to be started, i want it to be real time
I will tell you what i need to do, i want to spawn a minecart every second if i use WorldTicks
it will spawn a minecart  20 times a second and i don't want that


As for the current world i want to get a specific player for example player name = "Steve"
it will get his world then spawn a minecart in steve's world every second

also how can i get a class for example i would do
Note: This code is obliviously wrong because function GetClass doesn't exist  
Code:
--Gets the player
player1 = GetClass(player)
if( player1:GetName() == 'Steve' ) then
    --Defines item as redstone
    Redstone = cItem(E_ITEM_REDSTONE)
    Redstoneitem = cItems:Add(Redstone)
    --Get the player's current world
    world = player1:GetWorld()
    --Spawn the item
    world:SpawnItemPickups(Redstoneitem,0,0,0,3000,0)
end
This would solve my problem.
Reply
Thanks given by:
#6
(10-29-2016, 05:42 AM)denwo Wrote: also how can i get a class for example i would do

This would solve my problem.

You mean you want the player object? Often the arguments for a callback will provide those. If not, you can get it from cRoot()

Code:
cRoot:Get():ForEachPlayer(function(cPlayer)
       if (cPlayer:GetName() == "Steve") then
            ....
       end
end)
Reply
Thanks given by:




Users browsing this thread: 1 Guest(s)