Testing flash embedding

Just testing some flash embedding. Ignore the generic platformer. Or.. don't.


Posted in Daily Update | Comments Off

Back To Camp!


Look at all that action! There is now a goal (but no way to win or lose). As you can see, the monsters are stealing the food from the camp and carrying it off. If they get back to their dimensional portal (the black smudge on the lower left - it looks a lot more interesting in motion), they zoom away with your food and you are sad. Lose all 5 food items and you lose the battle.

Now you can also see an arrow pointing at the selected Beefy. That's because it means something to select one - You can select one just to check out its health like any tower defense, but the selected enemy also becomes the focus of any tower that's in range to hit it, rather than them automatically aiming at the one closest to the campfire as is normal. Just to spice it up and make it an important bit of gameplay, I might add an actual damage bonus against the selected enemy (and replace the arrow with a skull to make it serious business). To be fair, I'm about 80% sure I've seen this feature in a game I've played, so it's not original. But if I'm wrong, then it is super original! Wow!

Anyway, I can't say I was super productive today, but the good news is that Amazinrandi, my dwarf priest (so named because he looks like James Randi), turned 85 today! Now I have 4 85s, and one more guy within half a level of it. So yeah, a worthwhile day. Good thing I also coded something, or I wouldn't have had anything to blog at you!
Comments Off

The Future Is Disorienting

I just jumped years into the future with both my cell phone and my computer. The future is a mixed bag.

The Cell Phone

For quite a few years now, I’ve had a flip-phone and was paying for service out of contract. When people started getting smartphones, I kind of wanted one, but never enough to justify the cost of a phone or tying myself to a new 2-year contract to get a free one.

Share photos on twitter with Twitpic

I did the math recently. I found out that for what I pay just for voice, data, and texting would be equivalent to getting a new smartphone with a contract. I’d even be able to take advantage of the data plan since the one with my flip-phone which became nearly unusable when they stopped supporting some of the already-poorly supported video options. I went with my fiancee’s Sprint-based provider instead of AT&T, and that referral netted me a discount that made it even cheaper.

It's here! And freezing! on Twitpic

So now I have an Android, the HTC EVO Shift, and I’m fairly happy with it. It’s way more useful than my old phone.

Plus, now I can play the games everyone has been talking about.

Plus plus, I have a device to test my own projects when I start mobile development.

However, I’m still getting used to figuring out how to hold it since I seem to keep accidentally hitting the search button or some part of the screen. I still hit the volume control on the side too often, and I press the power button as I slide the device into my pocket if I’m not careful. And as great as having a keyboard is, the phone automatically unlocks when I slide it out, which has accidentally happened in my coat pocket more than once already. While my old phone let me easily look up a name in my phone book since I could type a letter and it would jump to that section of my phone book, this new device seems to think I want to include everyone I’ve ever emailed, Tweeted, circled on G+, or friended on Facebook when I want to search for someone to call.

But this is the future, and the benefits are outweighing the negatives. Being able to fit a computer more powerful and integrated than my first desktop into my pocket has so far been pretty amazing.

The Computer

And speaking of computers, I’ve mentioned my dying laptop a few weeks ago. My Dell Precision M90 was a fine machine until the video card started failing. I’ve purchased a replacement: the Precision M6600.

Share photos on twitter with Twitpic

I’m going from:

  • 32-bit 2GHz CoreDuo -> 64-bit 2.2GHz i7 Quad Core
  • 2GB RAM -> 8GB RAM
  • NVIDIA Quadro FX 2500M (512MB) -> Quadro 3000M (2GB)
  • 100GB HD -> 750GB HD
  • 17″ WUXGA display -> 17.3″ 1920×1080 display

That last item unfortunately feels like a downgrade to me. I really liked having a 1920×1200 display. The extra lines of resolution were great for coding work. Unfortunately, it seems that 1920×1200 will no longer be available for most laptops. There’s no real explanation, either. There’s merely speculation that the hardware manufacturers will save money, especially since most people probably just want to watch movies on their laptops anyway.

Still, this new laptop came with Windows 7, and while I’ve used Windows Vista by being the default tech support guy for my family, I hadn’t really become familiar with anything since Windows XP, which is what my old laptop came with. The new interface is somewhat jarring. When I was running Firefox and had the download window open as well as the main browser, I wanted to minimize it, and so I clicked on the button in the taskbar, like I normally would. Instead of minimizing Firefox, it popped up a the windows as buttons so I could choose which one I wanted. Darn new-fangled tech doing things differently than I expected…

But no matter! I repartitioned the drive so that Windows gets about 100GB, and I installed Ubuntu 11.10 on the remaining space. And it turns out that Ubuntu is all new and shiny, too.

See, I used Ubuntu 10.04 Long-Term Support (LTS), and I didn’t want to upgrade to newer versions because I wanted a stable development system to do my work on.

So here I am, jumping two years’ worth of Ubuntu development, and this new Unity interface threw me for a bit until I realized they were trying to make it seem more Mac-like. There’s a launcher, and the buttons are all big and shiny, and there’s a Software Center where I can easily click and download apps and even purchase them. There’s quite a few indie games there, and it seems like it could be a decent marketplace.

But after looking at the entire games collection, I took the time to find the Terminal (more on that later) and typed: “apt-get install nethack-console” as fast as I could.

And then I added the Terminal to the launcher.

Seriously, it’s nice that the UI is 3D-powered and all, but sometimes I don’t want to mess around with a frickin’ mouse. I LIKED being able to open the system menu at a keypress and see ALL of my installed applications. Now I have to explicitly type to search or click on Dash, then click on “More Apps”, and then click some more? On the old system, I could hit Alt+F1 to open the “Applications” menu, then use the arrow keys to move to “Accessories” > Terminal. Now, I press the Windows key to bring up Dash, and if I know the name of the application, I can type it out right there, but if I don’t, it’s faster to click on “More Apps”, then go to “Installed” and click on “see all”, then scroll down until I find it.

I also liked being able to figure out how to open a file within an application by just looking at the screen. Unity has Mac-like context menus that applications share at the top of the screen, which…ok, whatever. Maybe it’s great, and I’ll try it, but why Unity hides the menu until you mouse over it, I don’t know, but I’m not the only one who is unhappy with it. Maybe I just need to get used to it, but there were a couple of times when I opened an application and wondered why the heck I had no way to open a file since the window I was using (seriously, why would I want to shift my eyes all the way to the top of the screen when the application I’m using is over here?) didn’t have a File option.

Unity also seems to hate the idea of customization. At least Windows let me move the taskbar around the screen. Why does the launcher have to stay stuck to the left side? Why do my windows have to have the close and minimize buttons to the left like on a Mac when I prefer them on the right? I’ve yet to find a way to change anything to work the way I want it to work.

I’m aware that it is possible to replace Unity with Ubuntu Classic UI or KDE, but I’m going to try to stick things out just to see how most users are expected to run their new Ubuntu-based systems. I actually do like being able to see all running application instances in an Exposé-like way, and there are parts of the UI that feel more intuitive. And Ubuntu 11.10 does seamlessly run 32-bit and 64-bit applications. I ran Stop That Hero! just to see if I had all of my development libraries installed, and I didn’t even realize that it shouldn’t have worked on this new 64-bit machine. So that was a pleasant surprise.

But if this is the future of computers, where laptop screens get smaller resolutions instead of larger ones, where the UI is made out of candy and shiny at the expense of being useful for people who know what they are doing, I’m not sure I’m happy about it.

I feel like the future is pushing me out of the way to make room for people who just want to watch widescreen movies or use a “workstation” as nothing more than a consumer electronics device.

Am I being unreasonable? Am I just disoriented because everything is different in the future? Is it better and I just need to get used to it?

The Future Is Disorienting is a post from: GBGames - Thoughts on Indie Game Development

Posted in Geek / Technical | Comments Off

How To Make Games

Uh oh, over 5 journal comments! Yes, it is a tower defense game (not reverse!). I love tower defense!

I got this email yesterday, and I thought I would answer it here, since it's kind of a FAQ of sorts, but a little more broad than that. The gist of it was "I want to be a game designer, so can you tell me how to do that, or talk about how you got where you are?"

The first issue is what it means to be a "game designer". That's a very specific job in the game industry, and you, yes you, aren't going to get to hold that job unless you work for yourself. There are actually a lot of different jobs that are called "game designer", but most of them are very menial and, while they're creative, you are creative over only some small part of the game, and it's not what people think of when they say "I'm designing a game". For example, you might be writing scripts that control how a switch opens a door. It's like programming, but simpler. Or you might be designing levels, similar to what you do in the Dr. Lunatic editor, except that the basic idea and layout of the level is dictated by a senior designer ("This is the snow level, where you snipe at guys and then have to stealth down to the castle and climb up the side of it").

What people really want to be is a senior designer (that is not necessarily the title at every company, but it gets the point across). That's a job that requires lots of experience and proven results to show you're capable of it. That's the job where you get to come up with the game and dictate how it all comes out while peons crank it out for you. ...Except it's not. There is still a whole layer above you that is really in charge. The company president and/or owner meet with marketing guys and decide what kind of game they want, and they turn to you to make their vague concept into a fleshed out design, which you then turn to programmers and artists to make into an actual game. Just another cog in the machine, albeit one with a lot of latitude, hopefully. The boss I worked under at a game company didn't exactly spare the freedom... he liked to be in charge, which meant dictating a lot of details, and I don't think that's rare. After all, he's the boss, it makes sense that he wants to be in charge!

So, what you really want (I'm projecting here, but I think it's statistically likely) is to be the company owner. And how do you get to be one of those? You either A> work for yourself, or B> be a famous pitcher with millions of dollars and pay for an army of employees out of your pocket (Ooh, I played the Kingdoms of Amalur: Reckoning demo, and it was cool!). Since you probably are interested in this stuff because it's what you want to spend your time doing, you shouldn't go the baseball route, and you should work for yourself.

And that's the bottom line. If you want creative freedom, if you want to make the games that you have ideas for, there's only one realistic path: do it yourself. But the good news is that this path is easy! Anybody can do it, you just have to want to do it and put in the effort. The tools are all freely available, and you've got the spare time, so start making games! That's what I did, that's what every other indie you've ever heard of did, and that's what you should do. Just make games.

Now if you want to know how to just make games, that's a much longer discussion that I'm not qualified for. But I can recommend Flash Game Dojo as a way to get started in flash games, which is a nice quick-feedback way to go. You will definitely start out very confused, no matter what tools you start with, but you have to keep hacking at it, making stupid little tiny things, and gradually you'll pick up the good stuff. Back when I started this, it was at least 150,000 times harder than it is today. The tools you have today are unbelievably simplified and accessible, not to mention free and surrounded by a community of users who will help you. Do some googling, you'll be buried in more options to pursue than you have time in your life to try. Back in my day, we had to use paper books to learn things. You'll pick this up no problem if it's really so fascinating to you that you want to spend your spare time fiddling with it.
Comments Off

Guessing Game!


A screenshot of what I'm currently working on. Your challenge for today is to guess what on earth this game is. Obviously you can't know what it's called (I actually haven't decided that yet anyway), or what the plot is, but what kind of game is it? How does it play? What is going on there? Mysterious!! Actually, this is probably gonna be easy, but let's see what people say.

In other news, I was reading back through really old journal entries today, and I decided I need to blog more! So there's a second challenge for you: From now on (at least for the rest of this month, I may forget this rule later, but hey, you can remind me!), if a journal entry gets comments from 5 different people, I will journal again the next day! I think that's a fun challenge, because it means I get feedback, and it means you get to know what's going on, and it encourages me to do something I should do anyway. See if you can keep me journaling nonstop for the rest of my life!

Rule: Those 5 comments have to be present by the middle-ish of the next day. If you post something akin to "first!" or something otherwise useless or offensive, I will of course delete it like I always do, which means it won't be present! So post constructively.

P.S. Witch Game? is still very much on my mind and I'm really looking forward to getting back to it once I finish up the project pictured above.
Comments Off

An Online Conference You Can Attend #AltDevConf

If you’re not familiar with AltDevBlogADay, you should be. Each day, a game developer posts on a variety of game development topics. There’s a huge backlog of content there now, and while the recent redesign has made it difficult to find the category you want (you have to click on a post to see only some of the tags available as of this writing), it’s great getting regular, up-to-date, state-of-the-art tips and tricks from the people in the trenches. Authors can be mainstream game programmers, indie developers, academics, or anyone who has something valuable to share.

AltDevConf

It seems to be such a successful site that they’ve decided to host an online conference. AltDevConf will be held on February 11th and 12th (that’s this coming weekend), featuring three tracks: education, programming, and design & production.

Our goal is twofold: To provide free access to a comprehensive selection of game development topics taught by leading industry experts, and to create a space where bright and innovative voices can also be heard. We are able to do this, because as an online conference we are not subject to the same logistic and economic constrains imposed by the traditional conference model.

As it doesn’t look like I’ll be attending GDC this year (I’m still hoping to win an All Access Pass with my GDC magnets), AltDevConf seems like a high-quality substitute. While it won’t be the same as rubbing elbows with other indies or meeting cool celebrities in the gaming world, I’m excited about it.

Do you plan to attend? Will you be speaking?

An Online Conference You Can Attend #AltDevConf is a post from: GBGames - Thoughts on Indie Game Development

Posted in game design, Game Development, Geek / Technical, Marketing/Business, Personal Development | Comments Off

Big Things for TF2 in 2012

Woah, woah, hold everything.

The team behind Valve’s online shooter Team Fortress 2 has posted a most interesting update on the TF2 blog, including some fascinating details about their plans for 2012. This includes a secret TF2 project, the new Steam Workshop Blog, and the Second Annual Saxxy Awards. Those are all well and good, but I think we’re most interested in the final “Meet the Team” short, the ever-forthcoming video documenting the Pyro’s (maybe?) dark and (probably) storied past. All the speculating fans will finally find answers to some of the questions surrounding the flame-throwing maniac. Is it a woman in the suit? Is she secretly the announcer? Was his or her flamethrower handed down by his or her ancestor GLaDOS from another universe through a inter-dimensional portal opened by none other than Gordon Freeman’s great uncle? But then, will Valve even reveal any of these details? We’ll have to wait for the video to find out.

On second thought, with Valve time in the mix, who knows when we’ll see any of this stuff. Oh, well. If you’ve got predictions about the Pyro or the secret project, or anything else about this post, leave a comment.


Posted in games, Links, Meet the Team, Team Fortress 2, Valve | Comments Off

How to create and play IVF / VP8 / WebM / libvpx video in OpenGL

Disclaimer: this tutorial covers how to render IVF / VP8 / libvpx video in an OpenGL libSDL / SDL window. IVF video only includes video, not audio. For game developers, it’s trivial to play audio via their own audio system. So you’ll have two files per movie “movie.ivf” and “movie.ogg” or whatever. As an exercise to the reader, you could easily jam both into a single file if you really wanted to.

The Problem We’re Trying To Solve

So you’re an indie game developer and you want to show a clip of video in your commercial cross-platform (PC/Mac/Linux/other?) game! Obviously you want a patent-free open source unrestricted license to do it.

Wait, can’t I go commercial?

Better than that, you could just use the built-in codecs on a platform! I’d suggest this if you are targeting a single platform, iPhone / iOS for example.

Otherwise, you’ll be using Bink, a commercial solution at $8500 / platform. I emailed about their “indie licenses” and never heard back.

The Open Source Options I didn’t like much

Here’s what we have for patent free open source codecs .. and their various problems.

Xiph Theora – Probably the best known codec. To get it working you have to have libogg, libvorbis, and libtheora all built for your target platforms. To me, that seemed like a lot to ask. Also, the libtheora API is a MONSTER. playtheora is a SDL example (similar to this one) that covers some of that ugliness, so I’d recommend checking that out if you want to use theora.

Dirac / Schroedinger – the BBC funded codec. I couldn’t get this one to build. It doesn’t seem to be all that popular.

Motion Jpeg – This isn’t so much of a codec as an idea. Make your own movie file with a ton of .jpg’s in it. I tried this. The files get really huge really fast. I wouldn’t recommend this.

Motion JPEG 2000 – This implementation was also pretty confusing. I couldn’t find where to start. And, yeah, this isn’t all that popular either.

libvpx .. why I chose it

WebM / libvpx – Backed by google this is a new contendor on the block. The thing that sold me was the sample encoder which was pretty simple. It also depends on nothing. Also, building it on OS X and Linux was trivial. Also they offer a pre-built Windows binary. Also, they just had their 1.0.0 release a few days ago.

So, yeah, having a supported, up and coming, easy to build codec was key to me.

How to encode for IVF / libvpx

Since it’s a new codec, not much supports it right now. I used a fresh build of ffmpeg under linux that I built with this configure command:

./configure --enable-encoders --enable-libvpx

Then I was able to use ffmpeg to encode ivf files pretty easily:

ffmpeg -i Untitled.mov -vcodec libvpx -b 1000k -s 1024x512 movie.ivf

Note: we’re not dealing with WebM files. WebM files are container files that also contain audio. Again, you’ll have to store your audio separately, or create your own container file, or figure out what WebM is on your own time.

So .. what’s the bottom line? Do we get any code?

Yes! I created a libSDL player that plays back the video at max speed possible and it converts the YUV data to RGB data and loads it as a texture. Here are the functions I provide:

void playvpx_init(Vpxdata *data, const char *_fname) ;

Just init your Vpxdata with a filename “movie.ivf” .. It’ll try and get libvpx up and running for you.

bool playvpx_loop(Vpxdata *data) ;

Call this once per frame to have it decode a frame of video. It will return false once it has run out of frames. If you want to mess with the libvpx YUV data yourself, it’s data->img. See the playvpx.cpp source or the libvpx example above to see what that structure provides. It’s pretty simple.

int playvpx_get_texture(Vpxdata *data) ;

Call this once per frame to have it convert the YUV data to RGB and upload the texture to OpenGL. It will return 0 on failure or a OpenGL texture ID on success. I convert on your CPU, so it’s not super fast, but it should work fine on modern computers. If anyone cares to provide a Shader version of this function, or provide a SIMD / MMX / SSE version .. well, that would be faster!

void playvpx_deinit(Vpxdata *data) ;

Call this function when you’re done to cleanup.

Conclusion and Source Code

Okay, here’s playvpx for you to check out. It’s a C-style API, but I’m sure I use some minor C++ in there. Probably wouldn’t be hard to make it C-only if you require C for your project.

Oh, and I include the libvpx binary for Windows, OS X, and Linux. So you may not have to build it for any platforms!

The code is licensed under the libvpx BSD-style license. My code here is a gutted version of their sample_decoder.c, so .. that seems to make most sense to me.

Posted in C++, development | Comments Off

My 2012 Quest: “12 in 12″

12 months

12 games

12 songs

12 chapters

12 blog posts

1200 pushups

12 steps

12 words

The year is twenty twelve, and I’m setting forth a personal challenge for myself. A way to break beyond my comfort level and strive for ultimate productivity. Success is not guaranteed, and I acknowledge that I’m reaching for the stars, but these are my new year’s resolutions:

1. To finish twelve (small) games. A few will likely be on mobile platforms, others in html5 and Flash. Pen and paper RPGs or boardgames count too.

2. To compose an entire album worth of new music (12 songs). I’ve written many songs in the past but it has been a while since I let the musical inspiration drive my daydreams. These songs will be released as (CC) freeware mp3s that will be available for download from my blog as well as several music social networks like soundcloud.

3. To publish at least two books, (twelve or more chapters worth) as ebooks (pdf, mobi, epub) or traditional paper books. The ancient wisdom, “write about what you know” will be my mantra, so the books will be about game development. These books will be available on stores like Amazon, Barnes and Noble, the Kindle and iBooks store and others.

4. To actively maintain my blog (www.mcfunkypants.com) even in light of the fact that I get much more response when I post the same content on social networks like Google+. The cloud cannot be relied upon for longevity so my website will be the permanent record. I hope to produce a few game development tutorials, game jam recaps, and other content that fits my areas of expertise.

5. To continue my quest to get into amazing shape. I hope to do at least 1200 pushups this year, though the number is derived from the other twelve-based choices and I hope to in fact do many more than that. I wonder if I can log 4800?

Last year at my yearly checkup my doctor told me that for the first time in my life I was overweight! I was shocked but my beer belly was a testament to the truth I’d been in denial of.

I’ve joined the fitness gamification social network www.fitocracy.com and have been using their operant conditioning tools on myself (you earn XP for exercising and slowly “levelup” yourself by completing challenges and quests in your workouts).

Great news: though it is only February, I’ve already gone back to NOT being overweight (I’ve recently lost 15 pounds by going for daily 3-6km hikes), which in itself was a great motivator to continue being active and eating well. I hope to never again be anything other than my ideal weight. Even more imporantly, getting into the habit of hiking has become psychologically beneficial as well: these moments of fresh air help me clear my head and I often come up with solutions to things I’d been having trouble coding earlier.

6. To teach my son to walk and talk. My most important quest for this year is to help my little boy learn to take 12 steps without any help (or furniture to hold on to) and to be able to say 12 different words (and understand their meaning).

That’s it! My quest has been set in stone.

I hope to use the public declaration of all these personal quests to hold myself accountable so that I do not give up on them. This is precisely why I waited until February to make New Year’s resolutions: those made on January 1st rarely persist.

The goals above are ones I’m dead serious about achieving: 12 months, 12 games, 12 songs, 12 chapters, 1200 pushups, 12 steps and 12 words.

Wish me luck!

Posted in 12in12 | Comments Off

My, myself and my God complex

Conditional statements as expressions

In my small review of Rust, I noticed one useful thing, that was the fact that the branches of if-else constructs could “return” a final value, which made the whole construct an expression, that could be assigned to a variable:

let var = if a == b {
    5
} else {
    6
};

I wrote it was particularly convenient associated with the fact that variables in Rust cannot be declared without initialization, which made the following Lua idiom impossible:

local var
if a == b then
    var = 5
else
    var = 6
end

And obviously, it makes for less typing, since there is no need to repeat the var = part.

However, I completely overlooked the fact that it was possible to do exactly the same in Lua, thanks to the lazy boolean operators:

var = a == b and 5 or 6

And see, it’s even far more concise. Even if there are many else if branches, it stays fairly short:

var =   cond1 and val1 or
        cond2 and val2 or
        cond3 and val3 or
        defaultval

Note however that you can have only one expression in the branch, while using Rust’s proper if thing gives you an actual block of statements, that can be ended in a single return value if you want. But well.

Lua’s logic

Reminder: in Lua, the or operator evaluates its first argument. If it is non-nil or true, it returns it. If it is false or nil, it evaluates the second argument and returns it. Of course, when using booleans, it’s exactly equivalent to the logical “or”. When using other stuff, it keeps the same logic but allows you to keep the values (not simply their boolean truth).

The and operator evaluates its first argument. It it’s nil or false, it returns this nil or false value. If it’s non-nil or true, it evaluates the second argument and returns it. Again, that’s exactly a logical “and” with booleans.

Let’s review the truth tables:

true or true = true
true or false = true
false or true = true
false or false = false

Therefore:

true or X = true
false or X = X

Hence the possibility of lazy evaluation. We need to evaluate the right hand side only when the left hand side is false.

true and true = true
true and false = false
false and true = false
false and false = false

Therfore:

true and X = X
false and X = false

Can you see the pretty symmetry? The “and” and “or” are perfect negatives of each other. These fundamental relations hold:

not(A and B) = not(A) or not(B)
not(A or B) = not(A) and not(B)

And these:

    not(true and X) = not(X)
==> not(true) or not(X) = not(X)
==> false or Y = Y

    not(false and X) = not(false)
==> not(false) or not(X) = true
==> true or Y = true

Logic is pretty. Math is pretty.

Anyway, some Lua users despise the overuse of and and or for other stuff than booleans, calling them unclear and difficult to read. I happen to like them very much.

The first classic use is the “default value”:

give_me(tea or coffee)

If there’s tea, I’d like some. Otherwise, coffee is just fine. When you say “I’ll have tea, or coffee”, you mean that both are OK, but you usually put your first choice first. That’s what happens in Lua too.

The second classic use is an equivalent of C’s ternary operator a ? b : c:

foo = cond and yes or no

If condition cond holds, give me the yes. Else, the no. It’s more difficult to find real language equivalents using “and” and “or”, but it quickly becomes a natural idiom in Lua.

take(raining and umbrella or sunhat)

“It’s raining and I take my umbrella, or then just my sun-hat.” I admit it’s less clear than the previous example, but once you’re used to it, it’s as readable as:

take(if raining then umbrella else sunhat end)

Or course if you don’t need a default value (and can deal with nil), you can leave it out, as a third (maybe less common) idiom:

take(raining and umbrella)

Finally, combine several of these and you get a multiple branch construct:

var =   cond1 and val1 or
        cond2 and val2 or
        defaultval

I had never done that in Lua, but it’s completely correct, and I like it. What about in sool? The boolean operators are going to work exactly the same, so it should be OK. The only missing thing is the operator precedence. In Lua, the and is applied before the or, and we have:

( (cond1 and val1) or (cond2 and val2) ) or defaultval

I recently re-established operator precedence to sool, and for the sake of this beautiful construct, and must have higher precedence. And or must be left-associative, which is what I decided for all operators.

Done.

Semantic digressions

Of course, all this supposes the existence of nil. And that makes a pretty sentence:

We suppose the existence of the nothing.

Riddle me that one! It’s easy to name things, but it takes one more neuron to name the absence of things. But even then, if you’re able to name it, talk about it, manipulate it in thought, isn’t it already something?

That’s one possible way to construct mathematics (or, well, at least the natural numbers). The only thing you know how to do it make sets of things. Start with an empty universe. There is nothing in it. Give a name to the set of all things that exist (which is none): the empty set.

∅ = {}

Then there’s one thing in your universe: the empty set. You can also call it 0. Consider the set that contains only it. And for fun, call it “1″.

1 = {∅}

And then

2 = 1 ∪ {1} = {0,1} = {∅,{∅}}
3 = 2 ∪ {2} = {0,1,2} = {∅,{∅},{∅,{∅}}}

The way to go from one set to the next one is called a successor function:

For any set n, S(n) = n ∪ {n}

And then you conclude, the set N of natural numbers is the smallest set containing 0 and closed by application of S.

And from this, define operations and whatnot, and build yourself a mathematical system. Fun hey? For more info, there’s always Wikipedia.

I love cosmogony, the study of the creation of the universe. Can really anything start existing if there was nothing before? And can you define “nothing” if there is indeed nothing to compare it too?

In a more fantasy-like setting, I love the power attributed to language and speech as a creation, or “actuation” tool, whether it’s from the Finnish Kalevala, where bards are wise men who engage in duels of words that slash like weapons, or the biblical creation myth that says

In the beginning was the Word, and the Word was with God, and the Word was God.

The French versions usually say “verb” instead of “word”, which implies action and gives it a certain twist. There’s also the Myst series of games in which writing books with the proper words creates actual worlds that can be visited, or Tolkien’s universe in which some ancient beings sang the world into existence (if I remember correctly).

In math terms, when there is a one to one bijection between one space and another one which has a similar structure, you can use one as if it actually was the other. If we simplify a bit, that would hold for the space of names and the space of objects or concepts they refer too.

Considering the name equal to the named: that’s what we often do in programming.

int i = 5;
Object *obj = new Object;

What’s i? It’s an int. No, it’s a variable that contains an int. One step further (notice the quotes): what’s “i“? It’s an int. No, it’s a variable that contains an int. Well, no, it’s really the name of a variable that contains an int.

And as I wrote before, it’s even worse with references. When I say obj is an object, I bypass several levels of existence: obj is a variable that contains the address of an object. And with quotes, it would be the name of that.

But mind you, there is exactly the same confusion happening in real languages, the confusion of the name and the named:

  • What is the name of this man?
  • This man’s name is John.

Now replace “this man” by another way to name him, his actual name:

  • What is the name of John?
  • John’s name is John.

And in that last sentence, “John” is both the person and the name. Confused yet?

In a way, we could try to solve the problem by writing it as such:

  • John’s name is “John”.

The need to put quotes is equivalent to using the address operator in C. Think about this:

  • The name of John is “John”.
  • The name of “John” is…

What’s the name of the name of John? John’s name probably doesn’t have a name, because John is a random guy. But if you take God instead, his name actually has a name: “tetragrammaton”.

Indeed, in Hebrew, Yahveh is written in four letters (semitic languages normally omit the vowels): YHWH. Hence, the name of god is “YHWH”, and since the name of god has four letters, it is itself called the “tetragrammaton” (“having four letters”, in Greek).

The quoting mechanism becomes a serious one in a language like LISP, where code and data are written the same. There is a quote operator that prevents the enclosed expression to be evaluated. Now try writing a LISP interpretor in LISP, using quotes and exec calls. Guarantied headache.

One last related issue in programming is naming strings. When I talk about the string “Hello”, does it include its double quotes? When I write about the string, should I say Hello or "Hello"? Or did I really mean ""Hello""? Is is clearer if I write <"Hello">? A programming language normally defines clearly how to write literal
strings and how to escape characters in them. But that is also a source of tremendous headaches, bugs, and security holes (PHP, JavaScript, anyone?)

Anyway, back to the creationist power of words. Complex architectures and silent beings live in the virtual world that is the computer’s memory, follow inflexible laws at the very base of their every behavior, evolve and reshape themselves as the processor ticks and does the bidding of a mighty being that conveyed it through language from outside the universe. I think the analogy is pretty clear.

I suppose all programmers have a little bit of a god complex.


Posted in sool | Comments Off

Linux Game Publishing CEO Steps Down

Yesterday, I woke up to an email saying that Michael Simms is stepping down after 10 years of running Linux Game Publishing and Tux Games.

Wow, 10 years! That’s a lot of enthusiasm and work, and unfortunately it takes its toll:

It took me some months to notice what was going on, and even longer to accept that my burnout was going to kill LGP unless I did something about it. The lack of drive slowed down production of new titles, shipping, customer service, everything that I either handled or had a big part in helping with, was all being compromised.

But I didn’t want to let the company die. Of course not, I have invested too much time, money, blood sweat and tears into LGP to just say ‘That is it, bye’. And so I sat down and had a long think about how to save it.

Clive Crous will take the reins at LGP and Tux Games. Good luck, Clive, and good luck with your future endeavors, Michael!

Linux Game Publishing CEO Steps Down is a post from: GBGames - Thoughts on Indie Game Development

Posted in Linux Game Development, Marketing/Business | Comments Off

Chaos Python

Add this into your functional tests and smoke it.
import sys, random
def chaos_trace(frame, event, arg):
if event == 'line' and random.random() < 0.000001:
raise MemoryError()
return chaos_trace
sys.settrace(chaos_trace)
You will get some lovely random failures injected into your code. A great way to find bugs, and make sure your reasoning is sound in the face of CHAOS!
Posted in python | Comments Off

Global Game Jam 2012 Pictures and More Video

Here are a couple of crowd shots I took which show just how many people were there.

Global Game Jam 2012 Photo 1

Global Game Jam 2012 Photo 3

I think it was fairly obvious that we weren’t going to win the children’s choice award.

Global Game Jam 2012 Photo 2

Finally here is some footage of people playing our game.

Posted in game dev | Comments Off

Is Asking Customers to Pre-order a Bad Thing?

A month ago, there was a post on Reddit asking what people thought about indie developers asking for money up front.

Some people are fine if they get a good quality playable build for pre-ordering, but no one seemed to be happy with the idea of funding basic engine development. It seems the general consensus is that people are getting tired of the so-called “fad” of funding a game before it is finished with no guarantee that they will see a payoff.

Minecraft‘s wild success through pre-orders aside, it’s not really a new funding tactic at all. Lots of indie developers have tried to ask for money before their games are finished, and some have seen more success than others.

The Indie Game Development Survival Guide by David Michael mentions how Samu Games started selling Artifact when it was in the beta testing stage, complete with perks for early customers. And this was in 1999.

Today, sites such as KickStarter and 8-bit Funding have enabled a number of high-profile projects to get funding from fans. Of course, a lot of projects don’t get funded and therefore don’t become high-profile.

So if you don’t have a big name to leverage like Notch or Andy Schatz or Derek Yu, are you doomed to obscurity?

No, but obviously an existing name brand helps. Otherwise, success at crowdfunding requires hard work to get your name out there. In other words, marketing. And you have to be able to demonstrate you can deliver the goods.

I started taking pre-orders for Stop That Hero! late last year, and I’ll admit feeling a bit anxious about it at the time. I didn’t have the game in a playable state yet, and here I was asking people for money in anticipation of the initial release.

While I didn’t get many pre-orders, it was definitely a nice feeling to see people actually spending some money on my game. It showed some interest, and it gave me a productivity boost to know I had existing customers to satisfy.

Now, when it comes to how I marketed the Stop That Hero! pre-order, I’m sure I did a lot of things wrong. Perhaps I should have had more videos of game play as I continued work. Maybe I should have been posting more screenshots. I could have chosen to prioritize work on certain features in the hopes that they would excite players more than the features I did work on. And maybe I wasn’t very assertive with asking for pre-orders in the first place.

At the time, I was struggling to get the alpha build across the finish line, but I kept getting good feedback from playtesters. Since the game was good enough to provide some enjoyment to players, it meant it was good enough to ask for players to pay for that enjoyment.

Now, of course some people weren’t happy with the idea of paying up front for a game they couldn’t see. And it’s hard to blame them. Since many game projects don’t get finished, it’s asking a lot to essentially gamble the cost of a pre-order on an unknown. Especially when indie developers don’t necessarily have the offsite backup solutions of larger studios when disaster strikes. See the Project Zomboid burglary for an example. All of their code was gone when someone stole two laptops, so it was a huge setback for the developers who had to rely on outdated backups to continue.

And it didn’t sit well with some of their customers, judging by the Reddit thread. It seems this experience turned some people off of pre-orders and paying for early builds in general.

All that said, it seems that making pre-orders work requires regular, quality content. Basically, if you stop talking to your customers and prospects, they’ll stop caring.

But if all you do is talk and never produce anything, no one is going to stick around. Whether you’re taking pre-orders or pledges, you have to be able to show that you can deliver results.

If you can do both, then pre-orders are worthwhile. Otherwise, you’re wasting your customers’ time as well as your own.

Is Asking Customers to Pre-order a Bad Thing? is a post from: GBGames - Thoughts on Indie Game Development

Posted in Game Development, Marketing/Business | Comments Off

The Humble Bundle for Android

and Linux and Mac and Windows.

Once again, the Humble Indie Bundle guys are back with The Humble Bundle for Android. While the title clues us in on the availability of the games, through the bundle, for Android, the bundle also includes versions of the game for Linux, Mac and Windows. The games in the bundle this time around are EDGE, Osmos, Anomaly: Warzone Earth, and if you pay more than the running average, the excellent World of Goo. As always, the bundle costs whatever you care to pay for it, is DRM-free, and supports charities (Child’s Play and Electronic Frontier Foundation).

Let us know what you think of the games and the bundle in the comments!


Posted in android, games, Humble Indie Bundle, Links | Comments Off

What Can’t Indies Do?

Well, a lot, it turns out. Let me open this discussion with a game review:

Batman: Arkham City

I got this free with my new video card, which is awesome, because I desperately wanted it ever since I played Arkham Asylum. I was going to say who it's by (Rocksteady), but there are layers of publishers and middlemen such that I don't actually know who it's by, which is certainly something indies need not worry about.

AMAZING game! I absolutely love it, and I've been playing it obsessively and dangerously for the past week, to the point of ignoring much that I really need to be doing. I'll make up excuses like "Well, I implemented that. I deserve a Batman break!" I've been annoying my wife as I constantly pop up behind the couch and around corners saying, "I'm Batman." (Michael Keaton voice, the most ludicrous of Batmen. Then again, I never saw Clooney, and I know he had to face Arnie)

So, if you click on that Arkham Asylum up there, you can see me gushing over that game. This game is, no question, more of the same. A lot lot more. I finished the main story mode several days ago, and since then I've been playing maybe 4 hours a day, and I'm still not done just collecting the hidden riddles and doing the side missions. I haven't even started New Game Plus mode or any of the Challenge Maps. I am now riskily going to launch Steam to display my total playtime. Let's hope I don't start playing. It says 27 hours played, which is less than I thought. The game calls my current state 63% complete, though for the sake of my productivity, I won't launch it to verify that. I think I've got so much to go that it'll be something like 100 hours if I actually try to complete all the challenges. And fail. Those fighting challenges are murder later on (in Arkham Asylum, anyway, and I expect the same here).

So... in short, the story's great (with a fun twist), it's never too hard (except the stupid AR training missions, which are very frustrating), the fighting is awesome (better than before, thanks to the ability to quickfire your gadgets in combat, yanking guys around with your batclaw, electrocuting them, dropping explosives while doing a backflip, etc), the stealth is awesome (better than before, thanks to more new and exciting tools, and enemies with more unique threats to you from thermal goggles to radar jammers). There is no Killer Croc fight this time, no boss is ever painful to experience, though some are pretty tough. There's a scene, in the museum, that's almost a parody of the Killer Croc fight, in that it's some of the same stuff, but completely trivialized. There are hundreds (400 I think?) of Riddler Trophies to collect, and almost every one has a little 'puzzle' involved in getting it. Usually something quite trivial, but the variety is amazing. Some test your gliding skills, some are for your brain, some require assorted tools, and some just rely on reflexes and timing.

There's also Catwoman, which is definitely a little change of pace from Batman. She has roughly the same abilities (only two very simple gadgets to Batman's 12ish), but she's a lot faster, a lot less capable in terms of travel, and a lot more limited in her stealth abilities. But she can get to places he can't, and has her own Riddler Trophies to collect. She also has a tiny bit of story of her own. Not much for sure, just a few scenes and a (very nasty) boss to fight, but they're fun to do, and you can even level her up a bit.

If there is a minus to Arkham City, besides the patently unfun AR Training, it lies in being a little too complicated, over what Arkham Asylum did. I definitely spend a lot more time wondering which gadget to use and looking at the situation instead of playing, and in fights sometimes my brain freezes up just thinking about the sheer number of choices because I can't think of which one applies to the specific moment I'm in. Then they punch me. Sometimes I just plain hit the wrong button because there are too many functions to keep track of. So it's kind of the classic, "If this game has any faults, it's that it's too much fun", only with an actual downside.

So that's an easy 5/5 Yerfdogs from me. It's so great that such an amazing new game series has appeared. They are rare and precious jewels!

What Can't Indies Do?

Now to my point. Batman is what indies can't do. Oh sure, there's the license, which obviously no indie will ever be able to afford, but take that aside. Indies can't make a game like this. The gameplay particulars could be copied - an open world in which you zoom around and punch guys or go into stealth situations against them, that is all possible and it's all been done (maybe not in one game though?) - but this implementation is light years beyond what the most dedicated independent developer could accomplish. A significant part of the thrill in playing this game comes from the things that cost literally millions of dollars and hundreds of man-years of work.

When you push the punch button as Batman, there are dozens of different things that can happen, and every one of them is an animation that somebody motion captured (or created, I don't know!). You might just punch the guy, knee him, elbow him, jumpkick, do a flying flip to elbow drop on somebody across the room, grab the guy's face and slam it into a wall, and just on and on through little variations, depending on your proximity to him, how far into a combo you are, what's in the environment around you, and other nearby enemies. And all of these things have been animated. I would guess that Batman alone has nearly a thousand animations in this game. The fact that the "break the enemy's weapon" animation is different for a lead pipe and a baseball bat and a gun (possibly even different for different guns?) and a sword is mind-boggling. He only bends the pipe, which I would expect to make it still quite useful, but nobody picks it up anymore.

What blows my mind in terms of animation is one very simple thing I keep noticing: there's a situation in which you can press a button to interrogate somebody (a feature I quite enjoy, much improved over how Arkham Asylum handled the equivalent scenario). The first few times I did it, there was a basic animation: he picks the guy up by his neck, threatens him, the guy talks and struggles, and then he elbows him unconscious. I thought that was it, until I interrogated a guy next to a wall, and discovered there's a different animation where he throws him against the wall. Then there was doing it near a ledge, and he dangled him over the ledge with one hand! Those may be the only three options, but I don't even know that. The point is, they could've easily done all this with one simple animation, the basic grab one. It would work near walls and ledges just fine, since you're still standing on the ground. But they didn't do that. They threw in more animations just for the joy of seeing it. Of course, all these interrogations also draw from a wide variety of random voicework too, different threats and responses.

And that voicework! There has to be hours of voicework (very top-notch voice acting too) in this game, from all the random conversations you overhear that hardly ever repeat to the massive amounts of talking between the different heroes and villains both in cutscenes and during gameplay. It's really something that I don't press a button to skip hearing what the villain has to say to me when I die. The first five times.

The art is great too, but whatever. Anybody can do good art (although I could rattle on about the sheer amount of it, as with everything else. Tiny TINY details everywhere, that vary so widely from place to place).

The point is, it's a blockbuster. Indies can make great games, games that are more fun than the big publishers make. But they can't make this kind of spectacle. It's not a matter of time and heart and love and sweat, it's a simple impossibility. It's the same as in movies, but in movies that's a no-brainer (indies can't afford to blow up a building), while in games I feel like, as the indie bar keeps rising, people think anybody can do anything. Well, we can't. Batman is something only a big-money publisher could create, or would. They could've made just as much money with 1/10 the work. Batman really doesn't need 20 different kinds of punches, nobody would've complained if the punch button always threw a normal punch. But thank heavens for the hubris of the big-money fat cats, because without it, Batman would not be the spectacle and wonder that it is. Those 20 kinds of punches add a subtle thrill to the game that means something, even if it's something minor, nowhere close to worth the 6 months of effort from a team of ten animators that went into it.

So what's the conclusion? I suppose it's that I'm glad the big publishers and overhyped AAA games exist, and that they don't comprehend the concept of effort vs. reward. I like indie experiences, but I also want to see The Matrix sometimes. Don't worry, big publishers, I will let you continue to exist. For now.
Comments Off

Video from Global Game Jam 2012

Just throwing up some video I took of the 2012 Global Game Jam at NYU. I caught about 20-30 seconds of most of the games. Sorry if I missed anyone’s.

Posted in game dev | Comments Off

Global Game Jam 2012

On Friday I travelled to Vienna to attend my sister’s “Sponsionsfeier” (ceremony where she was awarded her Master’s degree in biomedicine). I had heard of the GGJ earlier from Oncer in #Moosader so looked up the address and went there. And decided to register and take part.

I teamed up with someone who wanted to make a 3D snake clone. The result turned out quite interesting I think:

Posted in News | Comments Off

Some tweaking before the last straight line

Class and struct renaming

One thing has always bothered me while writing my compiler, it’s the
fact that the words “class” and “struct” have a very different status,
although they are used at the same level. Lemme clarify.

A class is a model of object. The model is the class, the particular
instance is an object.

A structure is both the model and the instance. I like this not. When I
do:

astruct = MyStruct

the model MyStruct is called a structure, and the content of that
particular variable astruct is also “a structure”. This is very
annoying, because when talking about the models, structures and classes
are very similar. In fact, they are even the same thing in my compiler,
except that one has a “structure” flag set. And I never know how to
refer to the set of both. “Class or structure”. “Composite” usually does
it. But everywhere in my code, I use “Class” for both.

Since structures are just “like classes, but value types”, saying
“class” for just the “by reference” classes seems like a bit of language
abuse.

When in the code we use “class” to introduce a class definition, and
“struct” to introduce a structure definition, we mix up the semantics a
little bit. Where “class” is quite clear (“what follows is a class, ie.
a model for an object”), “struct” is not (“what follows is a structure,
but not the one you mean, a model for it, but it still kind of is
called a structure anyway”).

Wiktionary defines class as:

A group, collection, category or set sharing characteristics or
attributes.

That applies to both: all structs of a same kind share attributes and
behavior, they form a class, just as much as objects that share
attributes and behaviors form a class.

In sool, an object lives on the heap: it has a unique identity (it’s
address), not only an internal state. It is an object as we mean it
in every day life (a mug, a phone, another phone). A structure on the
other hand has only its internal state, its value: where it is in
memory is irrelevant, and two separate structures with the same content
are indistinguishable.

So what about using the word “class” for what it means: model,
blueprint, and apply it to both kind of things we have: objects and
values.

Writing source code is writing object classes and value classes. And
since all we do is writing classes, we can omit the word, and put
directly the precision:

object Dog
    text name
end

value Vector
    num x
    num y
end

The fact that it emphasizes the natural behaviors is really interesting.
Values have no identity, just content, or essence: they can only be
copied around. A Vector is a value just as a number is a value. An
object, on the other hand, is an independent separate entity. You create
it, fill it with data, and it can interact with other objects. The word
“class” is polyvalent.

v = Vector(1,2)
d = Dog("Barky")

v is a value of class Vector. It is the vector (1,2). If another
vector has the value (1,2), they will be the same vector, just as we
say that 10 and 10 are the same number, or blue and blue are the same
color. On the other hand, d is an object of class Dog. It is a
dog, which happens to be named Barky. If another dog was named Barky,
they would still be different dogs. Just like two cars from the same
model, despite being exactly the same when they come out of the factory,
are really two different cars that can evolve in their own ways.

I like this. Not only it makes things clearer, but it brings a
bit of originality. “struct” and “class” as keywords are so C++.

Default values for argument

I was writing a draft of interface for the built-in types, and I noticed
I was already repeating myself a lot. Thanks to method overloading, we
can have different prototypes for the same method. A particular use of
this is when we want to omit some arguments:

method data copy() end
method data copy(blob newsize) end
method data copy(blob newsize, blob val) end

method text gsub(text pattern, text repl) end
method text gsub(text pattern, text repl, blob times) end

method text rep(blob times) end
method text rep(blob times, text sep) end

That already bothers me. In Lua, where everything is dynamic, you can
pass any number of arguments to any function, and if you don’t pass
enough, the rest is filled with nil. That way, you can have a sequence
of optional arguments at the end of the argument list, which makes very
nice shortcuts.

The current way to do that in sool is to manually define all prototypes
and do the calls with the default values inside. Thanks to inlining, the
final machine code will probably be the same, but the source code has a
lot more in it.

Lua allows this in a general manner, and uses the nil mechanism to
“catch” which arguments were not filled. C++ allows default values for
the arguments at the end of the list as well.

Since now we have a proper “optional type” equivalent for each normal
type, we could do something similar.

method data copy(blob? newsize, blob? val) end
method text rep(blob times, text? sep) end

Consecutive optional typed arguments at the end of the list can be
omitted when writing the call (and will get the nil value):

foo = somedata.copy         -- same as .copy(nil,nil)
foo = somedata.copy(200)    -- same as .copy(200,nil)
foo = somedata.copy(200,2)

megalol = "lol".rep(2)      -- same as "lol".rep(2,nil): "lollol"
megalol = "lol".rep(2,",")  -- "lol,lol"

Pretty sweet. But here comes the “however”.

However, that can result in an ambiguity:

-- method foo(num a)
-- method foo(num a, num? b)

obj.foo(10)     -- is it really foo(10) or foo(10,nil)?

Are there really cases where you’d need to be able to define both? Are
there really cases in which an optional-typed argument is not an
optional argument?

Some ambiguities are trickier:

-- method foo(num? lol) end
-- method foo(blob? lol) end

obj.foo()
obj.foo(nil)

Yikes. What nil is that? A nil num or a nil blob? A poor
implementation would simply pick the first one in the list that matches
the call. That would be random, that would be bad. I mentioned already
that classes may have a built-in selfless nilval method that would be
used only to type an expression:

obj.foo(num:nilval)     -- no more ambiguity
obj.foo(blob:nilval)

But the less stuff I build in, the happier I am. You can always do:

extend num
    selfless num? nilval()
        return nil
    end
end

if you really need it.

In any case, trying to solve ambiguities at the call is not completely
awesome. Defining conflicting methods in the first place is a bad idea.
If it’s possible to pass either a number, or a blob, or nothing, the
safer thing to do is:

method foo() end
method foo(num a) end
method foo(blob a) end

and be done with it. I have difficulties imagining cases where you’d
need to make the difference between an absence of number and an absence
of blob. When in doubt, just write them all down.

Unfortunately, that means a bit more work for the analyzer, which will
have to internally generate all prototypes corresponding to missing
arguments:

method foo(num n, num? m, text? t, data? d) end

-- becomes:
foo:num,num?,text?,data?
foo:num,num?,text?
foo:num,num?
foo:num

And all conflicts can be caught. C++ allows ambiguous interfaces to be
defined, and catches the conflict only at the call. That seems odd,
given that an ambiguous interface will always result in a ambiguous
call, except if all arguments are provided, which would make the whole
default argument thing pretty useless.

If the conflicts are checked at the interface, then every call is non
ambiguous and can be resolved to a single method.

It’s true, defining optional-typed arguments at the end of the list does
slightly pollute the method namespace, and may unintentionally bring
trouble. But like I wrote earlier, if two methods have the same name,
they really should do the same thing. If this system gives birth to a
conflict, then probably one of the methods does something slightly
different, and needs to be named something else.

Optionals, in any case, should be used wisely. They require a certain
amount of care in handling them.


Posted in sool | Comments Off

nil, again :(

What I found most special about Rust is that every variable is always
initialized, and no pointer can ever be NULL.

In C++ or sool, you allocate an object of a given type, and then fill
its members. In Rust, the only thing you can do is build an object by
specifying its members, and this determines its type. Then, if you
happened to have named this type earlier and defined methods for it, it
becomes the equivalent of an instance of that class. If you omitted to
give a value to a certain field, the field doesn’t exist and the record
is of another type, in which you cannot access this particular field.

Of course, in practice, that doesn’t prevent you to have shortcut
functions that construct the objects by filling in default values, so
it’s not necessarily less convenient.

What fascinates me is that is suppresses the need for NULL pointer checks
entirely. Remember, when discussing nil, I said it was impossible to do
without because we use it for:

  • variables not yet initialized: in Rust it simply is not possible to
    have a variable without initialization
  • empty a variable (and free its object) before it goes out of scope:
    now that I think about it, it’s garbage collected anyway, so you might
    as well wait the end of the block
  • optional arguments: in Rust you use enum types, like option<T>, and
    the alt control statement that branches depending on which subtype is
    that particular value. It is very clean, but requires one more control
    structure
  • invalid return values: that is covered by enum types as well

This is beautiful. Very logical, very neat and theoretical like a
functional language. Type constructors and type pattern matching. Wow.

Does that mean I want the same in sool? Grumph. No nil-checks sound
really good, implementation-wise. So far in my head, the way to deal
with nil is inserting a test before each method call (and hope for
compiler optimization), or inserting a test on self at the beginning
of each method’s body. Doable, trivial, but not awesome.

What would the “all members always initialized” mean in sool? For
primitive types and struct members, no issue there, I planned on having
default values anyway. The big question is for objects on the heap.

Like I mentioned before (probably when discussing the “single
ownership” thing), it’s OK the pre-allocate sub-objects all the way
down, as long as the whole thing has a tree structure. Obviously, when
there are loops, we’d have an infinite recursion.

Many pointers are not meant for ownership, but for shortcut reference,
like pointing to parents in a tree, or a network manager that could be
accessed from all over the program. A big program can look like a cloud
of objects, all talking to each other.

Having an automatic allocation in sool would also mean that we force the
distinction between “owned objects”, that can be allocated
automatically, and “simple shortcuts” to must be pointed at something
existing, at initialization.

Instead of an initializer method in which you can set some fields, and
leave the others be, it would mean that every pointer-like variable
has to be set. Either to a new object, or an existing one. No exception.

What about recursively defined data structures like linked lists or
trees? If we don’t go all the way of Rust and have enumerated types, we
need new tricks: the last node of a list could point at itself to mean
“no next one”.

What about returning nil to mean “is not in this collection”? Or “this
pattern did not match”? What about object members for which we simply
do not have the info yet? Like in my sool compiler, lots of fields of
the AST nodes are filled in by the analyzer, a lot later than the
creation of the node. Should I, for instance, define a child class that
adds the new fields, and replace the old object by the new one instead
of having lots of members hanging uninitialized so long?

Can members be optional, or only arguments and return values?

Oh, Rust, why do you bring my old questions back? I was never satisfied
by the nil solution I had, but now I’m afraid to go back to the
“nullable vs. non-nullable” type quest.

Why we really like nil

Compare

str = "yepee"
if str:find("blah") as where then
    -- use where
end

to:

std::string str("yepee")
size_t where = std.find("blah")
if(where != std::string::npos) {
    // use where
}

The principle is exactly the same: if we have a valid result to return,
we return it. If not (and that might happen, we don’t know), return some
kind of invalid value. What invalid value? -1? std::string::find
returns a integer of type size_t, which is unsigned, so it can’t be
-1. Actually, it is -1 casted to an unsigned, so it becomes the
highest possible integer of the native integer size. So, in this case
the guys who made the STL decided arbitrarily what was the invalid
value. Not only that breaks if you have a veeeeery big string (albeit
unlikely), but also, it’s so arbitrary that nobody can ever remember,
and the best practice is to compare it to some class member where it is
stored.

Now if you use an std::map, the find method will return an iterator
that points to the result of map::end(). It could be anything else.

In Lua, there is one standard normal way to say “invalid”: nil. And nil
happens to evaluate to false in condition statements, which gives very
natural looking idioms. That’s one of my favorite things in Lua, and I’m
not ready to give it up.

It works in the other direction as well. In my compiler, when I generate
a diagnostic, I must first give the severity and the message, but then I
can also add a source position, so that the report contains a reminder
of the source code, with the exact position highlighted. “Would you like
to highlight some source?” “Yes” “Which one?” “Here, have this SourcePos
object”. Without optional arguments that gives: “Would you like to
highlight some source?” “No thanks” “Which one?” “Err wut?”.

Sure, I could have two versions of the method, one with the SourcePos
argument, one without. But then what if I have instead two arguments of
the same type that I would like to be optional? If I make a method with
one less, how do I know which one was omitted?

For sure we can do without optional arguments, but they can also make
life so much easier.

Lastly, I think about the members. Same example, instead of printing
directly a diagnostic, I want to store it in a Diagnostic object, which
will be probably used for statistics and printing later. If I don’t have
a SourcePos to give, what do I do? Invent an invalid SourcePos that will
not be printed later? That means inventing for each and every kind of
objects a way to mean “this one is invalid”. Or we could have a unified
proper mechanism: nil.

Sure, I can think of ways to do without: have the Diagnostic class
contain no SourcePos, and a child class DiagnosticWithSourcePos contain
one, and by magic of dispatching, one would print its source and not the
other. Now suppose I have 10 similar optional members. Should I really
derive 2^10 children classes? I think not.

In my previous example of the Class AST node, in which we add a bunch of
information later, it might be indeed a cleaner design to have a derived
class, because in that case they’re not really optional, they are just
“not there yet”. But once there, they will most likely be all there,
and we can do without nil-checks.

“Optional” should really mean optional. Since it does require special
handling, it should be used only when really necessary.

For all the rest (non optional references), we can force initialization.
Period. That’s a good practice anyway. It will mean that you will have
to gather all the elements before grouping them together in an object.
But once an object is made, it is guarantied to be full.

If you use a custom init, the compiler checks that all members are
initialized. If you use the default init and the with member = val
end
construct, the same check goes there. No mercy.

Syntax

As I recall, I longly pondered about how to check for presence or
absence of an optional object, and how to cast an optional to a
non-optional.

It’s not that hard. We can decide that the only way to de-optionalize an
optional is conditional statements:

  • by naming a new local with the as in if and while constructs
  • by catching the value in a lazy or

No message can be sent to an optional, it has to be verified first, and
in case it does have a value, this value should be put in a new name.

-- opt is an optional Object

opt.dothing             -- ERROR

if opt as value then    -- value is of type Object
    value.dothing
end

value = opt or Object() -- if opt is non-nil, value gets it,
                        -- otherwise the second branch

This means the conditional statements can take either:

  • a boolean expression
  • an optional

Nothing else. In case of an optional, the variable in the “true” branch
will be casted to a non-optional. Sure, that requires using a new name,
but it makes sense. The fact that you can’t do anything with an optional
(except casting it into a non optional) mirrors the fact that it would
be unsafe to do so anyway!

nil, or optionals, are also a way to catch errors. And dealing with
errors is important enough that it may require additional typing. For
instance, typecasting may return nil, if the cast (which can serve as a
test as well) failed:

vehicule = Vehicule()
maybe_car = vehicule to Car     -- this returns an optional
if maybe_car as car then
    car.honk
end

Of course that can advantageously be combined into a single statement:

if vehicle to Car as car then
    -- car is a real Car
else
    -- well, vehicle is not a Car
end

Same for results that may be nil:

if mytext.match("pattern %w+") as thematch then
    thematch.print
end

It seems like a lot to type, but in Lua you would have to deal with
nil too anyway, otherwise you risk a runtime error of sending message to
nil:

local thematch = mytext:match("pattern")
thematch:whatever()     -- hey, forgot to deal with the nil case!
                        -- if it didn't match, runtime error!

The fact that sool forces to use optionals in the if construct and
name a new local for the result is just one step further than Lua where
you don’t have to, but any good code needs it anyway.

And, well, you don’t even have to use them right away, you can store
them in a temporary optional. But you will definitely need to rename
them once they’re casted into a non-optional:

opt1,opt2,opt3 = iReturnThreeOptionals()

if opt1 as a then
    a.yay
end

Finally, you can use optionals in boolean expressions even if you don’t
gather their value:

if opt1 and opt2 then
    "haha".print
    -- still can't use the values of opt1 and opt2, even if
    -- logically, we know they're both valid
end

So, yes, it might make some syntax a wee bit heavier, but in the grand
order of things, it’s for the greater good. Optionals and nil are
serious matters. In C/C++, dereferencing NULL makes the system shut you
down immediately and without mercy. In Lua, which runs on its own
virtual machine, it’s caught, but the sentence is the same: immediate
shutdown. And the fact is that since these errors happen at runtime, you
don’t see them happen until they do, which can depend on input data, and
is therefore more difficult to debug.

If we manage to design a system in which there is no such thing as NULL
pointers, or rather, where the validity is enforced at compile time by
the type system, well, maybe the programming work-flow will get improved.
Be forced to be tidy and organized from the start instead of just
throwing sloppy code and see if it works.

I don’t know, but it sounds like a good idea.

Declaration syntax

The C# syntax of using ? seems obvious. A question mark for something
unsure is definitely meaningful, and it’s one of the few tokens left.
Where to put it is another question:

class Meh
    -- after the type
    num? somenum
    -- after the name
    num somenum?

    -- for return values it doesn't change much
    method text?,blob,blob match(text pattern, blob? start) end
    -- but for arguments it does
    method text?,blob,blob match(text pattern, blob start?) end
end

One important thing to notice is that it qualifies the variables or the
expressions (which are equivalent internally to temporary variables),
not the values or objects. You can’t have an allocated object that
contains the information “I am actually nil”. The info is in the
variable.

Of course for value types, the distinction is less clear, since the
entire value is contained in the variable itself. But conceptually, the
optional status is part of the variable, not the value.

If also means that you can’t use optionals as template arguments. Can’t
have an “Array of optional numbers”. And that’s good, because having a
suffix ? in the type declaration would be syntactically annoying. If
you really need containers of optionals, you must box them into a struct
or object. Templates are instantiated using class names, not types.

Attaching the ? at the end of the type seems more logical. It mirrors
exactly the type signatures and method signatures (in which the argument
names disappear).

nil keyword

Finally, we get back the generic and unique keyword nil that works
with every type (provided it’s optional):

method foo(Object a, Object? b)
    a = nil     -- ERROR, "a" is not optional!
    b = nil     -- fine

    somebool = (b == nil)   -- ERROR, optionals answer to no method!
    somebool = (b is nil)   -- that could be allowed
    somebool = (b is a)     -- that shouldn't hurt either
end

Since the is operator is meant for testing the references, there is no
dereferencing, only comparison. In theory, the optionals should be
casted back to non-optionals, and only in the success branches could
their value be accessed (and compared). But it still would be safe and
much nicer to replace

method foo(Object obj, Object? opt)
    if opt as obj2 then
        if obj is obj2 then
            -- they are the same
        end
    end
end

by:

method foo(Object obj, Object? opt)
    if obj is opt then
        -- they are the same
        -- technically, we still can't do anything with opt,
        -- but we know obj is the same, so...
    end
end

That only implies that the compiler does the glue between the
representation of an optional reference and a real reference. Which
brings us to:

Implementation

Soooo, we’re back to adding a nil value to each variable type. A
consistent and simple way is to stick a boolean to each, and use it to
mark the variable as nil or not.

But of course, we know there are more efficient ways to deal with this.
As long as it’s hidden in implementation, anything goes. References can
obviously use the address 0 as a nil value. Double precision numbers
have a very big range of unused values (the NaNs), out of which we can
pick one to be nil (we just need to be sure that normal operations won’t
generate this particular NaN). Booleans are probably going to be bytes,
so plenty of room there. The only sensitive one is the blob (aka opaque,
aka unsigned int), in which every value is normally valid. For this one,
no choice, we have to either reduce its range by one bit (or one value),
or add a boolean to it.

This is mostly detail, except we’ll have to be careful with it when
writing the foreign function interface. The C API should define clear
functions or macros to produce or compare these values. And as always
with FFI, C functions should be kind enough to return the correct type,
even if there will be built-in dynamic type checking on the sool side:
basically every pointer that comes from C is assumed to be an opaque
blob, and it is dynamically type-casted back to the sool declared type.
You can always fool the system, but that should prevent accidental
mistakes at least.

Summary

  • all normal reference types must be initialized during init (either
    to a new object, or an existing one). There is no such thing as
    assigning nil. That way, we know that all pointers are always
    valid, and there is no need for built-int nil-checks.
  • all variables (and return values) can be declared “optional”. In
    addition of an actual value/object, we can assign nil to an
    optional.
  • no message can be sent to an optional, ever.
  • optionals cannot be assigned to non-optionals, ever (explicit
    assignment, argument in calls, or return values).
  • in conditional constructs (if, while, or, and), optionals evaluate
    to false if they are nil, and to true if they are not nil. In the
    true case, the resulting value can be caught by the as Name
    construct, or the result of the or operator, and will be of the
    equivalent non-optional type.
  • optionals of reference types can be used with the special is
    operator, to be compared between them, or with non-optionals, or
    with the nil literal.

Well, I’m fairly happy with that new nil system. Hopefully it won’t
come back to haunt me later.


Posted in sool | Comments Off