News for hackers. Stuff that matters.
Re:Game Programming Patterns – Bytecode by anon4 on Tuesday March 25, 2014 @02:34PM (#7467814)
In what situation would "just embed python/lua/js/etc." not be the appropriate solution to this problem?
Re:Game Programming Patterns – Bytecode by ANTSANTS on Tuesday March 25, 2014 @05:06PM (#7468965)
You saw them a lot in old games (the kind likely to be written in assembly), because not even C, let alone a general-purpose scripting language, was acceptable for performance reasons for quite some time, yet there has always been a need for greater abstraction and expressive power. Look at any Japanese RPG of the 80s and 90s, for instance, and there's probably a simple bytecode VM (or state machine, if you prefer) for scripts that move characters around during story sequences, one for displaying text in menus and dialogs, and so on. Look in an arcade shooter, and there's probably one for defining enemy movement patterns, etc. Tiny bytecode DSLs not only boosted expressive power for programmers, they also allowed non-programmers to script behavior. 6502 assembly might be out of reach for non-programmers, but `SETANIM PLAYER1 PLAYER_WALK_TICK; WAIT 20; SETANIM PLAYER1 PLAYER_WALK_TOCK; WAIT 20; LOOP;` is not. If you only have to remember a few simple rules to get the behavior you want, even learning to hand-assemble bytecode is not out of the question for a dedicated professional.

Nowadays, you'd probably be better served by embedding a general-purpose dynamic language in most of these scenarios, but hand-rolled bytecode can still win in certain situations that need high performance and/or low latency. Imagine, for instance, one of those fancy "danmaku" shooters with hundreds or even thousands of bullets on screen moving in intricate patterns. You just might want a language more dynamically manipulable than C to script the behavior of those bullets, but using a dynamic language that furiously generates garbage and demands collection pauses for something that must happen 60 frames per second on so many objects is probably going to cause hitches eventually that will get your player killed (in the game, at least). You'll then be subject to a very harsh but valid criticism: "I played games like this on my PlayStation back in the day with no slowdown, this guy must be a crappy programmer if his 2D game stutters on my monster PC." Sure, it's great that you don't need to break out the optimized assembly to do a Breakout clone anymore, but at the same time, you should feel bad if a 1Mhz 6502 provides a considerably more reliable experience than your game on a modern x86.

A certain popular 2D shooter series made by an ex-Taito arcade game programmer uses a bytecode DSL for its intricate bullet patterns for this reason. It was probably an easier decision to make when the series started in the late 90s, but today it lets the author be productive without adding wildly variable latency into the mix that would kill the gameplay. People should not forget that running smoothly even on poor hardware is a real feature. Even in 2014, I'm sure there are plenty of people out there still using Pentium 4s and Windows XP. If you make the only new games/apps/whatever that run well on these computers, you have a captive audience.

At the same time, I think you could extract 99% of the performance benefits of the bytecode approach if your language had a well-defined heapless, stackless "tasklet/microthread" subset that centered around mutating existing objects and making certain limited kinds of allocations (allocating from a memory pool and manually "freeing" by marking the object as unused later). Knowing that a piece of code will never generate garbage and that a garbage collection cycle will never happen during its execution makes a huge difference.

Re:Game Programming Patterns – Bytecode by anon4 on Tuesday March 25, 2014 @08:23PM (#7470502)
Thank you for your answer. I do realise why it was done in the past, I was wondering about the present. Also what you're describing in the last paragraph sounds a bit like Andy Gavin's GOOL - http://all-things-andy-gavin.com/2011/03/12/making-crash-ban...
Re:Game Programming Patterns – Bytecode by TheZenPsycho on Tuesday March 25, 2014 @06:10PM (#7469525)
"At the same time, I think you could extract 99% of the performance benefits of the bytecode approach if your language had a well-defined heapless, stackless "tasklet/microthread" subset that centered around mutating existing objects"

Oh hey, I think you just described asm.js

Re:Game Programming Patterns – Bytecode by munificent on Tuesday March 25, 2014 @05:23PM (#7469062)
> Even in 2014, I'm sure there are plenty of people out there still using Pentium 4s and Windows XP.

Don't forget low-end mobile devices.

Re:Game Programming Patterns – Bytecode by eropple on Tuesday March 25, 2014 @05:45PM (#7469274)
Low-end mobile devices these days have 80+MB of RAM available to an application and the downsampled assets that I assume you're using on them 'cause you're a pro leave you with a pretty large chunk of RAM to work with.

I sorta doubt there are many games targeting these devices (and, more particularly, the people who still use these devices in 2014) in the first place, but even fewer which are pushing their headroom so hard that, if they decide they need what amounts to a scripting language, they can't fit one in off-the-rack.

Re:Game Programming Patterns – Bytecode by TheZenPsycho on Tuesday March 25, 2014 @06:14PM (#7469566)
What's your market? Is it the U.S. or… India? if you put the population of each next to each other, which would you describe as "most people" ?
Re:Game Programming Patterns – Bytecode by eropple on Tuesday March 25, 2014 @07:24PM (#7470082)
If you can point me towards a smartphone, with significant market share and usage numbers in 2014, that doesn't have 128MB of RAM and, like, an 800MHz Adreno or something, I would really like to see it. Because that's my defnition of "low end" and I've literally never seen crappier than that. (I'm targeting phones with at least two cores and 768MB of RAM and up, because I'm lazy and I don't think anybody with lower would be buying my game because it doesn't fit the profile of titles that make money on such devices, but I do have a couple paperweights around for idle testing.)

My market research has shown that when you get below those specs you start getting into dumbphone/J2ME platforms, where you have other constraining factors.

Re:Game Programming Patterns – Bytecode by TheZenPsycho on Tuesday March 25, 2014 @07:39PM (#7470174)
it is not always about whether you can at all. Sometimes it's about power budget- That is, battery drain, etc. Run your lua engine and you have less budget for particle effects. Run all the things and your game drains the battery in 10 minutes.

It's perfectly legitimate to not care. Writing a from scratch bytecode vm is a fairly extreme last resort solution.

But on the other hand, the bytecode VM, appropriately scoped, could end up being easier than embedding lua. That is, if what you're doing is only barely more than loading and using data- not even quite crossing the boundary into turing completeness.

Re:Game Programming Patterns – Bytecode by eropple on Tuesday March 25, 2014 @08:18PM (#7470444)
The power delta between AngelScript or Lua and your own interpreter is gonna be pretty small, though. It's a cost you pay once (in a modern implementation) to interpret into...a bytecode VM. And you totally might be a wizard beyond my ken here, but I strongly doubt it'll be easier to implement a bytecode VM of your own than calling, like, five functions. =) I'm not denying that there are cases where such a thing is valuable, don't get me wrong, but don't reinvent what you don't have to. And probably should not be the first thing you're reaching for in your toolbox. Or second.
Re:Game Programming Patterns – Bytecode by TheZenPsycho on Tuesday March 25, 2014 @08:53PM (#7470698)
You're probably right and it's totally irrelevant in this day and age with cheap memory and cpu power.

I can't help but remember fondly on the idea of the bytecodeVM that "Out of this World" used to draw vector graphics, and think "That. is. neat".

or the power and flexibility the SCUMM vm afforded story writers.

It's fetishistic and unrealistic I suppose, to think it would be cool to do some amazing game on some extremely primitive slow low power cheap machine that anyone could buy for like $10. The whole fantasy usually ends when you realise that the cost of displays is well beyond the cost any actual computer hardware you may consider.

Re:Game Programming Patterns – Bytecode by x1024 on Tuesday March 25, 2014 @05:42PM (#7469236)
Eh, angry birds runs on Lua. Your point was?
Re:Game Programming Patterns – Bytecode by ANTSANTS on Tuesday March 25, 2014 @06:12PM (#7469545)
Angry Birds is not a terribly involved or demanding game, and the hard part, 2D physics simulation, is handled in C by the Box2D library anyway. It requires no real-time control, you just aim the bird and watch the reaction. You could half or quarter the framerate and add enormous interframe jitter and the game would play exactly the same. If you tried to do the same thing with an demanding twitch action game, it would quickly become unplayable.
Re:Game Programming Patterns – Bytecode by eropple on Tuesday March 25, 2014 @07:30PM (#7470115)
Lua's been fast enough to do what you describe for years on gear much slower than even bottom-end smartphones, but if that's too iffy for you, AngelScript is a sub-megabyte runtime when run on Android via the NDK. It's historically been performant enough for use on the PS2 (with its ballin' 300MHz MIPS CPU and 32MB of RAM). It's statically typed, has a very well-defined garbage collector you can pretty easily avoid ever invoking once, and as a simple transform language generates very little (possibly even no? I don't remember) heap allocations. It will also take literally ten minutes to set up for the sort of use case we're talking about here. (If you want to bind a big honking C++ library to it, then things get interesting, but we aren't talking about that.)

You still write your perf-sensitive code in C++, of course. Just like if you embed Lua. Because you aren't dumb.

munificent hit on the one use case where this makes a ton of sense to me--when you're a basically-embedded environment, he was talking about a platform with four megabytes of RAM--but that hasn't been a serious use case in games on popular platforms for a while now. Which is why I'm confused why it's in a book that seems to basically be for novices, at least without some "you should rethink doing this unless" asterisks.

Re:Game Programming Patterns – Bytecode by ANTSANTS on Tuesday March 25, 2014 @07:45PM (#7470214)
AngelScript seems relevant to my interests, I'll have to check it out.

I'm sure Lua is "good enough" for a lot of tasks in most games on most hardware, I was just trying to hint at scenarios where the GC overhead could be painful enough to warrant a custom approach.

>but that hasn't been a serious use case in games on popular platforms for a while now.

The DS with its 4 megabytes of RAM was still a relevant platform not that long ago. The 3DS doesn't have a whole lot of breathing room either; 128 megabytes of RAM, who knows how much of which is available to the game and not reserved for the OS.

>Which is why I'm confused why it's in a book that seems to basically be for novices, at least without some "you should rethink doing this unless" asterisks.

I haven't read too much of this book, but I got the impression that it was for programmers of other disciplines that wanted the skinny on game development practices, not for total novices. Somewhere between "Game Programming for Teens" and "Game Engine Architecture." This chapter doesn't seem that out of place to me.

Re:Game Programming Patterns – Bytecode by eropple on Tuesday March 25, 2014 @09:33PM (#7470899)
> 128 megabytes of RAM, who knows how much of which is available to the game and not reserved for the OS

The OS reserves 32MB for itself, which leaves you with a bigger working set than a bottom-shelf Android phone. =) I get what you're saying, but I don't think anybody's going back to the halcyon days of fewer megabytes than I've got fingers.

> I got the impression that it was for programmers of other disciplines

It felt like something I'd recommend to a CS student, tbh. Maybe that's just me.

Re:Game Programming Patterns – Bytecode by ANTSANTS on Tuesday March 25, 2014 @10:04PM (#7471067)
>The OS reserves 32MB for itself, which leaves you with a bigger working set than a bottom-shelf Android phone. =) I get what you're saying, but I don't think anybody's going back to the halcyon days of fewer megabytes than I've got fingers.

Ok, 96 megabytes. When you set aside RAM for everything else in a 3D game (the main binary, sound/mesh/texture caches, etc) there's not a whole lot of space left for a scripting language's heap, especially when you consider that most garbage collectors require that the heap be significantly larger than the working set in order to achieve reasonable performance. Again, not saying Lua wouldn't work just fine (any 3DS devs in the audience that would care to comment?), just that it's by no means impossible to bump against that limit. For comparison, here are the slides to a 2010 presentation that demonstrates how devs were having real performance problems with (PUC-Rio, not Mike Pall) Lua on the 360 and PS3:

http://www.slideshare.net/hughreynolds/optimizing-lua-for-co...

>It felt like something I'd recommend to a CS student, tbh. Maybe that's just me.

Well, I wouldn't be surprised if the average programmer is actually working below the level of what we expect a CS student to know /me ducks

Re:Game Programming Patterns – Bytecode by eropple on Tuesday March 25, 2014 @10:23PM (#7471147)
I think--not sure, I haven't tried--that I'd be a lot less worried about that RAM budget on that phone, actually. That phone almost certainly doesn't have the GPU for a game rolling with nontrivial models/textures. Like, just as an example, I don't think you're running even a downscaled, super-optimized, everything-in-C++ version of the Angry Bots demo that comes with Unity (it's on Google Play if you want to check it out) without stripping out actual functionality to the point where I think you sort of miss the point of the game because I think the GPU would give up the ghost. So I feel like on this low-end hardware you're basically talking about fairly lightweight 2D games because it can't push better, so I'm not sure I'd really worry about big honkin' meshes or textures. Is that unfair? (My own libgdx game doesn't use more than 60MB of textures at its peak, and I'm doing a lot more stuff in my JRPG than I'd think almost anybody would try to do on that sort of bottom-of-the-barrel hardware.)

Also, yeah, maybe I'm overshooting the average programmer.

Re:Game Programming Patterns – Bytecode by TheZenPsycho on Tuesday March 25, 2014 @06:11PM (#7469537)
I think the point is you don't generally need something with the extreme power and flexibility of lua to simulate comments like yours. A simple bytecode with 5 or 6 instructions should do. And it'll be performant enough to run on a casio calculator watch at full speed!
Re:Game Programming Patterns – Bytecode by eropple on Tuesday March 25, 2014 @03:41PM (#7468398)
Disappearing up your own backside to implement something like this is a time-honored indie game development tradition.

(I should know, I've certainly killed enough projects by doing it.)

Re:Game Programming Patterns – Bytecode by adamnemecek on Tuesday March 25, 2014 @03:40PM (#7468384)
If nothing else, if you want to learn. If there was an article about building a hobby OS would you respond

'''In what situation would "just use linux/freebsd/etc." not be the appropriate solution to this problem?'''?

Re:Game Programming Patterns – Bytecode by anonymoushn on Tuesday March 25, 2014 @03:43PM (#7468414)
If the article about building a hobby OS was on a blog about making actual useful software, and framed building an OS as a reasonable subproblem of making actual useful software, I would expect lots of people to reply like that.
Re:Game Programming Patterns – Bytecode by adamnemecek on Tuesday March 25, 2014 @03:50PM (#7468456)
The blog is specifically about patterns used in game development not about game development per se. So it's about the journey, not about the destination.
Re:Game Programming Patterns – Bytecode by namuol on Tuesday March 25, 2014 @03:49PM (#7468452)
But the site isn't "Programming Language Patterns" -- it's "Game Programming Patterns".
Re:Game Programming Patterns – Bytecode by adamnemecek on Tuesday March 25, 2014 @03:54PM (#7468483)
Correct and one of the patterns is embedding a language. Maybe lua/js does not fit your criteria for whatever reason so it might be a good idea to know how to roll your own language.
Re:Game Programming Patterns – Bytecode by sitkack on Tuesday March 25, 2014 @04:05PM (#7468564)
More importantly, that you understand how the underlying language runtimes work and their call/data structure models. Most of us will still embed an existing system.
Re:Game Programming Patterns – Bytecode by eropple on Tuesday March 25, 2014 @04:12PM (#7468616)
I am beyond very confident that the solution set containing embedding Python, JavaScript, AngelScript, and Lua covers just about everything in the problem set a game programmer reading that book is going to encounter.

And if it doesn't, it's not going to be improved by rolling your own.

Re:Game Programming Patterns – Bytecode by munificent on Tuesday March 25, 2014 @04:53PM (#7468883)
Hi, I wrote that chapter. I also wrote the AI system for Hatsworth. There is absolutely no way we would have embedded Lua on a DS!

What we did do was write a tiny little bytecode VM as described in the chapter. The level editor I wrote[1] let designers author behavior using a little UI. It then compiled that to bytecode.

It worked like a charm if I may say so. Unlike a full-featured language VM, we didn't have to deal with strings, parsing, garbage collection, or any of that stuff at runtime, which would have been untenable on a DS.

[1] http://imgur.com/a/PIFUk

Re:Game Programming Patterns – Bytecode by eropple on Tuesday March 25, 2014 @05:41PM (#7469225)
That's really interesting, thank you for mentioning that. That's totally in my "just about", for sure--but this isn't 2008 (even the 3DS has 128MB of RAM) and this doesn't exactly strike me as the sort of book that somebody who already has a console devkit is going to be reading. "A bytecode interpreter" seems to me to be something that that guy (i.e., you) is going to have sussed out entirely, whereas somebody who actually needs a book like this is in just about every case going to be getting off in the weeds building something instead of going off the rack.

(That said, no-GC AngelScript uses under a megabyte of RAM on ARM; before I switched to libgdx and decided that I could do without supporting the Galaxy Nexus I'd spent a lot of time examining it. It certainly might be tight on the DS, but on any modern system, I think you're probably cool.)

Re:Game Programming Patterns – Bytecode by kelvin0 on Tuesday March 25, 2014 @05:18PM (#7469030)
As we say in Quebec: 'Din Dents...'
Re:Game Programming Patterns – Bytecode by xjh on Tuesday March 25, 2014 @04:48PM (#7468861)
Security. When you want to run untrusted modding code.

It's hard to sandbox python or lua and block I/O and all system calls. 1 mistake and player computers are compromised.

Even with JS in browser you can't just eval(), you need sandboxing like https://code.google.com/p/google-caja/ or https://github.com/jterrace/js.js/ (200 times slower than js).

Re:Game Programming Patterns – Bytecode by deluvas on Tuesday March 25, 2014 @03:16PM (#7468208)
Can confirm. This is by far the most ideal option available.
Re:Game Programming Patterns – Bytecode by kelvin0 on Tuesday March 25, 2014 @03:27PM (#7468298)
I used to work as a programmer in a game company shipping on consoles+PC. The first time we embedded a scripting language in our engine to be used by the level designer's they surprised the whole team with their new found 'toy'. This was great and lessened the burden on programmers. Fast forward to first milestone (everyone crunching) and the milestone was delayed ... The level designers (not being programmers) had introduced a new category of bugs. Compound that to that fact that this script language had a very very primitive debugger ... hell ...
Re:Game Programming Patterns – Bytecode by namuol on Tuesday March 25, 2014 @03:52PM (#7468474)
I get the feeling that "crunching" is to blame for the bugs a lot more than the engine's newly-integrated scripting language.
Re:Game Programming Patterns – Bytecode by kelvin0 on Tuesday March 25, 2014 @05:15PM (#7469018)
I didn't imply the scripting engine was to blame. It's unchecked use and the lack of tools for debugging it were mostly to blame ... of course crunching amplified all these problems
Re:Game Programming Patterns – Bytecode by sitkack on Tuesday March 25, 2014 @04:03PM (#7468547)
The affordances of debugging are almost more important than the language.