How do I attack an enemy in Screeps

7.3k views Asked by At

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.

1

There are 1 answers

2
dlkulp On BEST ANSWER

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