Archive for the ‘Java’ Category

Java or Flash for game programming: which one?

Monday, April 26th, 2010

This question comes up a lot.

No, a lot!

The problem is, it’s not important.

Not important?! you exclaim. Then why do people ask it all the time?

Ok, actually this is a variation on another question: “What programming language should I use to make my game?” Once you’ve made some games, you come to know that it doesn’t matter. Any language will do. What does matter is the end product! Is it fun? Does it run on enough machines to meet your goals? Does it do what you wanted it to do? That’s all that matters.

Technology is a tool, so use whatever language you’d like.

As true as that is, it’s not always true.

The one way that choosing a language does matter is in the context of what you want to accomplish. Is your game downloadable? Or is it meant to be played in a browser? Those are big considerations, and they will have a material effect on what language (and tool set) you choose.

Narrowing things to Flash versus Java (where I have the most experience if you don’t count 10 years of C++), though, the answer is still, “It depends.”

Java and Flash both do downloadable and browser deployment, so let’s assume those factors are equal and are, thus, non-factors.

Do you want to reach the broadest audience or are you more interested in fast rendering and processing?

We all want fast rendering, but I mean does your project need lots of rendering muscle to succeed? Not just, “Oh it looks so cool.” but “Oh, this is not playable!” It’s got to really matter to the end product (because, ultimately, the end product is all that matters).

Java can render using hardware and is way faster than ActionScript, so it will have more power than Flash. A great browser library for Java is PulpCore. The feel of Flash with the speed of Java (although, it doesn’t do hardware rendering that I know of–but it is very close to a Flash experience without Flash, if that suits your persuasion).

Flash will ensure that your users have a more hassle-free experience since it’s installed on 99% of computers, whereas Java has quite a few old versions that are common. Relying on hardware rendering can give rise to technical incompatibilities, though Java doesn’t have to use hardware, so that can be mitigated.

Java is getting better about updates and penetration, but Flash still wins when you want to be compatible with the most computers (especially older or quirky hardware). That’s not to mention Flash is more web-enabled (ie, has strong support for integration with many sites and game portal APIs like Kongregate and Facebook), so for online games it makes lots of sense.

So, again, your answer has to derive from the question of how it affects the end product and what your goals are for it.

I’m doing online games, so I don’t want players to have to download and install anything. That’s a huge concern to me, so Flash wins almost right away because it’s the most hassle free. My second concern is penetration, and again–for my goals–Flash wins. I design for lower quality visuals but it’s made up for by the fact that I can reach more potential players. For commercial reasons, reaching more players is more important than having the best rendering sizzle.

For my backend (a.k.a. game servers), I use Java. Java is plenty fast, works on the platform I care about (Linux for servers), and was easy to learn with my backlog of C++ experience.

To summarize: the language you use is only relevant in context of the goals of your end product.

If you’re a hobbyist and don’t have any real goals, pick anything you like. If you have commercial goals, pick something that directly supports those goals.

It’s very project-specific, and it might even change from one project to the next.

Whatever the case, just make sure you’re having fun and don’t get all serious and uptight about languages and tools. It’s about the games!

When instanceof doesn’t recognize an instanceof.

Thursday, August 13th, 2009

Java’s ‘instanceof’ keyword is not the same as ActionScript’s ‘is’ keyword. In theory, they do the same thing: find out if an instance of an object implements a class or interface. In reality, they’re quite different.

Java compares for an exact match. ActionScript compares for a match or any subclass of the class being compared.

Let’s say we have class A which implements interface I. And we have class B which is a subclass of A (and implicitly implements interface I).

Java:

if( A instanceof I ) // True
    print( "yeah" );

if( B instanceof A ) // True
    print( "yeah" );

if( B instanceof I ) // False!
    print( "yeah" );

ActionScript:

if( A is I ) // True
    print( "yeah" );

if( B is A ) // True
    print( "yeah" );

if( B is I ) // True!
    print( "yeah" );

So, having learned a lot of ActionScript before diving into Java, I expected ‘instanceof’ to behave just like ‘is’. But no. And that was annoying not to realize at first. So, you’ve been warned!!

Update: Hmm, now I wonder if overloading method parameters behaves the same way as instanceof in Java? I’m going to be safe and make an interface for any classes that have multiple subclasses (ie, categories of entities which require special behaviors). It’s still polymorphic, so I guess it’s “good design.” I haven’t had quite enough experience digging into the reasons for Java’s language design choices.

Update 2: Of course I had to test the behavior of overloaded methods. Turns out that Java isn’t as exacting with overloaded methods–which is a relief!

For example, I have an Entity class, and a subclass called ItemEntity. I need to do some special handling on an ItemEntity (or any subclass of it) in a method that takes Entity as a parameter. Overloading that method to take an ItemEntity gives me the desired behavior for ItemEntity or any subclass of ItemEntity. Yay!

When ActionScript talks to Java using bytes

Saturday, August 8th, 2009

I’ve just finished building my client/server communication loop for Spirits of Gaia. The client is Flash (Flex), and the server is Java (built on Project Darkstar).

Darkstar uses a binary message format, which is a different approach from something like SmartFox, which uses strings (“raw”, XML, or JSON). I had experience with SmartFox, but I hadn’t tried to implement a binary format in Flash before. It really wasn’t bad in the end, and most of my time was spent just learning about ByteBuffers and ByteArrays in ridiculous amounts of detail.

And–of course–one of my bugs probably induced more research than necessary as I attributed malfunctions to various other possible causes. The bug was that I inherited one of the command processing classes from a base class which didn’t implement what I thought it did, so it was silently failing. :roll: But I learned a lot. A whole fricken lot! And that’s enough learning for anybody.

So one issue is that you can’t be sure what the endian-ness is for the server or the client. Well, maybe you can, but before now I didn’t know enough about it to make any assumptions. Darkstar uses ByteBuffers to hold incoming messages, and the default for Java is big endian for ByteBuffer. Thus, I use big endian on the server.

I had a feeling that Flash would take care of it for me, but of course I wanted to make sure it was using big endian, too. For a while, though, I didn’t know how to make a ByteArray use big endian or little endian.

hexadecimal My first approach was to get around it entirely by encoding all values that require more than 1 byte into hex strings. What a joyful aside that was! (Sarcasm, you see.) I mean, now I know how to encode anything to hex and back and do it sideways, too. But, this wasn’t working so well and it was bloating the nice binary format anyway. (No, I wasn’t really concerned about the size of the packets based on that, but if there’s a better way and it’s more space efficient–why not?)

Then I dug a bit deeper and realized that you can tell a ByteArray to use big endian like this:

var buffer:ByteArray = new ByteArray();
buffer.endian = Endian.BIG_ENDIAN;

Hard, huh? :) So then I didn’t need to encode multi-byte values into fun hex strings anymore (with one exception). So my PacketBuffer class got a bit more efficient in speed and size, and all is well now.

The one exception is for Java long values, which are 8 bytes. Flash (as far as I know) has no way to pull an 8-byte value from a ByteArray. And, no, I don’t want to do it by hand–which doesn’t mean I won’t! If encoding to hex string doesn’t work out, then I’ll write some code to stitch 8 bytes into a long value and stuff that into a Number (since it won’t fit into a 4-byte int). But, honestly, that kind of stuff is not my cup of tea because it’s not fun to me. But if it’s gotta be done, I’m a professional, so I’ll buckle down and do it. Clearly, I want to whine about it a lot just in case I do really need to do it.

For completeness, you tell a Java ByteBuffer to use big endian like this:

final ByteBuffer buffer = ByteBuffer.allocate( someSize );
buffer.order( ByteOrder.BIG_ENDIAN );

I think it’s ok to assume that Java (and Darkstar) will still default to big endian on whatever OS my future game server will be running (a flavor of Linux, no doubt). If I run into some bizarre bug where command ids are getting scrambled, I’ll know the first place to look.

Learning resources for Darkstar newbies

Friday, June 5th, 2009

I’ve been intensely researching Project Darkstar Server (PDS). Along the way, I thought I would collect some of the documentation, forum posts, and tutorials that have helped me understand how to get started. I’ve tried to avoid outdated stuff and only post things relevant to the latest Darkstar (0.9.9 0.9.10 0.9.11). Over time, I will add more as my own knowledge and experience deepens.

So, I present to you A collection of valuable links and resources for finding your way through the Darkstar code thicket.

Getting Started

You’ll need to understand this before you even get started.

Server

Example Clients

Darkstar Basics

Understanding Managed Objects

Understanding Tasks

Services and Managers

Protocols and Transports

Known Issues (Big Ones)

Vital Tools

  • Gamalocus profiler and viewerreally useful, and the graphical viewer makes interpreting the data much easier. It hasn’t been updated in a while, so I hope the project owner keeps it up to date or it can be given a new maintainer.
  • Asynchronous task library – if you need to do anything outside of Darkstar (like access a database), you will probably want to use this to work outside the Darkstar task time limitation. (I wish this was on Google code. The Java.net site is really horrible [and slow for me] by comparison.)
  • MockSGS – a unit testing library for Darkstar.

Better than SmartFox? Researching Project Darkstar Server.

Thursday, May 28th, 2009

I haven’t reached any conclusions yet, but I am really excited about Project Darkstar Server.

Project Darkstar is software infrastructure that aims to simplify the development and operation of massively scalable online games, virtual worlds, and social networking applications. Originally created by Sun Microsystems, it is today advanced as an open source project through the Project Darkstar Community.

I knew about it a few years ago, but it didn’t seem complete enough to bother with. But then I saw that CampFu had launched using it on their games backend, and that pushed me over the fence into wanting to investigate because it has to be pretty robust and usable to support a commercial product like that.

campfu I like SmartFox Server just fine. I think for my needs (indie games on small to medium scale), it would work really well. But two big motivations for looking at Darkstar are that 1) Darkstar seems like it provides more MMO functionality from the start, and 2) Darkstar is free and open source.

I had written a hefty chunk of server code for SmartFox already, and had plans for more. That’s really the product’s weakness for doing something with a persistent world type experience. SmartFox is very bare bones, but that’s not a bad thing if you go in knowing it’s not MMO-in-a-box. Well, I don’t think Darkstar is, either, but it appears to have more than SmartFox that is specifically for virtual worlds. So that is what really excites me about it: maybe I can get to building game logic sooner!

I’ve only done a day of reading about it, and I’ve downloaded and browsed through the 0.9.9 distribution. So far it looks really cool, and I like that the developers want to make it seem like a single-threaded program, because I’m not looking forward to debugging thread race conditions and all that crap. Another big plus for Darkstar, in my book.

If things pan out, I am going to be releasing a multiplayer Flash game. Some folks were balking at the idea of using Unity for Lila Dreams, but I still think that’s the way I’m going for that project. But it doesn’t mean I’m done with Flash, because I have a closet full of game concepts that I’d love to be able to create eventually. PushButton Engine is still very much on my radar. And, as I’ve said elsewhere, I choose the technology based on the needs and goals of a project and its parameters. I’m not a fanboi of any particular technology, platform, programming language, etc. I just use what makes the most sense on a case by case basis.

Maybe Darkstar will become my server technology of choice. I need more time to dig deeper and find out what it’s really like to use it on something that is non-trivial. Does it really provide a lot of virtual world infrastructure as compared to SmartFox? Is it viable for indies? I guess you can find the answers when I post again on this topic in the near future! :)

Unity3d *wipes drool from chin*

Thursday, December 18th, 2008

I keep lusting over this amazing looking tool. You can bet that I will be getting my grubby paws on it eventually. There’s simply too much potential there!

unity3d-paradise Unity3d is one of the many reasons I wonder why big companies are still making boxed retail games at all. For that matter, I often wonder why small indie developers are making downloadable games at all! I will never make another non-browser game in my life, unless there’s a huge incentive or it’s an optional download version. It just doesn’t make sense anymore to put up a barrier to entry like forcing a download and install.

There are two things I don’t like about Unity: 1) the development tools run on Macs only, and 2) I can’t use Java even though the tools use Mono (which supports Java).

First point, I have nothing against Macs (heck, I own one). It’s just not my development platform of choice. I have a bunch of nice tools already that I’m comfy with on my Windows machines. I don’t wanna have to build all that up again. That said, Unity holds enough potential that I would.

Second point, I don’t dislike C#. It’s a lot like Java, maybe even better in some ways. The reason I don’t want to move away from Java is because that’s what all the server code is written in, and it would be really nice to only write the classes etc for one language! Again, though, I think Unity has enough potential that I would bite the bullet and do it anyway.

Oh, point three: it costs $1,500 bucks. Ouch. I know there’s a cheaper version, but it’s too crippled for me–you can’t even publish to Windows! So, really there’s only one price point. Like the other two problems, I think I can overcome this one because I am so enchanted by what I could do with this engine. Lila Dreams in 3d? You never know…. :twisted:

Well, I was just distracted for a few minutes (again) by the tropical demo, so I wanted to get this off my chest.

Update: Sweet goomba! I just learned that Unity 2.5 will be the first Windows release for the authoring tools! *weeps with joy* But also the indie license will allow publishing to Windows now. And the indie version costs $200. *faints* Discovering Unity has blown my mind. It solves so many problems. I must have it!! :)