• Stars
    star
    123
  • Rank 290,145 (Top 6 %)
  • Language
    Lua
  • License
    MIT License
  • Created almost 10 years ago
  • Updated over 9 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Lua module for delaying function calls

tick

A small Lua module that simplifies the calling of functions at a set interval or after a delay.

Usage

The tick.lua file should be dropped into an existing project and required by it.

tick = require "tick"

At the start of each frame tick.update() should be called and given the delta time since the last call as its argument.

tick.update(dt)

tick.delay(fn, delay)

Calls the function fn after the given delay time has passed. Returns the associated event.

-- Prints "Hello world!" after 2 seconds
tick.delay(function() print("Hello world!") end, 2)

tick.recur(fn, delay)

Calls the function fn at an interval of delay. Returns the associated event.

-- Prints "tick!" every half-second
tick.recur(function() print("tick!") end, .5)

Chaining events

To avoid having to deeply nest several functions when creating chains of events, the :after() method can be called on an event returned by tick.delay(). You can keep using the :after() method to create complex timed sequences.

-- Prints "cat", "dog", then "owl", waiting 1 second between each print
tick.delay(function() print("cat") end, 1)
  :after(function() print("dog") end, 1)
  :after(function() print("owl") end, 1)

Stopping events

An event can be stopped and removed at any point by calling its :stop() method. To do this the event must be assigned to a variable when it is created.

-- Create a new event
local t = tick.delay(function() print("tick!") end, 10)
-- Remove the event before it has a chance to run
t:stop()

Groups

Tick provides the ability to create event groups; these are objects which can have events added to them, and which are in charge of updating and handling their contained events. A group is created by calling the tick.group() function.

local group = tick.group()

Once a group is created it acts independently of the tick object, and must be updated each frame using its own update method.

group:update(dt)

To add a events to a group, the group's :delay() or :recur() methods should be used.

group:delay(function() print("hello world") end, 4)

A good example of where groups are useful is for games where you may have a set of events which effect objects in the game world and which you want to pause when the game is paused. A group's events can be paused by simply neglecting to call its update() method; when a group is destroyed its events are also destroyed.

License

This library is free software; you can redistribute it and/or modify it under the terms of the MIT license. See LICENSE for details.

More Repositories

1

lite

A lightweight text editor written in Lua
Lua
7,287
star
2

microui

A tiny immediate-mode UI library
C
3,124
star
3

log.c

A simple logging library implemented in C99
C
2,472
star
4

json.lua

A lightweight JSON library for Lua
Lua
1,734
star
5

dyad

Asynchronous networking for C
C
1,437
star
6

fe

A tiny, embeddable language implemented in ANSI C
C
1,277
star
7

lume

Lua functions geared towards gamedev
Lua
945
star
8

classic

Tiny class module for Lua
Lua
799
star
9

lovedos

A framework for making 2D DOS games in Lua
C
666
star
10

map

A type-safe hash map implementation for C
C
558
star
11

vec

A type-safe dynamic array implementation for C
C
436
star
12

microtar

A lightweight tar library written in ANSI C
C
423
star
13

flux

A fast, lightweight tweening library for Lua
Lua
366
star
14

lite-plugins

Plugins for the lite text editor
Lua
346
star
15

ini

A tiny ANSI C library for loading .ini config files
C
315
star
16

log.lua

A tiny logging module for Lua
Lua
296
star
17

lovebird

A browser-based debug console for LÖVE
Lua
286
star
18

lurker

Auto-swaps changed Lua files in a running LÖVE project
Lua
268
star
19

kit

A tiny library for making small games with big pixels
C
240
star
20

aq

A lightweight framework for creating audio toys
C
236
star
21

uuid4

A tiny C library for generating uuid4 strings
C
214
star
22

sfd

A small C library for opening a file dialog
C
171
star
23

sandbird

A tiny embeddable HTTP server written in C89
C
169
star
24

cmixer

Portable ANSI C audio mixer for games
C
168
star
25

aria

A tiny, embeddable lisp-shaped language implemented in C89
C
166
star
26

juno

Framework for making 2D games with chunky pixels in Lua
C
160
star
27

dmt

Dynamic memory tracker for C
C
105
star
28

autobatch

Small LÖVE module to automate the use of SpriteBatches
Lua
103
star
29

lite-colors

Colors themes for the lite text editor
Lua
99
star
30

cembed

A small utility for embedding files in a C header
C
93
star
31

doq

A small tool for creating single-file html documents
C
82
star
32

lovebpm

A LÖVE library for syncing events to the BPM of an audio track
Lua
76
star
33

coil

A tiny cooperative threading module for Lua
Lua
71
star
34

shash

A simple, lightweight spatial hash for Lua
Lua
66
star
35

console

A console plugin for the lite text editor
Lua
58
star
36

atlas

A small utility for generating a texture atlas
C
41
star
37

gifwriter

A Nim library for writing animated GIFs
C
18
star
38

rxi.github.io

HTML
17
star
39

cmixer_sdl2-nim

Tiny Nim library for using cmixer with sdl2
Nim
3
star
40

cmixer-nim

Nim wrapper for cmixer, A lightweight audio mixer for games
C
3
star
41

crate

2
star