What's It Good For Pt 2
The previous post discussed a few of the simpler tiles contained in the TilePlus Toolkit asset. There are several special tiles that provide much more complex functionality but require integration into whatever control code your project is using.
But before getting into that some background on the support library is necessary.
Behind the scenes there are several static classes, the most important are TpLib and TpLibEditor. TpLib keeps track of TilePlus tiles. TpLibEditor uses hooks into the editor to invisibly aid workflow.
When a TilePlus tile's StartUp method runs, the tile "registers" itself with TpLib. What this means is that information about the tile is cached in several data structures - here are a few:
- A dictionary of tilemaps to tile instances and their positions.
- A dictionary that maps TilePlus tile tags to tile instances.
- A dictionary that maps TilePlus tile Types to tile instances.
TpLib has nothing at all to do with maintaining instance data in tiles. There are no hidden data files, no modification of scene files; nothing like that. As a matter of fact, with a couple of minor mods to TilePlusBase, TpLib can be completely omitted - although operating in the Unity Editor environment would be much more confusing.
Some of data structures' names sound sort of strange. Tiles with tags? Yep. One great thing about tags (and this is plural, any number of tags is possible) is that you can search for a tile without knowing which tilemap it is on. Tags are inherently built into the tiles themselves and are saved with the scene like any other instance data.
Likewise, being able to look for tiles based on Types is remarkably useful in practice. The TpLib implementation also allows you to filter on interfaces, so lookup is very flexible.
TpLib also includes support for sending messages to tiles and for tiles posting events. Why would you want to do this? Here's a hypothetical example:
Let's say that you have a top-down game where the player moves one tilemap square at a time. After the user moves the player you need to see if anything needs to react to that position change.
The way that's done in the TilePlus system is to message tiles with the position information. This can be done with tags, that is, send position information to all tiles with a certain tag. It can also be done with Types, that is, send position information to all tiles with a certain Type. If you want to message a specific tile and you know its map and position you can do that too.
If the tile examines the position information and wants to create an action it can post an event using TpLib. For example, the position might match that of the tile itself. This is easy to detect as TilePlusBase implements a way to internally store its position and parent Tilemap.
Tiles can post two types of events: Triggers and SaveData. Triggers denote any arbitrary action you want to implement and SaveData events indicate that the tile has some data that you want to save; for example, the tile is a waypoint and it's time to create a save.
Events are cached so that your control code (e.g., some sort of GameController monobehaviour) can react. The control code can subscribe to an event so that it becomes notified when an event is posted. At an appropriate time the control code can dequeue the cache and take whatever action you want.
The provided demo TopDownDemo shows how to use messages and events for NPC control and saving the position of a Player tile in the filesystem.
With that background, let's get on to the more specialized tiles.