Sliding Block Puzzle Blueprint

Could anyone help me as to be able to create a sliding puzzle with the use of blueprint.

30229-tilepuzzle.jpg

The furthest that I can understand is that I will need to line trace to be able to see if that individual piece has a free slot beside it to be able to move there.

And then with booleans for when they are in the correct spaces.

Edit 5/16/2018 - See my other answer. I’ve made available a fully functional demo of this in the first person template game type. Check my other answer below!
**You can download the project files, add to your Unreal Projects Folder, and take a look at all the Blueprints/Actors in the Slider_Puzzle folder: **

Many thanks to these two for providing the much needed legwork in how to accomplish this.

I’m trying to make exactly what you made, do you have more pictures of your blueprint? I’m trying to make my first game and have no idea where to start with this.

Sure, here is a link to a gallery of more screens:

edit: 5/15/2018 - Added additional pictures showing the variables and descriptions for each - thanks Awliee!
https://imgur.com/gallery/bOtf8WZ

I am in the process of overhauling this entire Blueprint, and will update this answer with more information once complete!

The basic idea is this:

  1. You have an array of X number of ‘Block’ actors, in this case 9. I chose to set these up as child actor components within a parent actor, but there could be other ways to set them up.
  2. Each block has a location value, in this case we only care about the X and Z values of each block.
  3. Each block also keeps track of it’s current index position and it’s ‘correct’ index position. For example, block 6 has a current index position of 0 (top left), but it’s correct position would be 5.
  4. One block has the boolean ‘Is Missing Block’ set to true. This blocks positioned is kept track of as well.
  5. Once you the player, select a direction to move, the parent actor checks each of it’s child actors positions at an offset. For example, if you select ‘UP’, it will check the position of 1 block above each block in space.
  6. If that offset position is EQUAL to the position of the missing block, it will then swap the positions of the Missing Block and the Block to Move, using a temporary placeholder vector position variable to help the transition.
  7. After it swaps the ‘Missing’ and ‘Moved’ Block, it updates the index position on those blocks, and then checks the current Index positions on all blocks against their correct positions.
  8. If the number of correct positions is equal to the length of the block arrays, the puzzle is ‘complete’ and you can fire off whatever functionality you want off that.

I hope that helps in some way, I may make a tutorial for just the sliding block component when I have some more free time here. Let me know if you have any specific questions or issues!

I wanted to update this thread again for anyone who finds it or is having trouble completing their own slide puzzle. I’ve lifted the Slide Puzzle setup I have in my game and ported it into an isolated project with some of the bells and whistles missing, but core functionality is there. This doesn’t use any line tracing, instead it just compares vectors in relative space.

You can download the project files, add to your Unreal Projects Folder, and take a look at all the Blueprints/Actors in the Slider_Puzzle folder:

Demo notes:

  • The blueprints are mostly commented as it goes through logic/functions. Some of the comments may have been lost in the porting process, so if anything is unclear leave a comment here!
  • In the demo map, you can see the Slider_Puzzle_AutoGenerate (the puzzle itself) and a Slide_Puzzle_Controller_New (the controller trigger). Note that on the Puzzle_Controller, it has a variable that has been set to reference the Slider_Puzzle in level. This is how the player controls communicate with the Slider Puzzle actor.
  • Select the Grid Size (2x2, 3x3, 4x4, 5x5) on the Slider Puzzle_Auto_Generate and it will spawn the blocks automatically. Be sure to also set the appropriate texture so the numbers match up properly on the tiles. You can add your own Textures/Photos and it will splice them up automatically. I included a sample texture photo of my hometown to demonstrate. :slight_smile:
  • At game start, it will randomize the blocks and cut up the assigned texture. Refer to the Array of Structs 2x2UVData, 3x3UVData, etc. and the Puzzle Master Material for how this is achieved.
  • The randomized blocks are checked for parity (I found this here https://www.cs.bham.ac.uk/~mdr/teaching/modules04/java2/TilesSolvability.html by brilliant Mark Ryan). If an unsolveable puzzle is generated, it will re-randomized the blocks and check again. This is honestly the most important element of this whole thing, otherwise you can end up with broken puzzles. Look at the CheckInversions function in the Puzzle actor for this.
  • Since the blocks spawn from the bottom up, the solution is numbers go bottom to top, left to right (i.e 1 in the bottom left corner, missing block in the top right corner).

Give it a whirl and let me know what you think! It’s not fancy but it’s got all the basic logic involved in creating your own puzzles!

havent checked this, but i have used your previous imgur guide to set up my own sliding puzzle in VR (had to completely change the logic in the checkForMissing function and some other stuff) but the general layout helped me out so much.

Thanks for your work

That’s great! I’m gald I could help you out, I’d be very interested to see a VR execution of this! The new .RAR just has grid size selection, proper randomizing at runtime, and solution validity checks added.

This is great, nice work!

Is there a way to edit this so that it uses individual meshes instead of dividing a texture?

For instance, I have 9 tiles mapped to a texture and 1 tile will need to be found elsewhere in order to complete the puzzle. The logic is all there for the game play to work, I just don’t know how to edit the blueprint to use my meshes and texture. Also, I can’t seem to get the controls to activate the puzzle when I migrated the blueprints. I get a message on my screen that says “wait the puzzle is active” when I press the arrow keys but the puzzle pieces do not move. Any advice?

Hello,

Regarding the control issue, I would suggest Simulating the blueprints as you run them and watching the blueprints in a separate screen. That way you can track where the issue is occurring. Is it consistently occurring each time or only after certain conditions? I just tested again and the controls do function, but there is not a lot of error handling in the setup, so it may be possible that a bug could occur. The logic is in the Slide_Puzzle_Controller_New actor, where a .25 second delay is manually set before reverting the controls to receive input properly.

Regarding the textures, if you look at the RandomizeBlocks function in the Slide_Puzzle_Autogenerate actor, you can see how the textures are set on each puzzle block. You could replace the materials set here with your own mats or textures. You may have to change the material (PuzzleBlockMaterial variable) type on the Puzzle_Block actors to Material instead of Material Instance to suit your needs.

If you wanted to add different meshes, you can look into the SpawnPuzzleBlocks function in Slide_Puzzle_Autogenerate. Look at the Add Child Actor Component blueprint node. Currently it is set to spawn Puzzle Block actors. You could create your own class to spawn, or replace the default mesh in the Puzzle_Block actor itself to whatever custom mesh you need.

If you’re having any issues please feel free to comment here or message me with screenshots/video demonstrating the issue.

Hey Drowanpower,

Thanks for replying to my question. I haven’t began working on this as I’ve been busy, but I am interested in getting this to work in my project. Do you have a Discord or email I can reach you at for any further questions? I’m not sure if this is the best place for it.

I’m trying to do sort of the same type of puzzle. However, there are a few differences. In my puzzle, the player needs to be able to place 10 items from their inventory into the puzzle grid (Imagine a telephone keypad without the * & # and no empty spot) and then be able to arrange the items (swapping instead of sliding) in to their correct location in order to solve the puzzle. Solving it turns on a set of lights.