Skip to content

Instantly share code, notes, and snippets.

View britzl's full-sized avatar
💭
Making Defold even better!

Björn Ritzl britzl

💭
Making Defold even better!
View GitHub Profile
@britzl
britzl / traceback.lua
Created April 2, 2014 16:15
Example of how debug.traceback() works
local function a()
print(debug.traceback())
end
local function b()
a()
end
local function c()
b()
@britzl
britzl / errorhandler.lua
Created April 2, 2014 20:08
Example of how to wrap script entry points to be able to do custom error handling
local function on_error(err)
print("In custom error handler:", err)
-- something went wrong, handle error here
-- report to client health for instance
-- print(debug.traceback())
-- dump tables
end
local function do_something(a_number)
-- if this check fails it will be caught by the error handler
@britzl
britzl / prettyprint.lua
Created April 2, 2014 21:27
Example of how to override print and pretty-print tables
-- keep the original print()
local _print = print
local indentation = ""
--- Pretty print a value
-- If the value is a table it's content will be printed as well (recursively)
local function pretty_print(value)
local t = type(value)
if t ~= "table" then
@britzl
britzl / keybase.md
Created May 13, 2014 21:47
keybase.md

Keybase proof

I hereby claim:

  • I am britzl on github.
  • I am britzl (https://keybase.io/britzl) on keybase.
  • I have a public key whose fingerprint is 0550 F9F9 695B AA0E C6A3 E29E 081A 6067 688C FEA3

To claim this, I am signing this object:

@britzl
britzl / superstrict.lua
Last active August 29, 2015 14:02
Lua strict mode variant with a whitelist for allowed names
--- A module for guarding tables from new indices. Typical use is to guard the global scope.
-- Get the latest version from https://gist.github.com/britzl/546d2a7e32a3d75bab45
-- @module superstrict
-- @usage
--
-- -- Defold specific example. Allow the gameobject and gui script lifecycle functions. Also allow assignment of
-- -- facebook and iap modules for dummy implementations on desktop. The whitelist handles pattern matching and in the
-- -- example all functions prefixed with '__' will also be allowed in the global scope
-- local superstrict = require("superstrict")
-- superstrict.lock(_G, { "go", "gui", "msg", "url", "sys", "render", "factory", "particlefx", "physics", "sound", "sprite", "image", "tilemap", "vmath", "matrix4", "vector3", "vector4", "quat", "hash", "hash_to_hex", "hashmd5", "pprint", "iap", "facebook", "push", "http", "json", "spine", "zlib", "init", "final", "update", "on_input", "on_message", "on_reload", "__*" })
@britzl
britzl / gdump.lua
Last active August 29, 2015 14:05
Dump contents of global table
local function gdump()
local function dump_table(t, indentation)
indentation = indentation or ""
local table_model = {}
for k,v in pairs(t) do
table_model[type(v)] = table_model[type(v)] or {}
table_model[type(v)][k] = v
end
for type,_ in pairs(table_model) do
print(indentation .. type .. ":")
# from http://stackoverflow.com/a/8597411/1266551
if [[ "$OSTYPE" == "linux-gnu" ]]; then
# ...
elif [[ "$OSTYPE" == "darwin"* ]]; then
# Mac OSX
elif [[ "$OSTYPE" == "cygwin" ]]; then
# POSIX compatibility layer and Linux environment emulation for Windows
elif [[ "$OSTYPE" == "msys" ]]; then
# Lightweight shell and GNU utilities compiled for Windows (part of MinGW)
elif [[ "$OSTYPE" == "win32" ]]; then
@britzl
britzl / luabindown.sh
Created August 25, 2014 05:36
Shell script to download an OS specific Lua 5.1 binary
#!/bin/bash
# Downloads an OS specific Lua 5.1 binary
if [[ "$OSTYPE" == "linux-gnu" ]]; then
LUA_DOWNLOAD_URL="http://sourceforge.net/projects/luabinaries/files/5.1.5/Tools%20Executables/lua-5.1.5_Linux32_64_bin.tar.gz/download"
LUA_BINARY=lua5.1
elif [[ "$OSTYPE" == "darwin"* ]]; then
LUA_DOWNLOAD_URL="http://sourceforge.net/projects/luabinaries/files/5.1.5/Tools%20Executables/lua-5.1.5_MacOS109_bin.tar.gz/download"
LUA_BINARY=lua5.1
elif [[ "$OSTYPE" == "win32" ]]; then
@britzl
britzl / timer.lua
Last active February 13, 2018 14:18
Timer module that can be used to get a callback when a certain amount of time has elapsed
--- Module that can be used to get a callback when a certain amount of time has elapsed
--
-- @usage
-- local timer = require "timer"
-- function init(self)
-- self.t1 = timer.seconds(2, function()
-- print("2 seconds have elapsed")
-- end)
-- self.t2 = timer.seconds(5, function()
-- print("5 seconds have elapsed")
@britzl
britzl / bundle.sh
Last active December 10, 2020 13:45
Script to build and bundle a Defold project for multiple platforms
#!/bin/bash
title=$(less game.project | grep "^title = " | cut -d "=" -f2 | sed -e 's/^[[:space:]]*//')
title_no_space=$(echo -e "${title}" | tr -d '[[:space:]]')
echo "Project: ${title}"
if [ ! -f bob.jar ]
then
echo "Unable to find bob.jar. Download it from d.defold.com."
exit 1