Road building and findPathTo



  • Hello,

    I try to build a road between 2 points, but this is not working :

            var sources = Game.spawns['spawn1'].room.find(FIND_SOURCES); // find source location
            var path = Game.spawns['spawn1'].pos.findPathTo(sources[0].pos); // get path between source and spawn
            
            for (var i = 0; i < path.length; i++) // for all position in the path, do :
            {
                Game.spawns['spawn1'].room.findPathTo(path[i].X,path[i].Y, STRUCTURE_ROAD); // build road
            }
    

    I don't understand how to work my variable path. He contain "object Object" x18 time (18 tile between my two point) I just want to get path[0].X ; path[0].Y ; path[1].X ; path[1].Y ; ...

    If someone can help me, thank you ^^



  • Roads only exist on a single position, so you don't need both points. You probably want something like this.

    for (var i = 0; i < path.length; i++) {
        path[i].createConstructionSite(STRUCTURE_ROAD);
    }
    

    Sitting a road on two tiles near each other will then connect those two tiles with a road. Creeps get the fatigue reduction when stepping onto a road tile, not when traveling between two road tiles.

    If for whatever reason you did want both points, you could do something like this

    for (var i = 0; i < path.length - 1; i++) { // note the -1
        var fromPos = path[i];
        var toPos = path[i + 1];
    }
    


  • Thank you for your answer, it helped me to find that I wanted to do

            var sources = Game.spawns[NomSpawner].room.find(FIND_SOURCES);
            for (var j = 0; j < sources.length; j++)
            {
                var chemin = Game.spawns[NomSpawner].pos.findPathTo(sources[j].pos);
                for (var i = 0; i < chemin.length; i++) 
                {
                    Game.spawns[NomSpawner].room.createConstructionSite(chemin[i].x,chemin[i].y, STRUCTURE_ROAD);
                }
            }
    

    Now everything works