How To Check Next To Objects – PuzzleScript

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. 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 ]

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. If you have any questions you can leave a comment below.

If you enjoyed this article, try my game Puzzledorf out on Steam and Humble Bundle.

4 thoughts on “How To Check Next To Objects – PuzzleScript

Add yours

  1. 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!

    Like

    1. 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!

      Like

Leave a Reply to nickdanger7 Cancel reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: