LUA patches for levels in Sarven Desert Campaign

Sarven Savior

Introduction:

An array is a list of items.

-- An array of strings:
local friendNames = {'Joan', 'Ronan', 'Nikita', 'Augustus'}

Overview:

Arrays are ordered lists of data. In this level, you have an array storing the string names of your four friends.

In order to save your friends, you’ll need to tell each one of them to return home in turn. You can use the provided sample code to loop over an array.

-- friendNames is an array.
local friendNames = {'Joan', 'Ronan', 'Nikita', 'Augustus'}

--You can access specific elements of an array using a number surrounded by square brackets:
local name = friendNames[1]

-- Causes the hero to say: "Joan"
hero:say(name)

You can use an index variable instead of a number, to access an element of the array.

To loop over all the values in an array, use a while-loop to increment the index variable each loop!

On each pass through the loop, you’ll retrieve the friend name at that array index, then tell the friend to go home.

local friendNames = {'Joan', 'Ronan', 'Nikita', 'Augustus'}

-- Arrays start at index 1
local friendIndex = 1

-- #friendNames gives you the length of the friendNames array.
-- The length is equal to the number of items in the array (4, in this case)

while friendIndex <= #friendNames do
    local friendName = friendNames[friendIndex]
    hero:say(friendName .. ', go home!')
    friendIndex = friendIndex + 1
end
-- This while-loop will execute using friendIndex 1, then 2, 3, and 4
-- Note that in LUA the length of the array and the last element is 4!
1 Like