Scripting Cookbook: What You Need
You'll need to create a few things for your game-scripting system, at minimum: unique object names, event listeners, an embedded scripting language interpreter, and delayed events. Here's what you need in detail:
- It helps if every game object is derived from the same ancestor class. This makes it easier for objects to interact with each other because they share a common set of methods. We already have a common ancestor class in this tutorial with the GameObject class.
- You need to be able to reference each object by a unique ID-preferably, a human-readable name. That way, you can give commands such as, "When the player touches blueSwitch, open doorToGrandHall." You already have unique names with GameObject's getName() method, and you can define object names in your map files.
- You need to be able to listen for certain events and perform actions based on those events. That way, the listener can be notified when the player touches blueSwitch and can perform the necessary actions. You already have a simple notification architecture with the GameObject methods such as notifyObjectCollision(), but you'll need to expand on this a bit.
- You need touch and release notifications for when the player (or another object) first touches and finally releases (stops touching) another object. Currently, you have only collision notifications.
- Likewise, you need to be able to perform delayed actions, such as closing a door after a certain time period.
- Finally, you'll want to design the system to allow level designers to code easily and to be as creative and flexible as possible. In this chapter, you'll hook up your game to a scripting language. This solution will be simple and flexible and will allow for rapid development.
First, you'll upgrade the event notifications. Then you'll move on to scripting and delayed events.