olistic / warriorjs

🏰 An exciting game of programming and Artificial Intelligence
https://warriorjs.com
MIT License
9.43k stars 490 forks source link

How to get an unit? #125

Closed 383366204 closed 6 years ago

383366204 commented 6 years ago

i read you documentation --> unit.isFriendly() so I wrote this:

let unit = warrior.feel().getUnit();
    if(unit.isFriendly()){
      warrior.rescue();
    }

but the terminal show: Invalid submitted code: Cannot read property 'isFriendly' of undefined

My English is not good , can you tell me how to get the unit. thanks

olistic commented 6 years ago

Hello @383366204! warrior.feel() returns a space, so it's correct calling .getUnit() on it. However, not all spaces have units on them. You should check if there is a unit before trying to call methods like .isFriendly() on them.

const unit = warrior.feel().getUnit();
if (unit && unit.isFriendly()) {
  warrior.rescue();
}

// OR:

const space = warrior.feel();
if (space.isUnit() && space.getUnit().isFriendly()) {
  warrior.rescue();
}

P.S.: When reporting a problem, please try to follow the issue template and provide the data requested in it.

r-i-c-h commented 6 years ago

I think, essentially - The docs of the 'APIs' provide no clear idea of these two relationships to the player/warrior const unitAPIObject = warrior.feel().getUnit();
const spaceAPIObject = warrior.feel();

It would be even clearer to just put in the instructions that const space = warrior.feel();
const unit = space.getUnit();

With the inability to log anything to the terminal, it is almost impossible to intuit this.

netzulo commented 6 years ago

what's wrong with people ? P.S.: When reporting a problem, please try to follow the issue template and provide the data requested in it. , love issue_templates, why not using ? xDDDD @383366204 @olistic

olistic commented 6 years ago

@r-i-c-h I'll see what I can do to make that clearer.

With the inability to log anything to the terminal, it is almost impossible to intuit this.

This should be possible with warrior.think('<your message here>') starting with v0.3.0.

olistic commented 6 years ago

The Space API and Unit API docs are now enhanced with @r-i-c-h's suggestions.