TheEmperorOfLulz wrote:
There's just one problem... How do i install it? I really don't know how to do that:)
Just download this file and put it next to your main.lua file (same dir):
https://raw.github.com/kikito/cron.lua/master/cron.lua
Then you can include it in main by doing this:
And that's it. You are ready to use cron.every, cron.after, etc.
TheEmperorOfLulz wrote:
Also, in the code, you say
Code: Select all
trollClock = cron.every(1, function()
To me, it looks like you let out a parenthesis. Is that an error, or did you do it on purpose?
It's on purpose. Ignoring the troll.health line, the code is like this:
Code: Select all
trollClock = cron.every(1, function()
...
end)
As you can see, the function() part gets "closed" by the "end" on the third line. That
function() ... end structure is called "anonymous function" (because it doesn't have a name). Anonymous functions are very used in Lua, for places where you need a function in one place, but you know you are not going to use it in any other places.
If you have difficulties figuring it out, this code is equivalent to the one on my first post:
Code: Select all
cron = require 'cron'
function increaseTrollEnergy()
troll.energy = troll.energy + 1
end
function love.update(dt)
cron.update(dt) -- you must do this once inside love.update
...
trollClock = cron.every(1, increaseTrollEnergy)
...
cron.cancel(trollClock) -- this stops the clock, for example when the troll dies
end
In this case, I've replaced the anonymous function by a regular function called increaseTrollEnergy. The only difference is that there is a global function called increaseTrollEnergy now - since I only needed to use it in one place, it didn't seem necesary.
Well, this is not entirely true; the code is equivalent only if the troll variable is available in both increaseTrollEnergy and love.update. If it was a local variable created inside love.update, then you would have to pass it as a parameter - like this:
Code: Select all
cron = require 'cron'
function increaseTrollEnergy(troll)
troll.energy = troll.energy + 1
end
function love.update(dt)
cron.update(dt) -- you must do this once inside love.update
...
local troll = something() -- troll is a local variable created inside love.update
...
trollClock = cron.every(1, increaseTrollEnergy, troll) -- we pass it as a parameter
...
cron.cancel(trollClock) -- this stops the clock, for example when the troll dies
end
I don't know if this helps you or makes it more difficult to understand
. Let me know if you have more questions.