Team Work
```lua
-- Gems will disappear soon. You ll need help!
– findItems() returns an array of items.
local items = hero:findItems()
– Get the first gem from the array.
– Dont forget that the first index is 1.
local gem0 = items[1]
– Tell Bruno to get gem0
hero:say("Bruno " … gem0)
– You can reference the gem without a variable.
hero:say("Matilda " … items[1])
– Create a variable for the last gem, items[3]:
– Move to that gems position using moveXY()
<hr>
Introduction:
`hero:findItems()` returns an array containing all the items your hero can see.
```lua
local items = hero:findItems()
local first = items[1] # The first index is 1
local second = items[2]
local third = items[3]
When you assign an item to a variable, you can work with it as you did it with the findNearestItem()
in previous levels.
Overview:
Methods like findItems
, findEnemies
and findFriends
return an array filled with items, enemies, or friends).
Elements of an array are counted starting from 0, so the first element of the array has an index of zero.
To get an element of the array use the array[n]
syntax, where n
is an index of the required element.
local enemies = hero:findEnemies()
local firstEnemy = enemies[1]
local secondEnemy = enemies[2]
Be careful about the length of the array.
If you try to read an index that is greater than or equal to the array’s length you can get an error or undefined
value,
You can assign elements of an array to a variable.
local items = hero:findItems()
local firstItem = items[1]
hero:moveXY(firstItem.pos.x, firstItem.pos.y)
You can also use array elements without assigning them to a variable:
local enemies = hero:findEnemies()
hero.attack(enemies[1])