Wednesday, April 24, 2013

How To Make Stuff

So, if you've been following my progress, you might remember something like this from long ago:

My first script

What's depicted here is me editing the behavior of an object--I think it was a bush--directly in the level editor. Pretend for a moment that it was actually a useful script, like describing how the bush spawns a bunch of leaves and then disappears when the player slashes it.

Obviously an inline script editor is not going to suffice if there's hundreds or thousands of these bushes in the game, since I'd have to copy the script everywhere. And then if I needed to make changes to the script, I'd need to update the copy everywhere. This is similar the 'don't repeat yourself' philosophy of programming: common behaviors should be abstracted to one place.

When I moved away from Lua and into full C# (see this blog post), I needed a way to bind entities in the level editor to behaviors in C#. Just to get things up and running so I could experiment with the model, I implemented a quick hack which looked something like this:

Just a hack to get things going, not a feasible long-term strategy.

So, basically, I was attaching behavior to entities based on the texture of the entity, and in one case, parsing the tags of the entity to differentiate between behaviors. Terrible! But this was just temporary. So over the past couple days I've implemented it the correct way.

Now, Behavior Templates can be picked directly in the level editor! That means we can write a template in the Implementation assembly and it will instantly be available in the level editor:

What's even better, I can add custom properties to the template in code...

...and have them show up as editable fields (with possible default values--use the checkbox to override) in the editor:

So, now I have a pretty good system for designing reusable behavioral entities and them placing them in levels via the level editor. 

I put together a short video of the process for Adding New Stuff, from start to finish. In the video, I take a sample graphic of a ball and create a custom behavior for it with a few properties--which can then freely be used on any level in the game--in just a few minutes. Check it out! Also, make sure annotations are on and you'll probably need HD/fullscreen to see the code.

That's all for now. Back to screen communication and making those hearts react to the player taking damage!

No comments:

Post a Comment