Announcement

Collapse
No announcement yet.

Spawning Actors on Grid?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Spawning Actors on Grid?

    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.

    Click image for larger version

Name:	grid.png
Views:	1
Size:	599.7 KB
ID:	1136420


    any help would be great. thanks
    Getting there...

    #2
    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).
    Support my RTS Game (Agora) on Patreon!
    Unreal Nexus - Create Share and Experience!
    Unreal Engine 4 World-Wide User Map

    [PLUGIN] Aws Gamelift Client Plugin
    [TOOL] Create your own binary release of UE4 from GitHub source
    My UE4 Tutorials and Misc Tools

    Lead Programmer at YetiTech Studios
    Your Friendly Neighborhood
    Satheesh PV (a.k.a RyanJon2040) | Twitter, Instagram, Facebook, LinkedIn, YouTube, Google+

    Comment


      #3
      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.
      Map Generator 1.0
      Map Generator 2.0
      Map Generator 3.0

      Comment


        #4
        going way over my head lol.

        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.


        Click image for larger version

Name:	bp.png
Views:	1
Size:	154.3 KB
ID:	1052403
        Getting there...

        Comment


          #5
          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:
          Code:
          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).
          Last edited by DoteDote; 07-06-2014, 02:03 PM.

          Comment

          Working...
          X