Cookbook

Retro Gadgets comes with a few example gadgets that you can take apart and learn how they work, just look in the Tutorial section of the main menu. However, if you want some ideas on where to begin making your own gadgets from scratch, we've prepared a few guides to follow along:

  1. Sound Board - Learn how to build a gadget from scratch, using button inputs to trigger sound samples
  2. Collect The Dot - Use a LED matrix to move around a dot to collect other dots
  3. Moving Mike - Move a sprite around a screen with the D-Pad

Useful snippets

Here are some functions that can help with frequent use cases in your gadgets:

Mapping knob and slider values

-- a function that maps "value"
-- from a scale of "src_from" to "src_to"
-- into a scale of "tgt_from" to "tgt_to"
function map(value:number, src_from:number, src_to:number, tgt_from:number, tgt_to:number)
    return ((value - src_from) / (src_to - src_from) * (tgt_to - tgt_from)) + tgt_from
end

For example:

log(tostring(map(50, 0, 100, 0, 360))) -- displays "180"

Turning modules into lists

function getCompList(prefix:string, start:number, stop:number)
	list = {}
	for i = 1, stop - start + 1 do
		list[i] = gdt[prefix .. tostring(i + start - 1)]
	end
	return list
end

For example, if you have Led3, Led4 and Led5:

local myLeds = getCompList("Led", 3, 5)

myLeds[1].State = true -- turns on Led3
myLeds[2].State = true -- turns on Led4
myLeds[3].State = true -- turns on Led5

Turning notes into pitch values

This will turn notes in semitones relative to 1 into pitch values for AudioChip's SetChannelPitch:

function noteToPitch(note:number)
	return 1.05946 ^ (note-1)
end

Wrapping a number back to 0 after it reaches a certain value

You can use the modulo function (%, AKA division remainder) to do this:

-- define variable outside update to retain value between ticks
local value = 0

function update()
	-- increase value by 1
	value += 1
	-- set value for the remainder by division of 100
	-- if value is 0-99 this will keep the value the same
	-- if value is 100 it will now be 0
	value = value % 100
end

In this case, every tick value will increase by one, and then be truncated by a modulo 100. If the value is 100, it will return to 0.

Generating random numbers

This is done with the standard Lua math library:

-- no arguments: floating point number between 0 and 1
log(tostring(math.random()))
-- one argument: integer number between 1 and argument, both inclusive
log(tostring(math.random(6)))
-- two arguments: integer number between lower and upper numbers, both inclusive
log(tostring(math.random(5,10)))

Delays and timers

Check the FAQ answer about delays.

Collision between sprites

Check the FAQ answer about collisions.