This post is part of a series in learning how to make games with PuzzleScript. If you haven’t read my previous tutorials, I recommend you do so here, as that is assumed knowledge for this post.
Sometimes you might want to check what’s next to an object. You might want to check if a duck is sneaking up behind you, or you might want to create blocks that can only be pushed horizontally, but not vertically.
We can do this without too much difficulty. Let’s review the PuzzleScript code format:
[ Condition ] -> [ Event ]
[ Check for this ] -> then if it's true [ Then do this ]
To check the sides of an object, we just need to add one thing. The format looks like this:
CheckSide [ object1 checked | object2 beside ] -> [ object1 | object2 ]
Before the condition, we specify which side we want to check of our object.
Then inside the condition, assume you need at least 2 cells. A cell is basically any objects in the same space that are not next to each other.
[ This is one cell ] [ This is one cell | this is a second cell | this is a third cell ]
The first cell is the object whose sides we’re checking. The second cell is what object we’re checking for. Try the following examples:
Left [ Player | Crate ] -> [ Player | ]
The above code deletes crates if they are in the square to the left of the player.
Left [ Crate | Crate ] -> [ Player | Crate ]
The above code will check if a crate is to the left of another crate. If it is, the crate whose side was checked will become a new player object.
Left [ Crate | Crate ] -> [ Crate | Player ]
The above code has the same check, but the crate that is to the left will become a player object.
Left [ Crate | Crate | Crate ] -> [ Crate | Player | Crate ]
The above code also follows the same pattern. The crate immediately to the left of the one checked will become the player, if 3 crates are next to each other horizontally.
Left [ Crate | Crate | Crate ] -> [ Crate | Crate | Player ]
The crate furthest to the left will become the player, if 3 crates are next to each other horizontally.
If you try each of those examples out, I think you will start to see the pattern. The first cell is the object you’re checking to see what’s next to it. The following cells, in order of closest to farthest, are the objects you’re checking for.
- Up – checks above an object
- Down – checks below an object
- Left – checks to the left of an object
- Right – checks to the right of an object
- Horizontal – checks left and right side of an object
- Vertical – checks above and below an object
I hope to be doing more Puzzle Script tutorials but don’t have any planned just now. If you have any questions you can leave a comment below.