partiallydisassembled.net

Watched constants, in Python

2009-03-28 19:50:24

Games need lots of magic constant value to set creature spawn rates, movement speeds, acceleration, rates of fire, random chance, etc etc. Finding values for all these numbers that work well is usually a lot of guesswork and good luck. And, unless you have some very sophisticated value-tweaking engine built into your game, a lot of recompilation and restarting as well. I stumbled upon this neat trick on Molly Rocket for automatically updating hard-coded constants in source code as the game is running. Here's my quick hacked-up version for Python: def TWEAK(initial_value): import traceback import re # Name of this function stack = traceback.extract_stack() func_name = stack[-1][2] # Pattern to locate the new value pattern = re.compile('.*[^a-zA-Z0-9]%s\(([^\)]+)\).*' % func_name) # Filename and line number where this function was called filename, lineno = stack[-2][:2] # See what that line looks like at the moment try: line = list(open(filename))[lineno - 1] match = pattern.match(line) return eval(match.group(1)) except: return initial_value Import this into your code, then whenever you have some constant you're not sure of, instead of writing: self.x += 0.5 use: self.x += TWEAK(0.5) Then run your game. The change is reflected in the game as it runs, as soon as you save the source file. As is it won't work for values that are used to intialise, say, a particle emitter; it can probably be improved with some clever use of __getattribute__ and small changes to the way you write your code like that. Another improvement that wouldn't be impossible is supporting multiple TWEAK values on one line.

Richard writes:

I guess it's a little more programmer-friendly than using a configuration file, but it gives me the willies :)

The takeaway

2009-02-20 10:26:21

When did this become the hip new word for "conclusion"? It irks me.

Anathem

2008-10-11 20:36:51

"Our opponent is an alien starship packed with atomic bombs," I said. "We have a protractor."

Alan Green writes:

Ooh, ooh. I know. We wait until they reach out to press the "fire" button, then we stab them in the hand, right?

Jesse Archer writes:

Perhaps there's another angle we could approach this problem from.

Apple C header licenses

2008-10-08 23:19:24

Seen in the prelude of the Kernel.framework/Versions/A/Headers/IOKit/hid/*.h files... "[...] APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. [...]" Not fit for quiet enjoyment? Oh, my.

What slows down development...

2008-08-16 17:00:22

... Vista bluescreening every time I terminate a hung Python process that has a lock ("Process has locked pages"). Yep, I can trigger it consistently and reliably. Makes.. me.. angry!
login