GameMaker: Cycling random funky text

The idea behind this effect is simple enough - for every string character that should be randomized, replace it with any other character that has same width (so that the drawn string would not wiggle chaotically).

It uses 3 scripts total - one for general initialization, one for per-font init (creating a map of lists for what glyphs any given one could be replaced with), and one for actual string processing.

Can be handy for various abstract pieces, or as an interesting way to censor text.

Download GMK Live demo

Text versions follow,

/// string_rand_init()
// Is to be called on game start
global.string_rand_map = ds_map_create(); // >>
global.string_rand_exclude = ds_map_create(); // 
var s = " !@#$%^&*()[]{}<>_+-=*',./|\?" + chr(13) + chr(10) + chr(9);
var n = string_length(s);
for (var i = 1; i <= n; i += 1) {
    global.string_rand_exclude[?string_ord_at(s, i)] = true;
}
/// string_rand_init_font(fnt)
#args fnt
var excl = global.string_rand_exclude;
var r = ds_map_create(); // >
var _last = font_get_last(fnt);
var wm = ds_map_create(); // >
draw_set_font(fnt);
for (var i = font_get_first(fnt); i <= _last; i++) {
    var c = chr(i);
    var w = string_width(c);
    if (!ds_map_exists(excl, i) && w > 0) {
        var cl = wm[?w];
        if (is_undefined(cl)) {
            cl = ds_list_create(); // 
            wm[?w] = cl;
        }
        ds_list_add(cl, c);
        r[?i] = cl;
    }
}
ds_map_destroy(wm);
global.string_rand_map[?fnt] = m;
return r;
/// string_rand(text, font)
var fnt = argument1;
var m = global.string_rand_map[?fnt]; // >
if (is_undefined(m)) { // this font wasn't used yet
    m = string_rand_init_font(fnt);
}
var excl = global.string_rand_exclude;
var s = argument0;
var n = string_length(s);
var r = "";
for (var i = 1; i <= n; i += 1) {
    var c = string_char_at(s, i);
    var k = ord(c);
    var cl = m[?k];
    if (!is_undefined(cl)) { // has a replacement list
        r += cl[|irandom(ds_list_size(cl) - 1)];
    } else r += c; // not a known/visible glyph
}
return r;
Use:
// create:
string_rand_init();

// draw:
var fnt = fnt_test;
draw_set_font(fnt);
var txt = "Hello world!"
    + "#" + string_rand("Hello world!", fnt)
    + "##That's some nice text you have there."
    + "#It would be a " + string_rand("shame", fnt) + " if something was to #"
    + string_rand("happen", fnt) + " to it...";
draw_text(10, 10, txt);

Related posts:

2 thoughts on “GameMaker: Cycling random funky text

  1. hey, thank you for your perfect tutorials, posts, and all the things you do

    the link is broken, can you fix it please?

    • The “live demo” link was perfectly functional and had all the code, but I fixed the GIF and ye olde GM8 link

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.