How do I create a snap grid volume?

I need to create a snapping grid volume for the inside of this space, for objects to snap within the grid to the correct increments so that they fit within the space. We know the objects size increments and even the space increments, but i do not know how to go about execution and i’ve seen no videos do what i need yet nor read applicable documentation. Help please

I feel like you’ll probably get answers more quickly if you can help folks visualize your goal. Explain how a user/player will be able to really touch and experience this, and it’ll be easier for a wider audience to grasp what you’re trying to achieve and offer useful suggestion. :smiley:

I’d start with a Blueprint that had a Box Volume, with its size and scale set to 1.

In the Event Graph (or Construction Graph, depending on exactly how you want to utilize this grid) you can start with a Get Variable for that box, drag a wire off, and get its Scaled Extents (or Unscaled if you wanted to set it without scaling).

That outputs a vector. You can Break that vector into X, Y, and Z coordinates. Snapping, at that point, it just a comparison between percentages of the total length of each axis. Once you exceed 50% to the next percentage (or grid point), your object will need to snap. From there, snapping components within that Blueprint (say, a Static Mesh Component that was already part of our Blueprint) would be a trivial matter. You just set that components Local (or World) location to the location of the next percentage along the axis. Do that for all three axes and you’re snapping in 3D space. Easy peasy.

Now, if the object snapping inside this box is an entirely separate class (or Blueprint), then you will need to use some BP communication to get the coordinates you’d like to snap to from the Box Component, and apply them to whatever is being snapped. That shouldn’t be too terribly hard:

I would probably use a Blueprint Event Dispatcher.

https://docs.unrealengine.com/latest/INT/Engine/Blueprints/UserGuide/EventDispatcher/index.html

That could get the Blueprint containing the volume to report back the next available snapping location based on a set of input coordinates, which you could then set as the new location for the object that needs to snap.

However, if I was just moving between snap points using, say, the arrow keys, I wouldn’t even bother. If I know the volume and I know the object size snapping within it, I can easily just create a StaticMeshComponent of my object that snaps inside the volume, and when I press an arrow key, just move it by a percentage.

Put another way, the concept of doing division, rounding, etc. would only be useful to me with analog controls. Such as the mouse cursor moving around in my volume and I want something else to snap to the nearest point to the cursor.

If I was just using digital inputs (gamepad, keyboard) I wouldn’t bother rounding because there would probably have to be some amount of motion under the hood before a player noticed the snap (i.e. hold left on the controller until the coordinates achieve a snap point, and only THEN does the snapping object move… that would be terrible).

Also, if the object ENTERS the volume and then needs to snap, it’s also easy to make an OnOverlap event which grabs that mesh, hides it (and deactivates its collision), stores it in a variable array, and then REPLACES it with a static mesh component of the same mesh and transform. That means you basically put the one from outside your volume into an array and spawn a component that the player THINKS is the same one. Until you pull it back out of your volume, at which point you destroy the component version and reinvoke that one you stored in the array.

But that’s all theoretical… again, not sure how you want to use this snap grid. :smiley:

Anyway, this is a stack of things to chew on. I hope it helps in some way!

Thanks man! This really helped flesh out the concept of how to set it up for me. I will use the example of a candy in a basket. So i set the basket up as the volume and i’m trying to place the candy neatly within the basket by touch dragging the candy thru the grid until release. i also would like to set it up to where if there is no candy under the one im moving, that the candy snaps to the back. I Am currently going to try using the Input Touch variable to try and execute the rest of this. Is there a more efficient, or simpler way at all? Either way thanks for this guidance!

Can you please show blueprints examples?