Home       About Me       Contact       Downloads       Part 51    Part 53   

Part 52: Scripting Languages

March 27, 2012

I'm currently working on two separate topics -- a new version of Crafty, and a new landscape algorithm. I've been switching back and forth and neither is ready for release. So here's another filler part, on scripting languages.

Rube Goldberg: Didn't he design C++?

The goal of Crafty is to be a framework for development of block-world games. I could just let people program their games in C++ and call into the framework. However, there will be plenty of casual game designers who would like something less formidable than C++. A scripting language would be nice, and there are several open-source alternatives.

Here are my criteria:

  • Familiarity. I want a language that a reasonable number of programmers will know, that's well documented, and has a decent-sized community online. I don't want to be in the business of writing tutorials or debugging scripts for people.

  • Integration. I need to integrate the scripting language into the framework. I need to manage its use of resources (trivial loops in the script should not freeze the game), and easily call into my framework from the scripts.

  • Development Environment. For a first version, it would be acceptable to have a separate tool that compiles scripts. When an error occurs (or the script "prints" an output), the message could appear on an in-game console window.

    As games get more complex, I don't think this will be enough. Ideally, I'd like an in-game development environment, where you could create your castle, create an instance of a dragon, open a window and attach a script to the "someone approaches" event handler for the dragon, and then debug the script. This is a significant piece of work with most of the languages I've looked at.

Some Open Source Languages

I've been looking around the net for various open source language implementations. Here's what I have so far:

Lua

Lua is a small interpreter which claims to be "the leading scripting language in games", and to have been used in World of Warcraft. I haven't played with it at all yet, but it sounds like it would do the job. Code looks like this:

 -- add all elements of array `a'
function add (a)
  local sum = 0
  for i,v in ipairs(a) do
    sum = sum + v
  end
  return sum
end

Skimming the documentation, it actually has more language concepts than I would have put into a scripting language. The biggest downside is that I would have to learn it. :-)

JavaScript

The Google V8 JavaScript interpreter is open source and has a just-in-time compiler for Intel machines. This is probably the best known of the languages I could pick, given the number of casual web programmers out there. Code looks like this:

var i;
for (i = 0; i < 5; i++)
{
  document.write("The number is " + i);
  document.write("<br/>");
}

I have a certain amount of prejudice against this language. I implemented a compiler for it as part of a project back in 1997. This was before the formal spec was available, and the language was a bit different under each web browser. I got very frustrated by the sloppiness of most of the implementations. I think at one point I had a test case that crashed every browser I tried! I also think the language itself is a bit loose.

Since this is a JIT interpreter (compiles to machine language), I guess I would be stuck if I tried to port the framework to the iPad, but I'm not sure Apple is allowing interpreters inside apps anyway.

Smalltalk

The last time I used Smalltalk was in 1992, at a company doing pen-based computers. There's an open source implementation called Squeak. One advantage is that I think Smalltalk systems normally implement an IDE in Smalltalk as part of the language. So I would get "in-world" development tools, including a debugger, with the language. Code looks like this:

n := 1.
[ n < 1000 ] whileTrue: [ n := n*2 ].

I liked the language for the generality of it, and the small number of concepts. Although it was intended to be easy to learn, I'm not sure that it really is. Some of the block and class concepts can get a bit subtle. Perhaps it's just coming from a C++ background that made it all feel strange.

Although the language is very well known to computer science students, I'm not sure many casual web programmers have even heard of it, let alone know how to write it.

Java

Does anyone know what the situation is with an open source Java implementation? Google pulls up some projects, but also comments about Oracle suing Google over Android. Apparently there are patents, but I'm not sure what they cover (language, or some VM internals?) Code looks like this:

int sum = 0;
for (int i = 0; i < 5; i++)
{
  sum += i; 
}

If you include all the standard class libraries, Java would make an excessively huge scripting language. I'm also not sure how hard it would be to integrate into the rest of my framework. I need to call from C++ into Java, then back out to the framework. Calling the compiler and a debugger from in-world also seems like a challenge.

Python

Python is another popular programming language that I don't know. Code looks like:

def fib(n):    # write Fibonacci series up to n
    """Print a Fibonacci series up to n."""
    a, b = 0, 1
    while a < n:
        print(a, end=' ')
        a, b = b, a+b
    print()

Personally, I hate using indent level to define blocks. It looks really unnatural to me after 40 years of languages with block delimiters. I wouldn't let that stop me if this was my choice of language though.

More of a problem is the language itself. I'm a minimalist when it comes to languages, and paging through the tutorials, Python seems huge. There are a lot of different concepts, a lot of syntax, and a huge standard library. I'm not thrilled.

My Own Java

On various projects in the past, I've written compilers/interpreters for Pascal, JavaScript and Java. I can't really justify designing and implementing a "real" programming language for this project, even though I'd like to. :-)

However, I have my own implementation of Java, written as part of an earlier attempt at this project in 2005. It doesn't handle 100% of the syntax (and there have been changes to Java since then), almost no standard classes are implemented, and it's slow, but it would do as a scripting language.

The language is well documented and many programmers know it. Plus the expression syntax of Java is almost identical to both C++ and JavaScript. On the other hand, a restricted implementation of Java that doesn't have most of the class library might just be annoying to Java programmers.

I mostly want scripts for simple things like event handlers on creatures ("something is near you, so attack it.") For some of the languages above, I'd have to write (or at least integrate) some kind of editor and debugger into the world anyway. Working with my own code could save me a lot of time.

A Poll

As I see it now, Lua or my own Java will be the easiest to integrate into the framework. V8 JavaScript might not be too bad, and I have no idea about Python. Integrating a real Java into the framework looks hard, but I don't even know which codebase I'd be using for a real Java VM. If someone knows of a Java being used as a scripting language, let me know.

Both real Java and Python strike me as bigger pieces of code than I really want to deal with at this point, but I could still be convinced. Smalltalk is the only choice that will give me an in-game development environment out of the box. All the other choices require me to hook in debuggers and my own class editors, etc.

Here's a poll. Scripting is not something I will get to anytime soon, but I'd like to know what you think. Choose all languages you think are acceptable. Please leave comments if you want to argue for your choice, or know answers to any of my questions!

Home       About Me       Contact       Downloads       Part 51    Part 53   

blog comments powered by Disqus