There is a very useful command within PuzzleScript called late. The order which events occur within your game matter, and sometimes to get the results you want, you need code that will run fashionably late. In Puzzledorf I did this very often. Every time you move:
- The game checks if you’ve finished moving
- Then it checks the positions of all the blocks to see if you won
- If you did win, it plays a big fireworks explosion as a reward
- After the fireworks have finished, you see a Complete message
So this is where late comes in. You don’t want code running all at once – there’s an order for things, and in Puzzledorf, I needed certain things to run after the level was won and after the fireworks had finished, etc.
This tutorial will teach you how to use the late command. It assumes no prior programming knowledge except the previous Puzzle Script tutorials I have written.
Why You Need It
Open this sample project. Now put the following code in your game and run it:
[ player | target ] -> [ player | ]
You might expect that as soon as the player stands next to a target, the target would be destroyed, however, this is not the case. The target instead disappears on the turn after you were standing next to it. Instead, try the following code:
late [ player | target ] -> [ player | ]
As soon as you move next to a target, it disappears. This is because anything designated as late will occur after all other code is run. There are times when you need this to happen.
Order Of Events
This is how code is run in Puzzle Script every time you move.
- Puzzle Script recognises that the player wants to move
- The rules are read and executed, if possible, from top to bottom
- The player moves, if possible
- Late rules are applied
Computers start at the top line of code and read down, line by line, checking every condition. So when you try to move, Puzzle Script reads all of your rules and checks to see if the conditions are true, and if they are, it does something. For example, the first line might be:
[ player | spikeTrap ] -> [ | spikeTrap ]
If the player isn’t standing next to a spike trap, the code continues on. This means that the order you write your lines of code matters. Some things you will need to use the late command for, and a lot of that will come with practice.
Practical Ways To Use Late
The best cases I have found to use the late command is when you want to check if objects are either on top of each other or next to each other, but there are other cases. If you check if one object is on another, it won’t be registered until the next turn, unless you use the late command:
[ player spikeTrap ] -> [ spikeTrap ]
In the above case, the player wouldn’t be killed by the spike trap until the turn after the player moved on to the spike trap. To have the player die instantly, just add the late command,
late [ player spikeTrap ] -> [ spikeTrap ]
To make the entire level restart when the player dies, you can do this:
late [ player spikeTrap ] -> restart
And the level would restart when the player walked on to a spike trap.