views:

188

answers:

3

Hey SO,

I know this is a pretty open question but I was wondering how people go about writing scripts that will "play" a game, or manipulate it in some way. I had been thinking that I'd try to get a working AI to play a game for fun, but don't even really know where to start. Are there any good resources to learn this? What are good languages to use? Once I have the language, how do I get my script hooked into the game? I was thinking of just trying simple flash games, if that helps.

Thanks a bunch!

+2  A: 

Are you looking to "play" an existing game, or write some sort of a game that can then be scripted?

If the former, you find that game's API and start writing code in the appropriate language. E.g. Unreal-powered games use UnrealScript and ship with the tools so you can embed new functionality. You're pretty much dependent on the game creators leaving an "in" such as this...

...unless you want to get totally nutty and hack the executable itself to execute your own code somehow. For information on how to do this, search for information on game "trainers". I'm not sure what a great set of search terms would be, but I know a lot of games do have trainers released, so type in "trainer <my_favorite_game>" and see what you get?

dash-tom-bang
I'm looking to "play" an existing game. It's made in flash.
Ethan
Then you're going to need to write software that can "view" the screen and send mouse events to the window hosting the flash application. There's no way to "plug in" to a flash app (and I think most are encrypted anyway?).
dash-tom-bang
+1  A: 

The best way to do that is to use the API provided by the game, if there is one. The Civilization games, for example, come with a very complete modding engine that allows you to script whatever you want. The same can be said for Age of Empires and a variety of other games.

Simple flash games, though? That'll be tough. Those don't generally provide any supported way to script, so you'll be left with simulating input. It'll be tough.

JSBangs
Supposing I wanted to try, what language would you suggest doing this with, and how could I try to go about getting input?
Ethan
+1  A: 

You can do this quite simply in Java 6.

  1. expose your objects/functions in your Java app into the scripting environment For example you can expose your character or the game world into the scripting environment and allow the script to control it.

  2. invoke script functions/methods from Java environment Allow the script to customize behaviours, add new actions, etc. Another use of this is to override the default build in game rules and load your own house rules. Another use is as a macro facility. Typically this would be part of the main game loop

  3. use the script as a data/level file For example, in a game likes SpaceHulk where it is mission based, you can use a script to define a game specific DSL, and us this DSL to define new missions. When the game starts up, it evaluates the data file and create a model of the game.

To integrate a scripting engine into Java, simply do this

   ScriptEngineManager mgr = new ScriptEngineManager();
   ScriptEngine jsEngine = mgr.getEngineByExtension("js");
   jsEngine.eval("print('Hello world!');

To expose an object into the script environment

   jsEngine.put("avatar", avatarObject);
   //An avatar object is now available in myscript.js
   jsEngine.eval(new FileReader("myscript.js");

To invoke a function in the script

   jsEngine.eval("function hello(name) ... ");
   Invocable inv = (Invocable)jsEngine;
   inv.invokeFunction("hello", "Fred");

One of the problem with scripting is that, the script has access to your entire application. You can restrict what the script can 'see' by setting a classloader when you create the ScriptEngineManager instance. Java 6 comes with a bundled JavaScript engine. However I prefer Groovy and loading Groovy basically mean changing 'js' in getEngineByExtension() to 'groovy'.

I know that a lot of people say that Java is not fast for games, but if you are writing turn based games or social games like Farmville then I think it is more than adequate.

For C/C++ based games, I think the preferred script engine is Lua. I've not used it so I cannot comment on it.

Chuk Lee
You win the check mark. What exactly do you mean by "expose your objects/functions into the scripting environment." How does one do that? Is there a guide somewhere?
Ethan
If you look at my example above with jsEngine.put, you will notice that I bind avatarObject to a name call 'avatar'. When we execute the script myscript.js now, there will be a predefined object call 'avatar'. See http://java.sun.com/javase/6/docs/technotes/guides/scripting/programmer_guide/index.html for a full description of what you can do.
Chuk Lee