Anyone give me some help in working out how to spawn actors on a grid using blueprints.
i can get the actors spawning under mouse cursor using hit result but idealy i would like to get things spawning on a grid so items snap together and not clip through each other or the world… (grid not visible ingame ofcourse)
to get an idea of what i mean ive attached a quick image.
I recently added Grid Placement of objects in my RTS Game. This is how i did it. Make sure you give a high value for Nearest nth (like 200 or 300 etc).
Create an array of vectors where each vector is one of your grid points. So the first index in your array would be Vector 0x/0y. The next index could be 100x/0Y, then 200x/0y until you need to start the second row at 0x/100y.
If you look in my map generator threads I show a vector field generator that creates such a field with hexagons.
Whenever you spawn your actor you can choose one of those vectors for the spawn transform.
needs to be explained abit more not getting a grasp of it.
here is what i have. and it spawns the actor when i press a key at the cursor location. but ofcourse its not on grid so items dont snap together but overlap each other or go through the floor or wall etc etc.
What they’re suggesting is to use an array to handle your grid and store all possible spawn points in that array (or list since arrays in UE4 are only 1D). When it’s time to spawn, pick a random index from the array and spawn there. To create the array, define the width and height of the grid in variables (max rows & columns), then run two Loops, first on X, then Y (or Y, then X). Example:
For Loop Index=0 through Index=width-1
Set X
For Loop Index=0 through Index=height-1
Set Y
Make Vector (X*100 | Y*100 | Z=0)
Add to Grid]
To spawn, Get a random index from Grid] and use the vector returned as the spawn location.
To prevent spawning to an occupied grid space, use a second array GridUsed]. When a location is pulled from Grid], test if GridUsed] “Contains” that location. If so, choose a different location from Grid]. If it doesn’t exist in GridUsed], then Spawn the actor and Add the location to GridUsed].
Alternatively, instead of storing actual locations in Grid], you could simply use the array index as the grid-space: Resize Grid] using width*height. On a 10x10 grid, the bottom row would be index 0-9, the next row up 10-19, and so on through the top row of 90-99. In this case, you’d handle the spawn location after you Get a random index from the array. If the random index is 46, the location would be: X=46%width, Y=46/width, Z=0. Note that % is Modulo.
Also note that UE4 uses a coordinate system in which Y points East while X points North (or X points East while Y points South).