Join the Discord. Talk about Game Dev. Talk about Gaming.
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.
Checking Sides
Sometimes you might want to check what’s next to an object. In Puzzledorf, for instance, I had to check what was around the player, and if a block is next to you, I make it flash, letting the player know you can push it.
There are other instances in Puzzlescript, however, where you might want to check something specific, like if there is a trap next to the player on one side and you are holding out a metal detector.
We can do it without too much difficulty. Let’s review the PuzzleScript code format:
[ Condition ] -> [ Event ]
a.k.a
[ 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.
Key Words
- 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
Conclusion
I hope to be doing more Puzzle Script tutorials but don’t have any planned just now.
Hey how would you check for certain arranges of crates? For example I want to check if four crates are arranged as a square:
This condition is true:
#########
#…….#
#…**..#
#…**..#
#…….#
#…….#
#########
But these are false:
#########
#…….#
#…….#
#.****..#
#…….#
#…….#
#########
#########
#…..*.#
#…*…#
#…**..#
#…….#
#…….#
#########
I’m not sure how to combine directions with multiple conditions.
Thank you for the tutorials!
LikeLike
I am not entirely sure how you would combine them (just a note also, comments have to be approved / moderated first but all of your comments got through).
And no worries, glad you enjoyed the tutorials 🙂
You might have to experiment a bit. I would try getting vertical checking working, and horizontal checking working separately to each other.
Once you’ve got that, you’ll have to experiment to combine them. One solution might be to use extended rigid bodies, but I’ve never explored that:
https://www.puzzlescript.net/Documentation/rigidbodies.html
Otherwise there might be a way to loop over them all, but I haven’t tried that before either. I am vaguely aware loops exist in Puzzle Script, but I have not tried them.
I would suggest exploring the documentation and also looking at other projects on the Puzzle Script front page and using the “Hack” button to see how they coded theirs. When you can’t figure something tricky out, that can be a good way to explore other solutions. Good luck!
LikeLike
Whoops sorry about the spam! I got caught in a login loop and wasnt sure if it was going through.Thanks for the speedy response, Ill try that!
LikeLike
No worries, just happened to be on 😀
LikeLike