How to turn multiple variables into a string


#1

In a level I am trying to utilize arguments and turn them into a x,y pos this should work shouldn’t it?

this.peasantBuild = function() {
    var x = arguments[0];
    var y = arguments[1];
    var target = x +","+ y;
    this.command(peasant, "buildXY","palisade",target);
};

#2

Because the usage of buildXY is buildXY(type, x, y) where x and y are x and y coordinates. You are passing in a string like "52,27" as x and nothing (technically undefined) for y.

So really it should be like this:

this.command(peasant, "buildXY", "palisade", x, y);

You could also try something like the below, but calling slice on arguments doesn’t seem to work.

this.peasantBuild = function() {
    var args = Array.prototype.slice.call(arguments, 0); // won't work for some reason
    args.unshift(peasant, "buildXY", "palisade");
    this.command.apply(this, args);
}

There is also no peasant defined within the function, though that could be defined elsewhere.

(Really though, why not just use function (x, y) { ... } ?)


#3

You can test also :

var myY;
var myX;
var target = {x:myX, y:myY};

If you need a target parameter.


#4

@trotod I tried that at first but it did not work

@Vettax I just tried that but it did not work either

Any suggestions @nick


#5

I don’t know what is the correct syntax for this.command(..., "buildXY", ...), but if you want to convert an integer to a string in JavaScript, the idiom is to add it up to an empty string:

"" + x + "," + y

#6

thanks for the help ya’ll but I am still getting the same error


#7

Which part of build XY are you having a difficulty with…

Do you moveXY(pos) or moveXY(“x,y”) . . . . . . . NO!!


#8

No I’m commanding a peasant to build something here is my syntax for the build command

 this.command(peasant, "buildXY","palisade",target);

#9

(I apologize for yelling, but …)

DO WE NEED TO POUND YOUR HEAD WITH A HAMMER . . . WE KEEP TELLING YOU IT IS:

this.command(peasant, “buildXY”, “palisade”, X, Y);

it is build X Y NOT build position NOT build string

By the way, position aren’t strings either, they are dictionary/objects and are created as vettax said:

targetPos = {x: 50, y: 50};

If you try it directly with a string (which you should have done before trying to build one):

this.command(peas, "buildXY","palisade", "50,50");

you get an error!! NO amount of string building is going to fix what a straight string can’t do.

If you try a proper position it will give you the SAME ERROR:

this.command(peasant, "buildXY", "palisade", {x: 50, y: 50});

It wants an X, Y coordinate. Once again because it is: buildXY

Since you refuse to believe us maybe you will believe the guild:

this.command(peasant, “buildXY”, “palisade”, 50, 50);

You find that you get NO error, because finally you did your part right.

(Your peasant may walk to the spot and build nothing. I’ve only tried on a couple of levels but haven’t got one to build stuff outside of Misty Island Mine, myself.)

(Again, I apologize for yelling.)


#10

Okay now the peasant builds but I’m still getting an error this is the command\

this.command(peasant, "buildXY","palisade",arguments[0],arguments[1]);

and I need a way to prevent the peasant from building more then the one necessary pailside

Another thing to note I find that the arguments array is working is a this a fail in the API protection

Yes, yes you do


#11

Well we can’t tell how you are using your this.peasantBuild() method.

You might be getting the error because there is no peasant defined, but there’s no way for us to know.

If you don’t want your peasant building unnecessary palisades then don’t call the function when you don’t want to, ie

if (condition) this.peasantBuild(x, y);
else this.say('won\'t build palisade');

I don’t think arguments is a fail in the “API” protection, it’s part of JavaScript, therefore anyone should be allowed to use it. Also, arguments is not an Array, it is an Array-like object (the properties are numbered and there is a length property, as well as caller and callee, but that’s about it; see MDN for more info).

And you are making the function somewhat more complicated, it would seriously be easier if you just did

this.peasantBuild = function (x, y) {
    // do something with x and y and not have to care about using arguments
    // because `let [x, y] = arguments` is bit cumbersome
    this.command(peasant, 'buildXY', 'palisade', x, y);
};