GameMaker: Shadow casting in platformer games

This is a very old example of mine actually (creation timestamp says Sep 2010), which I've recently fixed & improved at request.

It demonstrates a simple algorithm to cast shadow from instance in platformer game. So when player jumps, shadow is displayed on ground below (realism!).

For the sake of simplicity and performance, shadows are checked against bounding boxes by default. Pixel-perfect checking is added as a separate condition, requiring affected objects to be children of obj_complex.

If you're interested in how this works behind-the-scenes, here's an illustration:

Outlined instances are ones used for finding top-most position below the player (which shadow will be cast onto). Red line is a number of point checks to indicate top-most point on objects with precise masks.

As another random visual tweak, there's a piece of code added to make shadow shrink as caster gets further from ground. A small touch, but these add up over time.

Download GMK

GameMaker – Substitution “cipher”

This example provides a function to substitute all characters in a string, which present in first set of characters, by characters from second set. This can be used to substitute l/e/a/s/t/o characters in a string by 1/3/4/5/7/0 accordingly, turning your "Hello World" into "H3110 W0r1d" in single script call, or to replace/swap characters with completely irrelevant ones, providing simple "encryption" (e.g. Caesar cipher or various substitution methods) to challenge the player.

Functional part is presented by a single script, named string_subst(string, from, to), which returns string with all characters from set from replaced by according characters from set to. As long as both from and to are of equal length and do not contain repeating characters, output can be "decrypted" by passing parameters in swapped order, e.g.

var asrc, adst, source, encr, decr;
asrc = "0123456789"; // source "alphabet"
adst = "3456789012"; // destination "alphabet"
source = "51"; // source text
encr = string_subst(source, asrc, adst); // "encrypted" text
show_message(encr); // Displays "84;"
decr = string_subst(encr, adst, asrc); // "decrypted" text
show_message(decr); // Displays "51;"

Attached example demonstrates both "encryption" and "decryption", and has nice buttons.

Download GMK

Continue reading

GameMaker: Cycling random funky text

This example pictures something that I wanted to make for few days - awesome funky random text script.

The idea behind this effect is simple - for every string character that should be randomized, replace it with any other renderable character that has same width (so that output string would not shift in directions, and you could somewhat guess the words if you know letter widths well).

To keep lookup speeds optimal, system generates a hashtable (ds_map) for each font when it's first used with it. This allows high processing speeds even for GameMaker: Studio with it's 65K character limit per font.

Program interface consists of 3 functions, one of which is initialization (required to be called before you try to use system), one is font hashtable generation (just in case you want to push these manually at game start), and actual text_rand(string, font) function, which will return a string with randomized character for given font.

This sort of rendering may come particularly useful for different experimental games, along as an interesting way to censor text.

Download GMK Live demo

GameMaker: Input field (semi-advanced)

There are multiple cases where you may want to let user input text in a familiar way, keeping things intact with system settings and allowing minor tweaks like repositioning the text cursor. Such include consoles, chatboxes, input fields, and other. This example covers these needs with fairly short (less than 70 lines total) and fairly understandable code. It includes the following features:

  • Keeps track of input text on window focus loss. If you've tried to make an input box before, you may be similar with that unpleasant default behaviour of keyboard_string in GameMaker.
  • Allows to reposition cursor (left / right / home / end keys)
  • Allows to use Delete key to erase characters
  • Can be easily modified to support other keys and shortcuts

Example includes actual input field object, and sample list drawer, to which typed text is sent upon press of Enter.

Download GMK

GameMaker: Jetpack platformer

This is an older example of mine, featuring pixel-perfect movement and collision engine for jetpack platformer game. As extras, it includes particle spawning, and impact velocity calculations. Current version is updated to have a cleaner code style (no more bracket-less conditions and seriously doubtful operator combinations), as long as more comments (increasing comment coverage to a value close to 100%). Also there was a game being made around this concept once.

Download GMK

Haxe: Neko Server-client communication example (chat)

Recently I've been searching for examples of client-server communication in Haxe, however could not find anything specific. After some search and asking around, I was pointed to class, but the actual means of usage remained unclear. It was also confirmed to mirror POSIX socket functionality. Indeed it does that, though, given that Haxe implementation uses exceptions rather than return values, usage remained uneasy.

After some experimenting, I've figured a semi-simple way of using "blocking" sockets.

Continue reading

GameMaker: Finding resources by name

If you have been working with GameMaker, it might have been not a single time when you would really want to be able to do something like:

instance_create(x, y, 'obj_item' + string(irandom_range(1, 3)));

However, as you probably noticed, you cannot simply do that. That's because resource indexes are integers, and not strings. It is normally recommended that you do something like this:

var i;
switch (irandom_range(1, 3)) {
	case 1: i = obj_item1; break;
	case 2: i = obj_item2; break;
	case 3: i = obj_item3; break;
instance_create(x, y, i);

That works (and actually is one of fastest approaches to this), though it's doubtful fun to create these structures by hand. And even more doubtful fun if you need complex concatenation rules (e.g. spr_player_{attack}_{1}). Another thing you can see done often is:

execute_string('instance_create(x, y, obj_item' + string(irandom_range(1, 3)) + ');');
// Some say that you're a horrible person if you use such code.

However, this can be a performance pitfall, since the game has to parse a piece of code at every call. Oh, and also this won't work in GameMaker: Studio, since dynamic scripting functions have been excluded entirely.

At this point you're probably going to ask "And what are you going to suggest then?". I suggest a ds_map. This can be considered an elegant solution, since look-up will take about the same time for a given number of resources, and the only (relatively) time-consuming operation needs to be executed once at game start.

Example attached covers the initialization part for you, providing simple interface for actual lookup:

instance_create(x, y, object_find('obj_item' + string(irandom_range(1, 3))));

Download GMK Download GMZ

GameMaker: Circular cooldown rectangle

GameMaker: Cooldown rectangle

I do not know the exact name of this visual effect (angular/clockwise fade-in/fade-out?), but it is one of the most common things that you can see in interface of RPG and RTS games, displaying progress of construction/upgrade/ability cooldown. So, to the point...
You can read how this is actually being implemented, or just scroll down to the bottom of post and grab the code and/or GMK file.
Implementation of such is actually pretty simple, Continue reading