Skip to 0 minutes and 2 seconds In this video, I’ll show you how to create the personalised story that is generated using data that’s been inputted by the user. I’m going to ask the use of two pieces of information for my story. I’m going to ask them to provide their name and also, their favourite food. So the first thing I’m going to do is I’m going to create two variables to store these pieces of information.
Skip to 0 minutes and 29 seconds So I’m going to create a variable that’s called name.
Skip to 0 minutes and 34 seconds And I’m going to create another variable that’s called food.
Skip to 0 minutes and 41 seconds Now, we’re going to get the information from the user through the Ask block and that’s in the Sensing section, here.
Skip to 0 minutes and 52 seconds So let’s drag and drop this in. And if I click to test it, you can see that the dinosaur sprite asks the question– it says, what is your name? And a text box appears at the bottom for me to type my answer in. So I’m just going to type Bob, to test this out. And then you can press Return or click this tick box here. Now what’s happened is that the answer to this question has been stored in a variable over here, called answer, which is a built-in variable in Scratch. And if I click the tick to show the contents of this variable, you can see that the answer here has been sent to Bob– which is what I entered.
Skip to 1 minute and 38 seconds And if I want to store the data in this variable somewhere else, all I need to do now is to set my name variable to be the answer that was provided. So I’m going to use a set block for this. And I’m going to say that I want the name variable to be set to the value of the answer. And I can drag and drop that variable, and it fits nicely into that space there.
Skip to 2 minutes and 14 seconds So now, I’ll test it with a different name. I’m going to use Kim this time. And you can see that what’s happened is that the answer variable is holding that data that I’ve entered. And so is the name variable that I’ve programmed, too. Now I’m going to do the same with the next question. So I’m going to duplicate this code.
Skip to 2 minutes and 46 seconds And then the next question I’m going to want to find out is the user’s favourite food. So I’ll alter this text here.
Skip to 2 minutes and 58 seconds And this time, I want the food variable to be set to the user’s answer.
Skip to 3 minutes and 6 seconds And I’m going to do all of this where the green flag is clicked. So let’s test it out. And you can see that, first of all, I’m asked for my name. So I’m going to type my real name in this time. And then it asks me for my favourite food. So I’m going to use my real favourite food– cheese. And now you can see that the name variable is holding my name and the food variable is holding my favourite food.
Skip to 3 minutes and 39 seconds So I now have all the data that I need for my story and the next thing is to write the story itself. And to do that, I’m going to use a say block. And the first thing I want to say is here is your story.
Skip to 3 minutes and 55 seconds And then, for the first line of my story. I want to say there once lived a creature called and then add a name, which is the name that the user has entered. So there’s two parts to this sentence. First, it’s a phrase that once lived a creature called. And secondly, there’s the creature’s name. So to do this, I’m going to use a join block.
Skip to 4 minutes and 19 seconds And what I want to say is the initial phrase and then I want to join this onto the name that the user has entered. So I’m going to use that variable as the second part of the sentence.
Skip to 4 minutes and 40 seconds Now if I click these blocks to test them out, you’ll see that we get the story. And we get the first line. Except you can see that there was no space between the word called and my name. So I’m just going to detach this and just test it out, again. So I can change that by adding a space after the word called. And now if I clicked this block to test it, again, you’ll see that the sentence does have all the spacing in correctly now so I’ll pop it back into the same block. Now, let’s test it once more– that looks a lot better.
Skip to 5 minutes and 29 seconds And then, for the next line of my story, I want to tell the user what the character’s favourite food is. So again, I’m going to use a say block. And this time, I’m going to join another two phrases.
Skip to 5 minutes and 50 seconds And the first part of this phrase is going to be who loved.
Skip to 5 minutes and 59 seconds And then, I’m going to click on the Variables as select Food.
Skip to 6 minutes and 6 seconds And put it in the second part of the join block. So that will tell the user what the character’s favourite food is. Let’s have a look.
Skip to 6 minutes and 20 seconds And you can see that, again, I forgot to put a space in the join block after the word loved. So I’ll add that in and I’ll test it, again.
Skip to 6 minutes and 35 seconds That looks great. So now, I’m going to join the two sections of the script together. And I am going to test everything all at once now. So this time, I’m going to type the name as if.
Skip to 6 minutes and 53 seconds And the favourite food is going to be bananas. And you can see that this time, we get a slightly different story. Now, these variables at the top here on the stage are really handy to see when you’re testing the project, but we don’t need to see them in the finished project. So I’m going to hide these by unticking the boxes. And I’m also going to hide the answer variable, too. So there we have it– a very short, but interesting story.
Creating a Story
In this video you’ll learn how to create the personalised story seen in the previous example, by using variables to store data input by the user.