Skip to content
FloatingBanana edited this page Mar 18, 2021 · 6 revisions

Introduction

draworder.lua allows you to call functions (usually love.graphics functions) in a specified order.

Example

local order = require "grove.draworder"

local image = love.graphics.newImage("image.png")

local function drawImage(x, y)
    love.graphics.setBlendMode("subtract")
    love.graphics.draw(image, x, y)
    love.graphics.setBlendMode("alpha")
end

function love.draw()
    order.queue(3, "setColor", 1, 0, 0, 1)
    order.queue(1, "setColor", 0, 1, .5, 1)
    order.queue(4, "circle", 100, 100, 50, 100)
    order.queue(2, "rectangle", 50, 50, 100, 100)
    order.queue(-7, drawImage, 130, 25)

    order.present()
    --Here will be called all queued functions in the following order:
    --drawImage(130, 25)
    --love.graphics.setColor(0, 1, .5, 1)
    --love.graphics.rectangle(50, 50, 100, 100)
    --love.graphics.setColor(1, 0, 0, 1)
    --love.graphics.circle(100, 100, 50, 100)
end

Functions

order.queue(order, func, ...)

Adds a function to the queue stack.

order (number): The position in the stack. Accepts any number, even math.huge.

func (string or function): The function to be executed. If you give a string then love.graphics[func] will be called.

... (mixed): The arguments to be passed to func.


order.present()

Executes all queued functions in the defined order and clears the stack.


order.setSortingFunction(func)

Defines a custom sorting function that will affect the order the queued functions will be executed.

func (function): A custom sorting function. If omitted, a default one will be used

Function signature:

local function sort(a, b)
    local a_order, b_order = a.order, b.order --The order you passed in the order.queue
    local a_stack, b_stack = a.stack, b.stack --The order that function was queued

    --write your sorting function here
end

order.clearQueue()

Clears all queued functions


order.clearCache()

Clears all cached tables.

Note: draworder.lua stores all queued functions and arguments in tables. These tables are created when calling order.queue. To avoid creating a lot of tables in every update, some tables are cached to be reused later.


order()

Creates a new order instance. Remember to call the functions with the : operator. Ex: instance:present()

Clone this wiki locally