14

While playing screeps I can't figure out how to attack an enemy. Here's what I tried.

I created my attacker creep:

Game.spawns.Spawn1.createCreep(['attack','move'],'Attacker1');

Then when the first enemy came on the screen I tried running this command and it fails.

Game.creeps.Attacker1.attack("Player 3");

What is the correct syntax for the enemies?

Edit: Adding the link for the documentation for accessing objects in the game. http://screeps.com/docs/Creep.php

"Player 3" is the name of the enemies. I need to some how target the enemy and fight them.

J. Steen
  • 15,470
  • 15
  • 56
  • 63
parkour86
  • 193
  • 1
  • 7
  • 9
    If you don't know the game don't hate on the question! This is perfectly clear! He wants to know how to attack stuff in the game. I had the same question and it took me a lot of digging through documentation to figure it out. I am very confused as to why people think this is confusing, unclear, or otherwise a bad question. He provided example code of what he tried and asked a question. I'm not sure what else he could do to make this more clear. – dlkulp Nov 21 '14 at 07:13
  • @dlkulp "I tried running this command and it fails." is a pretty lame error description and VERY unhelpful for would be 'helpers'... don't you agree? – Paul Zahra Jul 18 '17 at 10:29
  • 3
    @PaulZahra Nope! Not when there's a clear description of the expected behavior and example code. I was pretty quickly able to see that he was using a string when it should have been an object reference! – dlkulp Jul 18 '17 at 19:01

1 Answers1

10

I'm not sure why you're getting down voted so much, you've put plenty of info on here! It looks like you're close to getting it! If you read the docs you linked to you'll see that it says attack(target) and that target is an object. Currently you're passing attack() a string, "Player 3". In order for the attack function to actually target something you need to give it an object. Try something like this:

Game.spawns.Spawn1.createCreep([Game.ATTACK, Game.MOVE],'Attacker1');
var attacker = Game.creeps.Attacker1;
var enemies= attacker.room.find(Game.HOSTILE_CREEPS);
attacker.moveTo(enemies[0]);
attacker.attack(enemies[0]);

This code:

  1. Creates a creep named Attacker1 and assigns the object to a var named attacker
  2. Uses attacker's find() function to find all enemies and assigns them to an array named enemies
  3. Moves your attacker to the first enemy in the array (.attack() only works close range)
  4. Attacks the first enemy in the array of enemies
dlkulp
  • 2,204
  • 5
  • 32
  • 46
  • This worked. How did you know to use HOSTILE_CREEPS? I don't see that in the documentation. Thanks. – parkour86 Nov 21 '14 at 06:36
  • 1
    [here](http://screeps.com/docs/Room.php) under `find(type, [opts])`. Took a bit of searching, not really sure why it's on the room page. – dlkulp Nov 21 '14 at 07:10