• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

SOLVED - Randomly generated objects have no collision

Lemmy4K

Member
Hey,

In my game I have these enemy's that have this simple ai grid pathfinding that I used from this itch.io page. https://randatabase.itch.io/simple-ai-grid-pathfinding
It does what it's supposed to do but I have a little problem.

I have this system where is randomly spawns a certain amount of tree's on a 64x64 grid and it looks quite nice but the enemy is not colliding with these randomly generated trees. It just walks right through it. The only time it ever does collide is when I purposely place a tree object in the room and THEN it walks around it. Which is what I want.

Here's the code the code from the Tree Spawner. (ObjTreeSpawner)

Code:
CREATE EVENT
var cellSize = 64;

var columns = room_width div cellSize;

var rows = room_height div cellSize;

var grid_xArray = array_create(0);

var grid_yArray = array_create(0);

var count = 0;


while (count < 300) //Change number to how many objects you want to be spawned

{
    var c_pick = irandom_range(0, columns);

    var r_pick = irandom_range(0, rows);

    if (count > 1)
    {
        var n = array_length(grid_xArray);
        for (var i = 0; i < n; i++)
        {
            if (grid_xArray[i] != c_pick)
            {
                if (grid_yArray[i] != r_pick)
                {
                    count++;
                    array_push(grid_xArray, c_pick);
                    array_push(grid_yArray, r_pick);
                    break;
                }
            }
         }
      }
      else
      {
            count++;
            array_push(grid_xArray, c_pick);
            array_push(grid_yArray, r_pick);
       }
}

var n = array_length(grid_xArray);



for (var i = 0; i < n; i++)
{
    var xx = grid_xArray[i] * cellSize;

    var yy = grid_yArray[i] * cellSize;

    instance_create_layer(xx, yy,"Collision", ObjTree);    //Collision is a name for a layer I have

}
And I'm only going to paste a little snippet from the AI example where I think the problem is happening.

This is inside an object called controller_grid
Specifically the line - mp_grid_add_instances(global.grid,ObjTree,false);

Code:
CREATE EVENT
//Size of Cells
var cellWidth = 64;
var cellHeight = 64;

//Amount Of Cells
var horCells = room_width div cellWidth;
var verCells = room_height div cellHeight;

//Create Grid
global.grid = mp_grid_create(0,0,horCells,verCells,cellWidth,cellHeight);

//Add Collision Object To Grid

mp_grid_add_instances(global.grid,ObjTree,false);
Can anyone help me out?
 

Ninety

Member
Two separate create events won’t necessarily fire in the order you want. You should use an alarm to offset the path finding generation by one step from creation, see if that helps.
 

TheouAegis

Member
You can set the instance order, but the safest thing to do is not rely on that. Another idea is to use the room start event for your grid controller as long as your tree spawner is also located in the room. If tree spawner is not located in the room that the grid controller is in, then it wouldn't work.
 

Lemmy4K

Member
You can set the instance order, but the safest thing to do is not rely on that. Another idea is to use the room start event for your grid controller as long as your tree spawner is also located in the room. If tree spawner is not located in the room that the grid controller is in, then it wouldn't work.
yea setting the grid controller to room start seemed to fixed the problem. Thanks bud!
 
Top