[Solved] Chained statements like .moveXY() and behavior override each other?

How do I get to lead my spawns along a polyline in Game Dev 2?
The code below results in the archer cutting through to endpoint without making any turns.

var archer=game.spawnXY("archer", 9, 57);

archer.moveXY(25, 58);
archer.moveXY(26, 36);
archer.moveXY(44, 36);

This is what I get:

Can anyone explain?

1 Like

What do you mean by that?

Isn’t the archer supposed to go to 25,58 (which is almost horizontally to the right),
then to 26,36 (almost vertically down),
and then to 44, 36 (again horizontally to the right)?

Try to put a game.wait so the commands will wait for the previous one to finish in order for it to start.
Does it work now?

How exactly do you use it?

Like put after every moveXY a game.wait(7) and you could change the number of seconds that the game waits.
Do you need any more assistance at this level?

I get an error “game has no method wait”…

1 Like

Or try something like this:

# here spawn the archer
var k=0
while (k < 3){
    if (k === 0){
        archer.moveXY(25,58);
        if ( archer.pos.x == 25 && archer.pos.y = 58){
            k=1;
        }
    }
    else if ( k == 1){
        archer.moveXY(26,36);
        if ( archer.pos.x == 26 && archer.pos.y = 36){
            k=2;
        }
    }
    else if ( k == 2){
        archer.moveXY(44,36);
        if ( archer.pos.x == 44 && archer.pos.y = 36){
            k=3;
        }
    }
}
2 Likes

Do you need any more assistance at this level?

Oh, wow! Thank you, this one is really complex for a tree-move path!
Still, I’m afraid, this didn’t do the trick. The code was either too slow or there was an infinite loop.I can’t really spot what’s wrong, as the loop should no longer run after (k==3).

I've added some semicolons and equal signs before running it. Could you have a look if it's allright?
var archer=game.spawnXY("archer", 11, 52);
var k=0;
while (k < 3){
    if (k === 0){
        archer.moveXY(25,58);
        if ( archer.pos.x == 25 && archer.pos.y == 58){
            k=1;
        }
    }
    else if ( k == 1){
        archer.moveXY(26,36);
        if ( archer.pos.x == 26 && archer.pos.y == 36){
            k=2;
        }
    }
    else if ( k == 2){
        archer.moveXY(44,36);
        if ( archer.pos.x == 44 && archer.pos.y == 36){
            k=3;
        }
    }
}

Something else that confuses me is that if I append
archer.behavior="Defends";
in the end of my code at the start of the topic, the archer does not move at all and immediately goes defensive at the spawn coordinates.

It feels like Game Dev 2 map has other rules than other maps I’ve been to.
I did not have trouble making a unit go in circles on previous maps.

1 Like

Sorry, but I am out of ideas unfourtunaly. But your code is ok formated. @dedreous, @Deadpool198, could you help @aYo11 with this one?

Thank you for your trouble anyway! I felt I was going insane when the code that should work just wouldn’t…

1 Like

While I’m not sure that I’ll be able to provide the answer, this has me curious too. To begin with, before adding any of the archer code, does your code work…are you able to complete the level? From the video, it looks as tho you either commented out all other objects, or something in the code is preventing them from showing. If you can pass the level, please post your entire code, including what you’ve added for the archer.

1 Like

There is no other code, just that.

This is an isolated part of my Game Dev 2 Final Project. I’m using Game Dev 2 Level 25 (Game of Coins Step 1: Layout) with all the code removed as a clean slate.

I’ve got my final project code so far stored in the Final Project level and have not seen it behave any differently. I figured I should run and test separate fragments isolated as the whole thing got tricky and complicated. I acted based on a suggestion that the both levels share the same rules.

It would be cool to have a sandbox where one could give their code fragments a dummy run.

1 Like
This is my code so far in Final Project Level
// Spawn a player with spawnPlayerXY()
var hero=game.spawnPlayerXY("samurai", 34, 8);
hero.maxHealth=200;
hero.attackDamage=70;
hero.maxSpeed=20;
// Add at least one goal!

game.addSurviveGoal();
var scoutGoal=game.addManualGoal("Defeat the three big ogres");
var skeletonGoal=game.addManualGoal("Defeat the skeleton using the lightstone");
var generatorGoal=game.addManualGoal("Destroy the generator! Have archers protect you from behind the fence.");

// Counters
game.collectedChests = 0;
ui.track(game, "collectedChests");
game.lightstoneCollected = 0;
ui.track(game, "lightstoneCollected");
var lightExists=false;
var lightDuration=6;
var lightEnd=0;
var heroGotLight=false;

// Animated units
var generator=game.spawnXY("generator", 65, 55);
generator.spawnType="scout";
generator.spawnTime=6;
generator.spawnAI="Scampers";

var skeleton=game.spawnXY("skeleton", 13, 56);
skeleton.maxHealth=1000;
game.spawnXY("ogre-f", 39, 56);
game.spawnXY("ogre-f", 41, 33);
game.spawnXY("ogre-f", 12, 10);
var archer1=game.spawnXY("archer", 10, 60);
var archer2=game.spawnXY("archer", 11, 59);
var archer3=game.spawnXY("archer", 8, 55);

// Pathways and keys
var lightX=game.spawnXY("x-mark-bones", 11, 26);
var fenceDoor1=game.spawnXY("fence", 52, 36);
var fenceDoor2=game.spawnXY("fence", 52, 32);
var openX1=game.spawnXY("x-mark-stone", 48, 35);
var closeX1=game.spawnXY("x-mark-wood", 43, 40);
var openX2=game.spawnXY("x-mark-stone", 58, 40);
var closeX2=game.spawnXY("x-mark-wood", 58, 35);

//var forestDoor1 = game.spawnXY("forest", 64, 28);
//var forestDoor1 = game.spawnXY("forest", 72, 28);

// Forest and fences

game.spawnXY("forest", 23, 8);
game.spawnXY("forest", 23, 14);
game.spawnXY("forest", 23, 22);

game.spawnXY("fence", 33, 46);
game.spawnXY("fence", 38, 46);
game.spawnXY("fence", 33, 50);
game.spawnXY("fence", 33, 54);

game.spawnXY("forest", 8, 48);
game.spawnXY("forest", 16, 48);

game.spawnXY("forest", 50, 60);
game.spawnXY("forest", 50, 54);
game.spawnXY("forest", 50, 48);
game.spawnXY("forest", 50, 42);

game.spawnXY("forest", 50, 28);
game.spawnXY("forest", 56, 28);

game.spawnXY("forest", 44, 22);
game.spawnXY("forest", 44, 16);
game.spawnXY("forest", 44, 10);
game.spawnXY("forest", 44, 6);

 // Fire traps
game.spawnXY("fire-trap", 57, 20);
game.spawnXY("fire-trap", 60, 20);
game.spawnXY("fire-trap", 63, 20);
game.spawnXY("fire-trap", 66, 20);
game.spawnXY("fire-trap", 69, 20);

game.spawnXY("fire-trap", 57, 17);
game.spawnXY("fire-trap", 60, 17);
game.spawnXY("fire-trap", 63, 17);
game.spawnXY("fire-trap", 66, 17);
game.spawnXY("fire-trap", 69, 17);

game.spawnXY("fire-trap", 57, 14);
game.spawnXY("fire-trap", 60, 14);
game.spawnXY("fire-trap", 63, 14);
game.spawnXY("fire-trap", 66, 14);
game.spawnXY("fire-trap", 69, 14);

game.spawnXY("fire-trap", 57, 11);
game.spawnXY("fire-trap", 60, 11);
game.spawnXY("fire-trap", 63, 11);
game.spawnXY("fire-trap", 66, 11);
game.spawnXY("fire-trap", 69, 11);

function onSpawn(event){
    var unit=event.target;
    if (unit.type=="ogre"){
        unit.maxHealth=15;
        unit.attackDamage=40;
        unit.say("Good morning");    
        while(true){
            if (unit.distanceTo(hero) < 20){
                unit.say("Hero within range" );
                unit.attack(hero);
            }
        }
    }
    if (unit.type=="skeleton"){
        while(true){            
            if (hero.distanceTo(unit)<20 && heroGotLight === false){
                unit.say("Hero got weapon!");
                unit.attack(hero);
            } else if (hero.distanceTo(unit)<8 && heroGotLight === true){
                unit.defeat();
            } else if (hero.distanceTo(unit)<20 && heroGotLight === true){
                unit.behavior="RunsAway";
            } 
        }
    }
}

function goToBorder(unit){
    var offset=game.randomInteger(-2,2);
    unit.moveXY(25+offset, 58+offset);
    unit.moveXY(26+offset, 36+offset);
    unit.moveXY(44+offset, 36+offset);
    //unit.behavior="Defends";    
}

function onDefeat(event){
    var unit = event.target;
    if (unit.type=="ogre"){
        game.spawnXY("potion-large", 32, 39);
        game.spawnXY("chest", 35, 38);
    }
    if (unit.type=="skeleton"){
        archer1.say("Skeleton down");
        goToBorder(archer1);
        goToBorder(archer2);
        goToBorder(archer3);
    }
}

function onCollect(event){
    var item=event.other;
    hero.say("Look what I've got! " + item.type);
    if (item.type=="gem"){
        game.collectedChests +=1;
        hero.say("Chests collected " + game.collectedChests);
        if (game.collectedChests >= 3){
            var lightstone=game.spawnXY("lightstone", 11, 26);
            lightExists=true;
            hero.say("Lightstone is spawned " + lightExists );
        }
    }
}


game.setActionFor("ogre", "spawn", onSpawn);
game.setActionFor("skeleton", "spawn", onSpawn);
game.setActionFor("ogre", "defeat", onDefeat);
game.setActionFor("skeleton", "defeat", onDefeat);
hero.on("collect", onCollect);


function checkLight(event){
    while(true){
        if (lightExists === true && hero.distanceTo(lightX) < 3 && lightEnd ===0){
            game.lightstoneCollected = 1;
            hero.say("I've got Light!");
            lightEnd=game.time+lightDuration;
            heroGotLight=true;
        }
        if (lightEnd < game.time && game.lightstoneCollected === 1) {
            lightExists=false;
            hero.say("Ooops, no Light!");
            heroGotLight=false;
        }
    }
}
game.setActionFor("lightstone", "spawn", checkLight);

It’s a kind of a quest.
You defeat the three big ogres and collect the three gem chests to spawn a lightstone. Pick up a lightstone to defeat the skeleton. On defeat of the skeleton, the function goToBorder() is executed with archers as arguments.
They generally get to the point, however somehow not nicely. But if I set their behavior after the .moveXY(), they no longer move on defeat of the skeleton. So I decided to test it separately while preserving the existing code :slight_smile:

1 Like

That is an excellent idea actually. Just so you know, the ‘Flower Groves’ are excellent proving grounds for campaign coding…the game dev tools are not available there, unfortunately.

I tested your archer code, but kept the other elements in place…same behavior, tho he did bounce off of the walls a bit :grin:

Just to make sure I understand correctly, you want the archer to move in a straight line, completing the current leg, before starting the next, correct?

2 Likes

Good news!

you want the archer to move in a straight line

As long as they get to their destination, it’s Ok. However, it’s not the only issue. I wanted to send the skeleton on patrol, but once the start and end coordinates coinsided, it just shook and never moved. I wasn’t able to make the unit circle around.

What really got me puzzled is that if I uncomment the change of behavior, the archers seem to completely ignore .moveXY() commands.

function goToBorder(unit){
    var offset=game.randomInteger(-2,2);
    unit.moveXY(25+offset, 58+offset);
    unit.moveXY(26+offset, 36+offset);
    unit.moveXY(44+offset, 36+offset);
    //unit.behavior="Defends";    
}
1 Like

Still, yes, I’d rather the units moved as I specifically coded them to :slight_smile:

1 Like

The behavior command will override the others, as it is the final command in that sequence.

I’ll play around a bit and see if I can come up with something. I think that @AnSeDra’s suggestion is a good start…sure, it might be a little complex for something seemingly so simple, but let’s see.

Danny might also have some good input here too.

As for the Flower Groves, the first one is in the mountains and I think is part of the main trail. The second is in the forest, but has to be unlocked to be accessed…it is a challenge level, so is off the track a bit. Sorry, I don’t remember which level unlocks it, except that it is also in the mountains…I think :grin:

1 Like

Somehow it did not work for me, but caused my brouser to crash as I ran it on Level 25 clean slate.
It seems there is no game.wait() method in that environment, either.
I am seriously considering teleporting the archers…

1 Like