Monday, April 19, 2010

Forking your game


Having used a scripting language with native fiber support (GameMonkey) with some custom modifications to provide in-language syntax for this, I found myself wanting a similar feature when making games in C#.

As an example, somecode would start out something like this:


// another enemy-based example
self.Shoot()


until someone requests 'can you just make it wait 1 second and then turn and shoot again?', or perhaps 3 times, or 4 times and then wait 2 seconds and explode. This sort of change without fibers will add a big mess of new members, tracking state, timers and sleep values, as so forth. After which may just end up being thrown away since it didn't 'feel' right.

Having native language support for fibers lets us do something like this:


fork {
  sleep(1);
  self.Shoot();
  sleep(1);
  self.Rotate( PI );
  self.Shoot();
  sleep(1);
  self.Explode();
}


and we can test it right away. The benefit here is that the code change is very very local and doesn't really impact any other behaviour of our entity.

There is no native C# support for this sort of fork'ing code, but with the yield statement and IEnumerable, we can implement something clever to make this work, which we will cover in the next blog post! :)

No comments:

Post a Comment