Bug on Code Orders?

My code works for most of the units that I have to deploy, but there is always one that gets sent to the wrong coordinates.
here is my code. Is it me, or the game???

var sign = this.findByType("sign")[0];
var message = sign.message;
var i=0;
this.say(message);
loop{
    var m = message.slice(i*5,i*5+1);
    if (m=="a"){
        this.summon("archer");
    }else if (m=="s"){
        this.summon("soldier");
    }else if (m=="p"){
        this.summon("peasant");
    }else if (m=="g"){
        this.summon("griffin-rider");
    }
    var friend = this.built[this.built.length-1];
    if (friend){
        var m2=message.slice(i*5+1,i*5+2);
        var m3=message.slice(i*5+2,i*5+3);
        var m4=message.slice(i*5+3,i*5+4);
        var m5=message.slice(i*5+4,i*5+5);
        if (m2=="0"){
            var x1=0;
        }else if (m2=="1"){
            x1=1;
        }else if (m2=="2"){
            x1=2;
        }else if (m2=="3"){
            x1=3;
        }else if (m2=="4"){
            x1=4;
        }else if (m2=="5"){
            x1=5;
        }else if (m2=="6"){
            x1=6;
        }else if (m2=="7"){
            x1=7;
        }else if (m2=="8"){
            x1=8;
        }else if (m2=="9"){
            x1=9;
        }
        if (m3=="0"){
            var x2=0;
        }else if (m3=="1"){
            x2=1;
        }else if (m3=="2"){
            x2=2;
        }else if (m3=="3"){
            x2=3;
        }else if (m3=="4"){
            x2=4;
        }else if (m3=="5"){
            x2=5;
        }else if (m3=="6"){
            x2=6;
        }else if (m3=="7"){
            x2=7;
        }else if (m3=="8"){
            x2=8;
        }else if (m3=="9"){
            x2=9;
        }
        if (m4=="0"){
            var y1=0;
        }else if (m4=="1"){
            y1=1;
        }else if (m4=="2"){
            y1=2;
        }else if (m4=="3"){
            y1=3;
        }else if (m4=="4"){
            y1=4;
        }else if (m4=="5"){
            y1=5;
        }else if (m4=="6"){
            y1=6;
        }else if (m4=="7"){
            y1=7;
        }else if (m4=="8"){
            y1=8;
        }else if (m4=="9"){
            y1=9;
        }
        if (m5=="0"){
            var y2=0;
        }else if (m5=="1"){
            y2=1;
        }else if (m5=="2"){
            y2=2;
        }else if (m5=="3"){
            y2=3;
        }else if (m5=="4"){
            y2=4;
        }else if (m5=="5"){
            y2=5;
        }else if (m5=="6"){
            y2=6;
        }else if (m5=="7"){
            y2=7;
        }else if (m5=="8"){
            y2=8;
        }else if (m5=="9"){
            y2=9;
        }
        var x=x1*10+x2;
        var y=y1*10+y2;
        var target = {x:x,y:y};
        this.command(friend, "move", target);
    }
    
    i++;
}

For the love of all that is holy… use TAB to organize things…
Secondly, read FAQ - Check Before Posting on how you should post your code.
Thirdly, Number will be your friend.

var sign = this.findByType("sign")[0];
var message = sign.message;
var i=0;
this.say(message);
var summonArray = {a:"archer", s:"soldier", p:"peasant", g:"griffin-rider"}
loop{
	var m = message.slice(i*5,i*5+1);
	
	this.summon(summonArray[m]);
	
	var friend = this.built[this.built.length-1];
	if (friend){
		
		var x1 = Number(message.slice(i*5+1,i*5+2));
		var x2 = Number(message.slice(i*5+2,i*5+3));
		var y1 = Number(message.slice(i*5+3,i*5+4));
		var y2 = Number(message.slice(i*5+4,i*5+5));
		
		var target = {x:x1*10+x2,y:y1*10+y2};
		this.command(friend, "move", target);
	}

	i++;
}

^ here is compact version of your script, hope that helps a bit :slight_smile:

And some advice, whenever you’re using math in your code, encapsule everything as much as you can, just to make sure, because it can be weird.
aka:

var target = {x:x1*10+x2,y:y1*10+y2};

becomes

var target = {x:(x1*10)+x2, y:(y1*10)+y2};

or

var target = {x:x1*(10+x2), y:y1*(10+y2)};

because the code doesn’t know which one you want to use at that point.

lol. I was trying to convert the string into a recognizable number. the string wasn’t reading as a number, so it wasn’t working. all that text was a simplistic (LONG!!!) way to convert the string into actual numbers. Your way was much simpler though (and less time consuming/mind numbing). Thank You!

1 Like

You’re very welcome :slight_smile: Google is your friend! Most things probably already exist as a usable function, here is a very annoying way to explain this: http://lmgtfy.com/?q=javascript+string+to+number :wink:

But, like I said, it’s very much possible your other issue is related to not encapsuling your math, so, check if that fixes things :smiley: