Spawning actors to a grid?

Hi everybody!

So, right now I have this game that allows the player to spawn actors at their feet while moving. But I’m running into an issue. I need the spawned items to snap to a grid of some kind, and I’m not sure how to do that.

I’ve thought that creating a sort of grid in a blueprint might work, but am unsure of where to go from there. This is what it looks like:

That script is there to make the grid, but I’m unsure of how to tell the spawned items to snap to that.

Anyone have any ideas that might help me here?

Depends on how they should snap. Should the user select the point?

It’s really not enough information to help you out.

When you want the spawned object to snap to the nearest element of the grid it’s really easy.

Spawn a 2D Array of Grid-Elements and save their X-Y-ID and position. When an object is spawned get the current worldPosition and based on the position of the first grid-element and the distance between each grid-cell you can calculate the nearest grid cell ID. Then you just need a function that iterates through all grid-cells and returns the WorldPosition of the grid with that ID.

FVector gridBase = getCellLocationAt(0,0); // get the world location of the cell 0|0
float cellDist = 50.f; // the distance between each cell of the grid
FVector newObjectLoc = obj->GetActorLocation(); //Get Actor Loc
FVector dist = gridBase - newObjLoc; // Distance
int32 X_ID = floor(dist.X / cellDist);
int32 Y_ID = floor(dist.Y / cellDist);
if(dist.X % cellDist > cellDist/2) X_ID++; // Check which element is closer
if(dist.Y % cellDist > cellDist/2) Y_ID++: // and for Y
FVector finalSnapLocation = getCellLocationAt(X_ID,Y_ID); // TADAA

This sounds like it’s exactly what I’m needing!
I just need to figure out how to get this setup now. I’m pretty new to Unreal so this is easier said than done :slight_smile:

Thanks for the help!

if you can add a delay to the spawning of milaseconds or 2 seconds it should automatically place at given grid at the moment and make unrepeatable

Tell me when you need help :slight_smile:

I could definitely use some more of your help.
I’m wanting to do this in blueprints since I know very little C++.
Here’s what my actual grid looks like.

d2c7c336148fb9fd294c6226a173ad66b7a6e604.jpeg

Basic formula for calculating grid position:

  1. Take the world x and z location of the object you want snapped to the grid and divide by the number of “boxes” on that axis. So if we have a grid that is 32 tiles wide and 32 tiles tall, divide the x and z by 32. This will give you the grid location…ie this position is at grid point (2,2). It is a good idea to round the decimal using “floor” function.

float gridLocX = floor(x / 32.f);
float gridLocY = floor(y / 32.f);

NOTE: This formula expects the top right corner of the grid to to be located at 0,0. If this is not the case, the coordinates with have to be adjusted to take into account the position of the grid. Something like:

float gridLocX = floor((x - gridOriginX) / 32.f);
float gridLocY = floor((y - gridOriginY) / 32.f);

(Subtract if the grid origin is positive on the x/y axis, add if the x/y is negative on the axis)

  1. Multiply that by the size of the “tiles” in world space of each grid square. For example 20cm x 20cm grid size:

float gridWoldPosX = gridLocX * 20.f;
float gridWoldPosY = gridLocY * 20.f;

(Don’t forget to add the grid origin back into the gridWorldPos if the origin is not located at 0,0)

  1. After that, there is some basic math you can do to center it within the grid tile. Divide the width and depth of the object by 2 to get the half size, then add that to gridWorldPos

I explained it in code, but it should be simple to translate it to blueprints. I haven’t tested this in UE4 but the idea is universal. This method is good for if you don’t have grid tile objects and the grid is just empty space.

Alternatively, you could do something like cast a ray to find the tile object that that position and maybe parent the object to that grid (I’m not sure if this is possible in UE). Actually, you don’t even have to parent it, just set the object’s location to the tile location.

So, you spawn a grid of these elements? I would make an BPActor for a cell and give them a variable of the type Vector2D named ID.

Then you spawn an array of those actors just like you did in the picture with Spawn Actor From Class. For each spawned cell set the ID x-y ID and add it to an array of Cells, where all cells should be stored. Then add a function with a parameter of the type Vector2D in your grid-actor that iterates through that array. Compare each cell’s ID with the one of the parameter, if they match return the actor Location of this cell (Get Actor Location). The rest is just like my mathematical explanation above. Try to setup what I wrote and post the screenshots of the BP’s.

I’m pretty stuck on this part still :stuck_out_tongue:
Do you know of any tutorials/documentation that covers this or something similar to give me a good idea on how to get this setup?

You can add me on Skype ( ) and I can explain it to you better

That would be awesome, thank you!!
I’ll get you added once I get home from work.