Wednesday, December 5, 2012

Baby's First Script

I added very basic scripting capabilities! Most of the work here was getting Lua integrated and setting up a data model for saving and executing scripts.

First, set up a tile with some sort of collision model. For now, the engine uses the same collision model to block the player and to detect collision for script execution. This might need to change--what if I want the player to be able to walk over a tile, but have the tile still execute a script?


Okay, as you can see, I've added a new flyout menu for managing scripts. If there's none there already, 'New' is the only option--if scripts exist, there's options for 'Edit' and 'Delete'. Let's create a new script...


Here's the Script Editor form. Pretty basic for now. Pick the function you want to define--I only have two right now, one that's triggered when the player collides with the model ('playerCollide') and one when the player does damage ('playerDamage') to the model. I'm going to choose playerCollide...


...and here's where I write my very first script. As you can guess, this script claims to move the player 80 units right and 30 units down.


Okay, I saved the section and ran the game. It works!


So, there's a very simple script integrated into the game. From here, it'll be easier to add more powerful scripts that act on a variety of game objects. Some examples of the first few things I'll add to the interface to implement the 'player slashes his sword at a shrub'

  • Change the texture of any of the layers of a tile or entity
  • Change the collision model of a tile or entity (you can freely walk over a shrub that's been slashed)
More advanced scripts for the same scenario might involve something like:
  • Spawn several debris entities (leaves)
  • Run scripts on those leaf entities to make them move in a certain way
  • Destroy the leaf entities after they hit the ground
  • Play a sound (I haven't set sound up yet, though)
  • Spawn an object or enemy that was hidden in the shrub.

No comments:

Post a Comment