Hello and welcome to the forums! First of all, please paste your code between 3 backticks (`) according to the FAQ so we can clearly ready it.
First of all:
It means that you define (create) the variable with a value of null
(“empty”). If you check this variable now (if (farthest)
), it will return false
as long as the value is null
. As soon as you give it another value, the check will return true
because the variable is not “empty” anymore.
Otherwise, your code seems to be correct. To help you understand it better, here are some comments in pseudo-code:
loop { // repeat the below instructions "forever"
/*
* define variables
*/
while { // go through the list of enemies
if { // if the current enemy is farther than 'maxDistance'
/*
* (note: 'maxDistance' is originally zero)
* then store its distance in 'maxDistance'
* and make this enemy the 'farthest'
*/
} // end if
} // end while
/*
* Note: at the end of the 'while' loop, when you went through all the enemies
* 'farthest' will contain the name of the enemy that is the farthest from you
*/
if (farthest) { // if 'farthest' enemy exists (so it's not 'null')
// (note: there will be a 'farthest' enemy as long as there are enemies)
while (farthest.health > 0) { // as long as it's alive
// attack it
} // end while
} // end if
} // end loop
I hope this helps