Game Development Reference
In-Depth Information
if theItemsArray[i] then count=count+1 end
end
return count
end
Now we can call this function to check if the player is ready or not, as follows:
if isPlayerReady(items)==5 then
print("The king blesses you on your journey to battle the dragon")
else
print("You are still not ready for battling the dragon")
end
By having an array, we not only reduce the number of variables required, but also make it easier to
work with while saving a list of items; in other words, if the number of items that you could collect
were 100, it would be silly to have item1 through item100 . Creating arrays to hold those values is
easier.
Another common question that a lot of new developers ask is, How can I create dynamic variables?
The answer is to use arrays instead; they are the equivalent to the dynamic variables in the context
that they were to be used.
Using Math to Loop
The safest loops to use in Lua are for loops. There are other ways to use loops, but some of them
can cause Lua to hang if not used properly.
local result=false
local value=0
while result == false do
print("The value is ", value)
value=value+1
if value>=5 then
result=true
end
end
When we reach a value of 5 or greater, we simply set the result to true , which does not evaluate the
condition result==false as being true , and hence breaks the loop. It is a good practice to have a
flag to exit a while loop, but if you ever face a situation where you need to have a while true do
loop, you can use the break statement to exit the loop.
local value=0
while true do
print("The value is ", value)
value=value+1
if value>= 5 then break end
end
 
Search WWH ::




Custom Search