Author Topic: Tutorial: Using Perl to Embed Assets & Speed Up Game Development Workflow (Haxe)  (Read 1278 times)

LaughingLeader

  • Member
  • **
  • Posts: 64
  • Karma: +0/-0
    • View Profile
    • LaughingLeader Blog
Hi guys.

I've written a pretty extensive tutorial on using Perl to embed assets and speed up your workflow.

This script will generate a class that holds all of your embeds, via the embed metadata tags in Haxe (@:sound, @:bitmap, etc.).

The end result will look something like this:





Note that I created this for my projects in HaxeFlixel. With some tweaking, it could easily be used for Flixel. (I actually originally used it with Flixel, before I switched over to HaxeFlixel).

You'll have to change the $OUTPUT_FILENAME extension from "hx" to "as" of course, and tweak the script to your liking.

For instance, you could edit the writeFile subroutine:

Code: [Select]
sub writeFile
{
my $em = shift;

    open OUT, ">$OutDIR/$OUTPUT_FILENAME" or die "$!\n$OutDIR\n";
    print OUT <<"EOT";
// Warning: this file is generated by $FindBin::Script, do not edit.
package $PACKAGE;
{

class $CLASS
{
$em
}
}
EOT
    close OUT;
}

Then tweak the process() function at this point:

Code: [Select]
} if($addComment == 0) {
push @embeds, {
id    => uncapitalize($filename),
path   => "('".$file."')",
upper  => capitalize($filename),
lower  => uncapitalize($filename),
caps  => capslock($filename),
class => "extends ".$class." {}",
metadata => $meta,
type => "class ".$type."_",
};
}

Changing it to something like:

Code: [Select]
} if($addComment == 0) {
push @embeds, {
id    => uncapitalize($filename),
path   => "('".$file."')",
upper  => capitalize($filename),
lower  => uncapitalize($filename),
caps  => capslock($filename),
class => ":Class;",
metadata => [Embed(source=,
type => "public static const ".$type."_",
};
}

And then the original formatEmbeds sub will work:

Code: [Select]
sub formatEmbeds
{
    my $input = shift;
    my $indent = 0;
   
return formatGrid($indent, [ map {["$_->{metadata}$_->{path}", "  $_->{type}$_->{caps}","  $_->{class}"]} @$input ], 0);
}

So the result should look like:

Code: [Select]
class EmbedAssets
{
[Embed(source='/sound/player_hurt1.mp3')] const SFX_PLAYER_HURT1 :Class;
[Embed(source='/sound/player_hurt2.mp3')] const SFX_PLAYER_HURT2 :Class;
}

Hope that helps.

paala

  • Contributor
  • ****
  • Posts: 250
  • Karma: +0/-1
    • View Profile

auriplane

  • Snails!!
  • Contributor
  • ****
  • Posts: 497
  • Karma: +1/-0
  • Snails!!
    • View Profile
Yay, you used my script! :-)