Uncertainty is fun!
Life is uncertain. Most of us try to forget this as we go about our daily business. But life without uncertainty is tedious and large school of orange fish. SEE! HA! By adding that bit about the fish, I added some randomness to your day, filling it with joy and delight!
Okay, that might be overstating things, but the point remains, however tenuous it may be. I'm a game developer and although the maths for games doesn't have to be complicated, pretty much everything is controlled by numbers. And it's useful to be able to come up with random numbers. Let's see an example: max = 100 r = math.random(max) print("I've picked a number between 1 and 100. It is:" .. r)
The result from this will be a some text and a number from 1 to 100. As I've shown you before, you can compact all that code down to one line if you want, but I'll leave that as an exercise for you folks.
This code may be used this to make a simple dice-roller if you want. All you'd do is swap that 100 for a 6. Woo! Useful! One important thing to notice is that there's an entire library of mathematical functions that do cool things like this held under the 'math.' name. You don't need to know them all, and will probably only use a couple but math.random()is one of the more useful ones. Arrays
Arrays are where the fun with coding really begins. You've previously seen how you can get sections of code to run again and again using 'loops' like for i = 1, 5 do, but arrays are where this starts to show its usefulness.
So what are arrays? Arrays are just like other variables: shoeboxes containing data. In the case of arrays, they are shoeboxes that can contain yet more shoe-boxes. They look a little different from variables in how we create them, but that doesn't mean they are scary. If I put a 2 small shoe boxes inside a large shoebox you'd be just fine. With a simple variable we just say a = 5 to make a piece of data with the label 'a' contain the value 5: fill shoe box 'a' with 5. Easy. With an array, it's simply a collection of shoeboxes with a label of its own, like 'the shoe boxes in my cupboard'. We might have more in the garage, or under the bed. Apparently we have a problem with shoes. Because we now want to name a collection as well as individual shoe boxes, putting data into the whole thing looks a bit different. We first have to say 'Hey! This data is going to be an array, and will contain more labels!' In addition, when we start putting in values, we need to say 'which shoebox in the cupboard we want to put the value in'. The format for creating an array is like this: cupboard= {} This means 'make an array called cupboard'. It's empty to begin with. cupboard[5] = "thingy" This means 'put the word "thingy" into the shoe box in the cupboard with the label '5'. The other shoe boxes are still empty! Let's take a look at how this plays out in actual code: t = {} -- Let's make an array named 't' t[1] = 5 -- Let's put a '5' into 't' at position 1 t[2] = 10 -- Let's put a '10' into 't' at position 2 print(t[1])
This prints out '5'. This is because we've put two numbers into the table: '5' and '10' at positions 1 and 2.
If I were to try print(t[3]) I'd get nothing, because there's nothing at position 3! Note the lines after the -- are called 'comments'. They do nothing but remind me what things are for. You can use them to write notes for yourself if you want. Any time the computer sees -- it will ignore everything afterward. Arrays are incredibly flexible, with multiple ways to set them up and the ability to hold anything at all, including functions and other tables! But why is this useful, and why did I bring up the whole 'random()' thing to begin with? Let's expand this a bit and see! friends = {} -- This tells the computer that 'friends' -- is going to be an array, not a number or 'string' friends[1] = "John" friends[2] = "Mary" friends[3] = "Kate" friends[4] = "Vlad" friends[5] = "Mimsy" friends[6] = "Kbun'daar the third" friends[7] = "Squiggles" friends[8] = "Spamford" friends[9] = "Mr Quackers" friends[10] = "Dickwings" -- print a line to give the data some context print("My friends are:\n") -- Now print out all my friends for i = 1, 10 do print(friends[i] .. "\n") end -- And pick out a best friend for me! r = math.random(10) print("But my best friends is " .. friends[r])
This will print out a list of all your friends, and then pick a best friend for you at random. If it's Vlad or Dickwings then you have my sympathies. Also, note that you already knew what this code did because of my comments, the things I put after --. This is good practice, so make sure you do it yourself.
But... still why?
Some of you will still be wondering how any of this is useful to anyone. Well, at some point you've played a game. Scrabble? Tic-Tac-Toe? Those games have grids which are held in an array like this. Minecraft? Its entire world is also held in an array like this. Every spreadsheet or word-processing application holds its data in a form like this. Arrays are everywhere.
An equivalent exists in pretty much every other language you might choose to learn. Computers are good at working on large sets of data very quickly, and arrays like these are the simplest way to do that. Alternatives
The way we created our friend list was pretty long-winded. There are alternatives! We could have written the friend array definition like this:
friends = {"John", "Mary", "Kate", "Vlad", "Mimsy", "Kbun'daar the third", "Squiggles", "Spamford", "Mr Quackers", "Dickwings"} print("My friends are:\n") -- Now print out all my friends for i = 1, 10 do print(friends[i] .. "\n") end -- And pick out a best friend for me! r = math.random(10) print("But my best friends is " .. friends[r])
This is exactly the same as the former version, apart from the way we set up the 'friends' array. In this case, the computer just assumes we want the labels to be numbers starting at 1 and ending at 10. Annoyingly, this means we have to count how many names we have in the array (10) so we can type in all those 10s in the rest of the code!
This is clearly rubbish. If we decide that Vlad is an ass, we might want to remove him and then we'd have to rewrite all the code! Ugh. The creators of Lua know this, so they gave us a gift: the '#' sign. friends = {"John", "Mary", "Kate", "Vlad", "Mimsy", "Kbun'daar the third", "Squiggles", "Spamford", "Mr Quackers", "Dickwings"} boink = #friends -- count how many things are in the 'friends' array -- store it in the variable 'boink ' for later print("My friends are:\n") -- Now print out all my friends for i = 1, boink do print(friends[i] .. "\n") end -- And pick out a best friend for me! r = math.random(boink) print("But my best friends is " .. friends[r])
When you have an array where all the 'labels' count up from 1 without skipping, you can use #array_name to ask 'how long is this table?' and it will put the answer in the variable you pick. Here we put it in 'boink'.
If we'd destroyed data by filling a container with 'nil' (which would look like friend[4] = nil -- bye Vlad!) this would no longer work! There'd be a gap between friend[3] and friend[5] so the computer would count until it hit that gap and then give you the answer '4'! Aren't computers silly? That was the hump!
This is the hard bit. Arrays and their more general form, the table, are the most complex part of Lua... as well as the most powerful. Today you learned about arrays, and how flow code allows you to process a lot of data with only a few lines (in this case using a simple numerical 'for' loop).
Next time we'll look at how arrays are a specific form of table, and what else we can do with them. Some words will be changed around a bit and it'll be all kinds of exciting. As ever, you can try out this code using this site. Just cut and paste it and play around with things. Get used to breaking code, figuring out what doesn't work and feel free to contact me if you still don't understand!
0 Comments
Leave a Reply. |
AuthorFluttermind’s director, Dene Carter, is a games industry veteran of over 25 years, and co-founder of Big Blue Box Studios, creators of the Fable franchise for the XBox and XBox 360. Archives
April 2022
Categories |