Tutorials


Linked Lists in Lua

As an example of creating custom data structures in Lua, I thought I'd give a little demonstration of a linked lists implementation. Note that this isn't going to be an in-depth tutorial or anything; for the most part I'll just be showing pieces of the code.

Linked lists are wonderful data structures, perfect if you need to efficiently add, remove, and iterate through elements. The one trade off is that you can't find an element by an index.

Read more

Custom Cursors in Love2D

When making a game in LÖVE, you'll probably end up needing to customise the mouse cursor. As I recently had to do just that, I thought I'd make a quick tutorial on it. If you want an example cursor, here's the crosshair I'm using in a game right now.

Now, all of what we need is contained within the love.mouse module. The first step is to hide the mouse with love.mouse.setVisible. You'll probably want to do this in love.load:

Read more

Lua for Programmers Part 4: Tips and Tricks

If you haven't read the previous parts (one, two and three), then I'd highly recommend doing so. For reference, here's a list of the parts in this series:

Command line arguments are stored in the arg table. For example, if we had the script foo.lua:

Read more

Lua for Programmers Part 3: More Advanced Concepts

If you haven't read part one and two already, I'd highly recommend doing so. For reference, here's a list of the parts in this series:

In part one I stated that Lua has "braces of a sort in the form of keywords like then and do." Much like a language with C-based syntax, these "braces" represent blocks, which are essentially sequences of statements. An example of some blocks in Lua:

Read more

Lua for Programmers Part 2: Data and Standard Libraries

If you haven't read part one already, I'd highly recommend doing so. For reference, here's a list of the parts in this series:

  • Part 1: Language Essentials, covers fundamental syntax and concepts such as operators, loops, and functions.
  • Part 2: Data and Standard Libraries, the current part; covers Lua's built-in data types and some of the standard libraries.
  • Part 3: More Advanced Concepts, deals with things like variable scope, advanced functions, and file loading.
  • Part 4: Tips and Tricks, a collection of small things that you may find useful.

Tables are essentially associative arrays which can have keys and values of any type. Tables are incredibly flexible, and are the only data structure in Lua, so knowledge of them is crucial.

Read more

Lua for Programmers Part 1: Language Essentials

In this series we'll be taking a look at most everything you should know to program in Lua. The series is targeted at people who already know how to program, and as such I'll aim to be brief in my explanations.

Here's an overview of the four parts the compose the series:

In case you didn't already know, Lua is an interpreted programming language. It's fast, flexible, embeddable, simple, and easy to learn. You can get Lua from its download page.

Read more

Getting More Ratings in Ludum Dare

I thought I'd share a few quick tips on getting more ratings, which I've picked up in my experience with Ludum Dare. Please note, I'm not putting this down as fact or anything, but merely expressing my own opinion.

Yeah, this one's kind of obvious by now I'd say. Your "coolness level" increases by one per game you rate, and the cooler you are, the higher chance you have of getting rated. Games are picked for people to rate both by how high the author's coolness is, and how low the number of ratings are.

Read more

How It Works: The Thruster in Facilitated Escape

In this post I'll be taking a look at how I programmed aspects of the ship's thruster in my game, Facilitated Escape. I'll most likely be making more posts like this in the future (as in, I intend to make a series of "How It Works" posts).

Initially I had a lot of trouble getting the thruster to look right; you might be able to see some of my troubles in the Ludum Dare time-lapse. The reason for this was that I had programmed the game to move the ship on the y axis instead of moving everything else, as expressed by this code (line 72 of ship.lua)

Read more

Mouse Dragging in Love2D

In this tutorial, I'll be taking you through my method of enabling objects to dragged by the mouse (in LÖVE of course). You can view the completed code for this tutorial at gist #1196228.

So, say you have an object with x/y coordinates, width, and height, and you want this object to be draggable by the mouse. For this tutorial, I'm just going to construct a table and put in the global, rect:

function love.load()
  rect = {
    x = 100,
    y = 100,
    width = 100,
    height = 100,
    dragging = { active = false, diffX = 0, diffY = 0 }
  }
end

The next thing we need to do is define love.draw to draw the rectangle. (We'll see what rect.dragging is all about soon.)

Read more

Glow Effect for Lined Shapes in Love2D

Recently I experimented with an easy way to make lined shapes glow (using LÖVE of course). There are of course other ways of doing it, and there are many styles of glow that can be used, but this is the one I came up with.

love.graphics.setColor(r, g, b, 15)

for i = 7, 2, -1 do
  if i == 2 then
    i = 1
    love.graphics.setColor(r, g, b, 255)
  end

  love.graphics.setLineWidth(i)
  -- draw lined shape here
end

The shape gets drawn multiple times, each time with a different line width, set by i. In all, six shapes are drawn with the widths 7, 6, 5, 4, 3, 1. For all the lines, except the last, the alpha of the colour (by the way, r, g, and b represent the respective values of whatever colour you might choose) is set to 15. Since the alpha of every overlapping colour is added to each other (by default), for each line the colour will get stronger and stronger, giving the glow effect. The last line is given an alpha of 255, since this is where all the "light" is meant to be coming from.

Read more

Lua Metatables Tutorial

In this tutorial I'll be covering a very important concept in Lua: metatables. Knowledge of how to use metatables will allow you to be much more powerful in your use of Lua. Every table can have a metatable attached to it. A metatable is a table which, with some certain keys set, can change the behaviour of the table it's attached to. Let's see an example.

t = {} -- our normal table
mt = {} -- our metatable, which contains nothing right now
setmetatable(t, mt) -- sets mt to be t's metatable
getmetatable(t) -- this will return mt

As you can see, getmetatable and setmetatable are the main functions here; I think it's pretty obvious what they do. Of course, in this case we could contract the first three lines into this:

Read more

A Guide to Getting Started with Love2D

In this post, I'll attempt to give you my personal guide on some good steps to getting started with the Love2D game engine (the proper name is LÖVE, which I'll be using from now on). It's not perfect, of course, but I hope you find it useful. If you have any feedback, I'd love to hear from you in the comments.

I'm guessing you probably already know, but for those who don't, LÖVE is a 2D game engine (or, framework). It's an environment which contains a lot of pre-written code targeted at making games. It interfaces with the Lua programming language to makes things even easier for you.

Read more

Making a 3D Button in CSS

In this tutorial I'll be showing you how to create a 3D-looking button, made out of pure CSS. The button we'll be building is similar to the button I've used on my project pages. Take note that this uses a number of CSS3 properties, like border-radius and box-shadow. Anyway, let's get into it.

The only HTML code we'll need is a div tag with the proper class:

<div class="big-button">Your Text Here</div>

If you want the button to link somewhere, just wrap a link round it:

Read more

ASCII Code Manipulation

While inspecting the ASCII table a little while ago, I noticed some interesting patterns in it, which can be used for string manipulation. It's important to understand the ASCII table, because it's the base of most encodings.

Note I'll be using C-like syntax in my examples, but I won't be taking advantage of C's characters, which convert stuff like 'a' into 97 (a's ASCII code).

For those who don't know what much about ASCII codes, I'll give you a quick introduction. ASCII stands for "American Standard Code for Information Interchange", which assigns certain numbers to certain common characters. Since computers can only work with numbers, every letter, or character of any sort, must have a number associated with it. ASCII assigns numbers to 128 different commonly used characters (although many of the non-print characters are no longer used for their original purpose).

Read more

Cameras in Love2D Part 3: Movement Bounds

Because there was some interest in a part 3, of this series, I've written it, and in this part we'll cover creating bounds that the camera can't move beyond. Make sure you've read part 1 and part 2 before continuing.

In case you're wondering what I mean by this, I mean restricting the movement of the camera to a "box", as in, having minimum and maximum x/y coordinates for the camera. This comes in handy when you're following players, for example, and you don't want the camera to show any of the stuff beyond the level (usually blackness) when the player comes to an edge. Now of course, movement bounding can get much more complicated than a simple rectangle, you restrict it to certain paths and the like, but we're going to keep it simple here.

Read more

Draw Origins in Love2D

In this post I'm going to be showing you origins when drawing stuff in Love2D. First of all, what are origins? They specify the offset for the origin of the object's x/y coordinates. In other words, if you specify the x origin to be 20, the object will be drawn 20 pixels to the left, as in x - 20. It's the same for the y origin: if we have a y origin of 20, the object will be drawn 20 pixels up, as in y - 20.

This allows us to do many useful things. First of all, if we have an object with centre based coordinates (like physical bodies), instead of drawing like this:

Read more

Cameras in Love2D Part 2: Parallax Scrolling

In part 1 we constructed a basic camera. Now we're going to extend it by adding some parallax scrolling. Note that the method I'll use is probably not the prettiest, as I came up with it in half an hour. Nevertheless, this will be a starting point for you to develop your own system.

Now, for those who don't know, what is parallax scrolling? It's a way to get a pseudo-3D effect while still have all graphics and gameplay based in 2D. It's currently used very widely among 2D games, as it adds a sense of depth that normally isn't there.

Read more

Cameras in Love2D Part 1: The Basics

This is the first of a couple of blogs on creating cameras in the LÖVE engine. This part will deal with the fundamentals of creating a camera. Part two will deal with parallax scrolling and creating layers. So, let's get to it!

Update: I've actually ended up writing a part 3, which covers restricting camera movement.

Read more

Specifying Names in Lua Without Strings

Lua is the most flexible language I've ever used, aside from its speed, this is probably the best thing about it. In this post, I'm going to show you how to specify names of things in function arguments, without strings. The method I'll use it a bit hackish, and isn't fit for normal operation; but hey, Lua allows us do it!

For an example, say you had an OOP implementation that used a function called class like this:

class('MyClass')

What we're going to change that into, is this:

Read more

Implementing Proper Getter/Setters in Lua

When I create a class in Lua, there are always times when I need to use a getter or setter on attributes, instead of raw access. The way I've always done this is to use methods with names like getFoo and setFoo. And then to keep my API consistent, I have to switch every single property to use these getter/setter methods. The pain about these type of methods is that:

  • You have to switch everything, even attributes that don't need getter and setters, to keep your API consistent. This would end up slowing everything down.
  • It doesn't look as proper as using real getting and setting syntax, like obj.foo and obj.foo = v.
  • It's more to type.

What I really wanted was syntax like this:

Read more