Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

Game Development 2 level 16

For some reason, When I did what I was supposed to, I started the game and clicked “Play”. But it told me there was an error in code. I’ll just copy paste all of my code and say which lines are the important parts >:)

// Use manual goals to specify which ogres to defeat.

function createGenerator(spawnType, x, y, spawnAI) {
var generator = game.spawnXY(“generator”, x, y);
generator.spawnType = spawnType;
generator.spawnAI = spawnAI;
}

// Scouts are aggressive and munchkins are just walking.
createGenerator(“scout”, 12, 12, “AttacksNearest”);
createGenerator(“scout”, 68, 56, “AttacksNearest”);
createGenerator(“munchkin”, 12, 56, “Scampers”);
createGenerator(“munchkin”, 68, 12, “Scampers”);

var player = game.spawnPlayerXY(“duelist”, 40, 34);
player.maxHealth = 10000000000000000000;
player.attackDamage = 200000000000000000000;
player.maxSpeed = 20;

// These are our goals. Notice we save them in variables!
var spawnMunchkinsGoal = game.addManualGoal(“Let 6 munchkins spawn.”);
var dontTouchGoal = game.addManualGoal(“Don’t attack munchkins.”);
var defeatScoutsGoal = game.addManualGoal(“Defeat 6 scouts.”);
// game properties used to count new and defeated ogres.
game.spawnedMunchkins = 0;
game.defeatedScouts = 0;
ui.track(game, “spawnedMunchkins”);
ui.track(game, “defeatedScouts”);

function onSpawn(event) {
game.spawnedMunchkins += 1;
}

function onDefeat(event) {
var unit = event.target;
if (unit.type == “scout”) {
game.defeatedScouts += 1;
}
if (unit.type == “munchkin”) {
// dontTouchGoal is failed if a munchkin is defeated.
game.setGoalState(dontTouchGoal, false);
player.say(“Oops.”);
}
}

function checkGoals() {
// If game.defeatedScouts is greater than 5:
if (game.defeatedScouts > 5) {
game.setGoalState(game.addDefeatGoal, true);
player.say(“Done!”);
}
// If game.spawnedMunchkins is greater than 5:
if (game.spawnedMunchkins > 5) {
game.setGoalState(spawnMunchkinsGoal, true);
player.say(“Done!”);
}
// If both other goals are completed.
if (spawnMunchkinsGoal.success) {
if (defeatScoutsGoal.success) {
// Set dontTouchGoal state as successful…
game.setGoalState(dontTouchGoal, true);
}
}
}

game.setActionFor(“munchkin”, “spawn”, onSpawn);
game.setActionFor(“munchkin”, “defeat”, onDefeat);
game.setActionFor(“scout”, “defeat”, onDefeat);

while (true) {
checkGoals();
}
for some reason, Line 49 was an error, (which was // If game.defeatedScouts is greater than 5:
if (game.defeatedScouts > 5) { game.setGoalState(game.addDefeatGoal, true); player.say(“Done!”)) and it said that "setGoalStates argument should have a type object, but got function. When I clicked play, it said that my first goal was finished, but the goal “Win the game” wasn’t. (I don’t exactly know what that means. any help?)

Hello and welcome to codecombat discourse! This is a cozy forum where you can share ideas, share fan art, get assistance for code, etc! Before you proceed, we hope that you review this topic, which shows all essentials of this board! Thanks!

Welcome To The Form!!! Please format your code with the </> button so we can see indentation
Ps I can’t help cause I do Python.

Is this Solved? I believe not please only mark the Solved button if you’ve passed the level.

1 Like

This line is written incorrectly, I believe it should be:

game.setGoalState(defeatScoutsGoal, true);
2 Likes

@Chaboi_3000 or @Deadpool198 when this topic closes can someone open it.

I don’t think that is needed. Once the solution is given the post will be closed automatically. It won’t need to be opened again

Yea, the solution got unmarked but it was going to close.

I just did pass the level