AS3 Literal Syntax for XML

ActionScript 3 has all sorts of cool things. One of them is support for “E4X”, which is “ECMAScript for XML”. In other words, it’s got built-in XML primitives. Poorly documented primitives.

One of these primitives is the ability to insert literal XML code right in your app, and have it magically compile into XML objects at runtime. This has lots of handy uses. I’m using XML as a simple communication protocol between two apps, so I don’t need to load XML files… I just want to send small blobs of prefabricated XML back and forth. But of course I don’t want it to be entirely prefabricated… I want to insert variables and such into it. In my case, I wanted to have a prefabricated blob that also had dynamically-generated children.

Insert Dynamic Values Into Nodes: If you look in the help for this, you are directed to the friggin’ E4X specification, in PDF format! There is no documentation other than that. If you google around, you can find that embedding stuff in curly braces {} lets you insert literal symbols. This blob will insert the literal value of “foo” into the XML nodes stored in “cmd”:

var cmd:XML =<cmd><id>{foo}</id></cmd>;

Great! So far, so good. But now, I want to embed several CHILDREN in there, not just the literal value of one node. How do I do it? I tried setting “foo” to a string like “<child>5</child><child>6</child>” etc., but the <> symbols in my string were helpfully turned into &lt and &gt so it was still just the textual value of a single node.

Insert Dynamic NODES into your static structure: So how do you do this? I can’t find a way to magically do this directly in the literal XML. Instead, I left the dynamic part of the data out of the literal, and after it was constructed, I tacked the missing parts into the XML structure. To dynamically create nodes, you can explicitly construct an XML object with a string of text, and it will make that into XML node thingies. Then you can use the += operator to insert those nodes into your literal XML. Let me show you an actual small code snippet:

 

public function sendCmdSetDeckList(deckIdx:uint, ids:Array):void {
    var idxml:String = “<abilities>”;
    for (var loop:int = 0; loop < ids.length; ++loop) {
        idxml += “<ability><id>” + ids[loop].toString() + “</id></ability>”;
    }
    idxml += “</abilities>”;

    var msg:XML =<cmd>
                               <id>{ID_SEND_SETDECKLIST}</id>
                               <deckIdx>{deckIdx}</deckIdx>
                          </cmd>;
    msg.cmd += new XML(idxml);
    send(msg); // casts “msg” into a String and sends it out on a network socket
}

In this bit of code, I create a string of dynamic XML and stick it in the variable “idxml”. Then I create some static XML in “msg”, and then tack my literal XML code into msg, under the “cmd” tag, which is the top-level tag. After that, the “msg” xml structure has a node named <ability>, which has all of my ability IDs in it.
 
Note that the literal string must be “valid” xml, which means it must have only a single top-level node. Thus, I had to put all the <ability> nodes inside an <abilities> tag. If I didn’t do this, I would get a runtime error when I try to stick the string into a new XML object later in the function.
 
Forgive the mediocre example but I wanted to throw something up about this before I forgot about it (but I can’t spare too much time right this second). It’s very frustrating that there’s so little documentation for this. This reference was pretty useful, as far as it goes.

A neat trick in Windows XP

If you’re a developer, try going into the Sounds control panel and assigning sound effects to “Open Program” and “Close Program” events. These actually fire whenever a process starts or stops. It can be very informative. You’ll hear a cacophany of noise when you boot up, but more than that, you can watch various programs doing sneaky things. For instance, if you’re playing WoW with these set, you’ll hear the open/close sounds go off every time you alt-tab away from the game. It’s WoW’s anti-cheat software going off.

You’ll also hear this go off when a screen saver starts or ends, or when a program decides to check for updates, or many other times. It’s pretty interesting.

Perl vs. PHP

Perl is almost obsolete. This makes me sad given how much Perl I know. Not a year ago, I loved Perl. The power I could command at just a few characters! The language flexibility! I love being able to create my own custom ”dialect” of Perl for any given problem. It’s neat, and fast, and sometimes it’s even elegant.

But not really. It’s never actually elegant in the “easy to understand” sense. Plus, Perl is not an easy language to understand in general. The syntax for deeply nested references can confound the newbie Perl programmer for a very long time. And the distinction between variable types ($, @, %) seemed like a neat distinguisher… until I used PHP more.

PHP is like a really stripped down version of Perl. But it has much better OOP support. Like, tons better. I miss some things, like enforced variable declaration. And Perl’s variable scoping is more intuitive. Perl has better warnings for some things, too. But PHP’s simpler syntax proves more elegant while providing the same level of flexibility. It doesn’t have the beautiful maleability of Perl… but then again, it’s pretty maleable in its own ways. It’s not intended to be an “only one way to do things” language like Python.

One of Perl’s great strengths for me has been its nifty tricks for file I/O and common maintenance-scripting tasks. But Perl 5 has been stagnating while PHP has been growing by leaps and bounds. Most of the neat operators in Perl now have built-in function equivalents in PHP. Often they are even better than Perl’s version. Inevitably they are easier to comprehend. I’ve actually started using CLI PHP for quick one-off scripts. My uses for Perl are waning very quickly.

A year ago, when I was an active Perl hacker, I was angry about Perl 6. Perl 6 obsoletes all Perl 5 code. It’s not backwards compatible at all. “What is the point of this?” I wondered. Perl 6 has many new features, and it cleans up a lot of the syntactic awkwardness of Perl 5. But… a whole new language under the guise of a version-number upgrade?

Now, I can’t wait for Perl 6 to actually arrive. (In a viable, not-dog-slow-beta form.) It will be another year or two, I think, before the language is viable. But I would love to love Perl again.

My little black programming book

I admit it. It came from watching the cheesy TV show “Supernatural.” They have a tattered old notebook and it always has just the right clues needed to solve whatever problem. It’s awesome and I want one.

When I was a teenager I made one, actually. I spent hundreds of hours in classes. I still have it. Every 386 assembly opcode, all the VT102 terminal emulator codes, Pascal references, the works. However, unlike the book in Supernatural, my original programming book is never, ever useful. I have never needed to refer to it in at least 15 years. Programming stuff goes obsolete a lot faster than notes about demons, apparently.

So I made a new one. I found an old leather-bound 3-ring binder my dad used to use before he retired. It has that “hand me down” sort of feel. And I started making my programming book. I create the pages in Word and then print them out and hole-punch them. There’s stuff on PHP, SQL, the C++ STL, a cheatsheet on CSS, and so on. I do actually refer to it occasionally. It sits right here so sometimes it’s faster to find a needed diagram than to look it up on Google.

It isn’t sexy enough, though. It doesn’t have that “here is all my knowledge” sort of feel. So I bought a pencil and paper notebook at the store, with a nice cover and stuff. And I started writing in the stuff by hand. But I got two pages in and realized how stupid that was. So now I have a vaguely unsatisfying Programming Notebook that isn’t quite cool enough. And a mostly-empty journal book with just two pages written on it.

I could go into ghost hunting…

Unity… So close…

Unity is cool. It’s a 3D engine that runs in your browser. It gets decent framerate (unlike its competitors) and it looks like the tools are pretty well developed. Check out the demos if you’re bored — that’s the future of web gaming right there. Low-requirement, hardware-accelerated 3D.

I would love to take this for a spin. But I can’t.

Although the Unity plug-in works on all the major OSes and browsers, the development tools are only available for the Macintosh.

The Macintosh.

I don’t own a Mac — I have nothing against them, but they are just too expensive for me. And the thing is, neither do most game companies. This is a pretty big limitation! Huge, even.

I wrote to them, and got a response from Unity’s “evangelist”, who said a PC version was coming. He said I could count the time before the PC version launches “in months rather than in years” but then took pains to make sure I understood that any number of years could also be represented in months…

Honestly, I think this misstep may be what kills them. They’ve been around a while but their adoption rate is itsy-bitsy tiny because nobody can run the IDE. Sure, in six months or a year they will finally be accessible. But Flash Player 10 finally has hardware 3D primitives. How much you want to bet Flash 10’s adoption rate will be near 95% after a single year? Sure, Flash 10 won’t come with any useful tools for 3D world modeling, but somebody will build them.

Unity’s website talks about how adoption rates aren’t the end of the world as long as the install process is clean and easy enough. And the install process was easy for Unity. But… you go to a random web page, it says, “To play this game you need to install Unity, click here!”… and you’re just going to go to some other game page instead.

I am rootin’ for you, Unity… at least in theory. I haven’t actually played with the IDE so I can’t tell how good it really is. But even a mediocre 3D editor is infinitely better than what we’ll get out of the box with Flash 10. But … you need a killer app, right now before Flash 10 catches on. Can you do that? I don’t think you can.

MySQL’s GROUP BY vs. MS SQL’s GROUP BY

I had a really meaty post here comparing MySQL to MS SQL. But I realized how wrong it was, and changed it before it went live. I had been extolling a neat feature of MySQL, but ended up extolling a safety check of MS SQL instead.

I learned MySQL first, and only more recently started working with Microsoft’s SQL Server. There are a lot of little differences in the products — MySQL has lots of handy tools for small-scale web projects, with its built-in page-sizing LIMIT syntax, fast-prototyping features like ENUMs and SETs, and so on. It knows what it’s good at and it plays to those strengths. SQL Server has a decided advantage when it comes to stored procedures and views. Like, a huge advantage. I love MS SQL’s views. But it’s not a drop-in replacement… they each have very different abilities.

Here is where I was going to talk about MS SQL’s limitation on GROUP BY. But now it is not an either/or comparison … MS SQL’s version is just better. I just didn’t understand. MySQL’s error detection for GROUP BY just sucks and allows you to make big mistakes… and it led me to misunderstand what was happening under the hood.

Say you have this:

CREATE TABLE cakes (
    cake_id INT NOT NULL,
    cake_type INT NOT NULL,
    frostiness INT NOT NULL,
    creaminess INT NOT NULL
)

Now if you want to find the most frosty cake of each cake type, you can just say:
 
SELECT MAX(frostiness) FROM cakes GROUP BY cake_type

And you’ll get the highest frostiness level from each type of cake. In MySQL, you can do this too. But you could also say

SELECT MAX(frostiness), creaminess FROM cakes GROUP BY cake_type

It would proceed to spit out an arbitrary creaminess value from one of the cakes in each cake type. Which one? Who knows! In MS SQL, this is a syntax error. And that’s good.

Suppose you want to know all the data about the creamiest cake. Because I didn’t realize how MySQL chose the rows, I tried to do this:

SELECT *, MAX(creaminess) AS creamiest
FROM cakes
GROUP BY cake_type
HAVING creaminess = creamiest

Clever approach, huh? Group them by cake type, then use HAVING to choose only the ones with the highest creaminess. Except no, this just doesn’t work. What happens is it groups the rows by cake type, choosing one arbitrary row for each cake_type value. Each row does have the correct MAX(creaminess) value for the cakes in that category, but the rest of the data is not necessarily from the row where the MAX(creaminess) came from.

So this chooses some arbitrary rows, and then throws out a bunch of them if they don’t happen to be the ones with the highest creaminess for their type. Although it may seem to work (because it only outputs cakes with the highest creaminess for their type), it silently leaks cake types.

It’s just a malformed query. It’s not a bug. But in MS SQL, this is a syntax error.

Is there ever a time when you want an arbitrary row of data from a GROUP BY? Maybe… but I can’t think of one. Mostly, being able to access columns that aren’t in the GROUP BY statement is only going to introduce hard-to-track-down bugs in your code.

How valuable is your web game idea?

I’m working with a lot of web-game developers recently at FlashGameLicense.com, and find that a great many developers are over-protective of their ideas, prototypes, and even finished (but unsponsored) games, for fear that someone will steal their idea and make a clone. Not because someone will steal the game itself — that can be protected against — but that somebody will steal the idea and steal their glory.

I’ve been in the games industry a long time now, and only newbs are over-protective of getting their ideas stolen. Sure, lots of big companies are also over-protective of their ideas… I’ve done consultation jobs for huge teams where I’ve had to sign NDAs in order to even hear what the job is about. After I sign the NDAs, I’m all excited to find out what the secret is… and it’s always just another obvious game idea. When that happens, I know the team lead is a game development newbie. (I make an exception for unannounced IPs, which often really do require an NDA.)

This is especially true for Flash games and the like. Three reasons.

1) On the web, nobody really cares if somebody made this game before, especially if the idea was made 4 months ago or more. If a game is a SUPER smash hit, sponsors may not want to be involved with a competing version for a few months… so they’ll just wait. But after a few months, most gamers will have forgotten anyway… and the ones that didn’t forget will be ready to play a new, better-quality version of that game.

If somebody else’s version is much better than yours, it doesn’t matter if it comes first or second. If you have a great idea but make a poor implementation of your idea, you lose anyway, because somebody will make a better version of your game and get more money.

2) Game ideas are worthless. Only game implementations are valuable. No reputable sponsor or publisher EVER wants to see a “game idea” by itself without at least a partially complete game, because anybody can come up with good ideas. It takes skill to make ideas into games worth playing.

If your web game involves any of the following, your idea is extra super worthless:

  • Bouncing balls against paddles, walls, bricks, or other surfaces
  • Spotting hidden pixels
  • Moving a guy around the screen while he shoots things
  • Working through a giant maze
  • Matching colored bricks
  • Trying to keep something from touching some other thing
  • Moving a jumping guy across platforms

No matter what twist or turn you add: physics, mouse support, keyboard support, 3D, whatever… the idea by itself is worthless. It’s an incremental twist on an old standby. When people play it, they’ll think, “Oh it’s just a so-and-so with such-and-such added on.” Coming up with these kinds of incremental improvements is the most trivial kind of game design possible. That doesn’t mean it’s a bad game! It could be the best game in the world. But the idea alone? No value.

3) These are web games! These ideas often take an expert coder less than a month to implement. So how much of a head start can your game possibly get? If you launch it, and it is fun but has some minor flaws, someone else can just launch a better version in a month anyway.

Plus, no sponsor is going to say, “Oh I would have bought this awesome game, but I already saw a mediocre version a few months back, so no thanks. Not original enough.” Your game is only as good as the quality of your game. The “polish level”, I mean. The fun level.

Quality can’t be mimicked quickly. Say you have a new twist on an old chestnut… I dunno, how about you’re combining Break Out with Space Invaders, plus a twist of lime. Whatever. But you play-test it for hundreds of hours. You make each level really fun and really beautiful and really surprising. Your game is high-quality.

In this case, it doesn’t matter if people know what you’re making. They can’t steal your quality, and that’s what makes your game valuable.

In my opinion, the only time you need to be really secretive of your game idea is if you’ve got some unannounced business partner, IP, or awesome graphics resource. If you’ve struck a deal with Coca Cola, you should keep that deal under wraps until it’s official. But a typical game? The concept is valueless. The implementation is the only valuable thing.

Computational Linguistics

So I was on this “make an earth-shattering new programming language” kick the other week and bought a whole bunch of used books on Amazon. One of them was “Computational Lingustics” by Ralph Grishman. This is an old book, from 1986. But apparently it’s a classic in the field. It’s about teaching computers to read human languages like English. That’s what computational linguistics is, basically.

It’s funny to read it. You read each part and think, “this approach will never scale to real-world problems.” Over and over. For everything you read. But the thing is, the authors of these approaches know their solutions don’t scale. They are just computer scientists building things in the hopes that other people can build better things from them. And they are so hopeful for the future.

But the future turned out bleak for computational linguists. The concepts we had in 1986 are the same things we have now: interpreters for very small sets of the language only. No real progress on the knowledge-storing solution, let alone the grammar parsing solution.

To my untrained eye, the book broke down into two big problems. If you want a computer to be able to understand English, you need to teach it the grammar of English. This is a massive undertaking. The book is full of great examples about why this is hard, like the “gapping phenomena” we find in English conjunctions.

In English, conjunctions can be used to combine a whole sentence with a fragment of a sentence in the same form as the first half, optionally dropping parts. For instance, if your sentence is “subject verb object”, then you can write “subject verb object AND subject verb object”, or “subject verb object AND verb object”, or “subject verb object AND object”. All these are allowed:

I ate the cake and Bob ate the pasta.
I ate the cake and ate the pasta.
I ate the cake and the pasta.

But there are special cases. It’s also okay to say “subject verb object AND subject object”. It looks a little archaic but it’s totally legal.

I ate the cake and Bob the pasta.

The thousands of special case rules in the language make a traditional grammar impractical. There are many theoretical approaches to fixing this outlined in the book, but none of them scale to the size needed.

And then we get to the “context” part of English. If you see a paragraph like:

“In the early morning dawn, the battleship fired two torpedos at the cruise liner. It sank beneath the ocean, leaving no survivors.”

Did the battleship sink or the cruise liner? You know, because you know that when you fire a torpedo at a boat, it is likely to sink. A computer does not know that, and cannot disambiguate this sentence.

There are lots of approaches to this problem, too. For instance, the concept of “scripts” could help. The idea is that we store data about the world in stories — little scripts about how something works. When faced with the sentence,

“Bob was seated at the booth and his order was taken quickly, but thirty minutes later he still had no food. Bob was furious.”

Why was Bob furious? If we think of “going to a restaurant” as a script, then the script goes something like “you are seated, you place your order, you get your food, the waiter checks on you, you receive the check,” etc. And somewhere in there you could codify the notion that steps shouldn’t take more than a certain amount of time to complete.

If you thought, “Wow, that approach will never scale,” then you win a vapor cookie. Because of course it won’t. Well, didn’t. None of the other approaches have scaled either, as is evidenced by the lack of computers who can interpret English paragraphs.

I think the whole approach is wrong. This is the “brute force” approach to a solution. The final correct solution won’t use anything even remotely resembling these approaches. It will have to tackle the problem from a completely different angle.

The Perfect Language

Who doesn’t want to write their own language? You liar! Down deep you want to. Everybody does. Everybody! I co-authored a language once before, and it was a big learning experience. (It was a private language for a game company, and it’s still in use today.) Afterwards I didn’t really feel the need to make a language again for many years. But the bug is coming back. None of the languages out there are quite good enough.

They lack compilability, for one thing. I appreciate the “no compiling” nature of scripting languages like PHP or Perl, believe me. But when the program gets big, when I have 20,000 lines of code to manage, I want to be able to syntax check the damned thing before I run it.

Consider this common case: you have a massive Python program embedded in your C++ app. You decide, hey, this function foo is too unwieldy. I’m going to break this into two functions that have different semantics and parameters: fooA and fooB. Now you search and replace every “foo” in your program and turn it into a call to fooA or fooB. And now, to test your work, you have to start up your C++ app and see if it crashes. And it will. You better hope your unit test suites can catch most of them…

Refactoring tools are supposed to help. But wouldn’t you know it, refactoring tools are only actually powerful in Java… which is a compiled language. The refactoring tools for, say, PHP or Python are pretty pathetic.

There are also tools in the same vein that are supposed to “syntax check” your code for you. However, the Python tools I used for this purpose were just not any good at it. I didn’t use lambda functions or dynamic class construction or anything like that, either. They couldn’t find simple typos in calls to predefined function names. Fail.

So the perfect language should be runnable as a scripting language AND should be compilable. For this to be useful, the language will have to lose some of the more outlandish features of modern scripting languages, such as the ability to dynamically add and remove functions from classes. If you can just add any function to a class, then there’s no way for the compiler to know if a syntax error is a syntax error, or just a function that won’t exist until run-time. Plus, it’s maddeningly hard to follow code that dynamically adds functions to classes. I won’t miss this.

I’ll need serious interface support. I guess I might as well have exceptions too. And the other usual suspects: dynamic list and hash support, nifty regex and text procession operators, probably some built-in web management APIs. Hmm, this will take 8 to 12 months to get to beta. That’s a long time…

And what’s the killer app for my language? Ruby was around for a long time before Rails came out and made people notice it. Rails is the killer app for Ruby. (More about Rails later… I’m already going off on a tangent.) But what’s my killer app for my language?

I guess I don’t have one. I don’t want a language that languishes. So I guess I’ll just not write one. It’s just not practical! It makes no sense to write my own language, and I don’t have the time. But it won’t stop nagging me! “Coooodddde meeeeee,” says the perfect language. “Coooddddde Meeeeeee!

Flex 3

If you haven’t played with Flex yet, maybe you should. It’s Adobe’s new way of making Flash programs. But instead of using an animation editor, you get to actually code in a real programming language. And it’s even a good language!

The Flex language has two parts. First there’s Actionscript, which is extremely similar to Javascript, but get this: it’s compiled! This makes it much easier to work with for large projects.

The other part of Flex is an XML format that lets you layout forms and whatnot. You can also embed Javascript right into the XML file, and most people do. So in a sense you might think of it as a replacement for HTML+Javascript — it’s XML+Actionscript. But it’s compiled, and without any browser compatibility issues.

97% of web users have Flash Player 9 and can run your Flex app. THAT is what’s so powerful about it.  Nothing else comes close to that level of acceptance. (Java is around 80%.)

The big weakness, to me, is the lack of 3D primitives in the Flash VM. But the next version of Flash (in beta now) has basic hardware 3D primitives! Which means the next version of Flex will be able to use those primitives. It’s a bit early to code your amazing game in Flex just yet, but in a year or two I think we will see an awful lot of them.

Flex is going to be really big with online games. And I am so there.