Thursday, December 11, 2014

Basics of Scripting: Print()

     So, before I start reading up on advanced scripting, I will tell you what I already know from previous programming experience. What I currently know should be pretty simple.
     Assuming you're already in ROBLOX Studio and in edit mode in your game, the two things you ALWAYS always always want to view are the Explorer and Properties (select to view these from, well, the view tab). Another thing you want out when testing your game is the Output window. When running your game, this will display the results of every code in your game, even when a player dies. You can run the game with the "Run" button, the "Play Solo" button, and the "Start" button on the Test tab. Run will activate the game as if a player is in it, Play Solo will actually put a player in the game (to test things that affect the player), and the Start button will put as many players into the game as you like (it will open a new RS for each player you choose, and an additional one that is an overview of the game as your players are being used as lab rats). The scripts in this article do not involve players, so you can simply click run and watch things work.
     Alright, let's finally do something involving code. In your Explorer window, you will see a bunch of items in a list (Workspace, Lighting, Players, etc.). We will only worry about the workspace. The workspace is every single item in the game and will be listed inside it once something is put in your game. Right click on this "Workspace" bar and mouse over Insert Object in the window that I hope you know should appear. A very long list will pop up, and buried in it is something called a Script, which you need to insert (by clicking it, of course. If you can't find it, be wary that the objects are listed in alphabetical order). The script will then appear inside the Workspace. Double-click on it and an entirely different RS window will appear, with numbers on the side and at the top, it says "print ' hello world! '." I am fairly certain that's for an outdated version of Lua, but just ignore it anyway. Erase it, y'know. The numbers on the side show how many lines there are. In the traditional script, there are only 2 (line 2 is blank). This is helpful because if there's an error, the output window will tell you what line it happened in and a very complicated description of what went wrong.
     We're going to rewrite the code that appeared. The line used the print() function, a command that will display text only in the output window. It doesn't sound very useful because the public won't see it, but it can be unbelievably helpful, because sometimes the output doesn't detect an error but things don't go how you planned. You can use print() to display text to tell whether or not something worked. Moving on, the parenthesis in functions are crucial, because whatever you type inside of them elaborates their job. Between the parenthesis, type, "Hello world!" (quotation marks included). Then run the game and see what happens in the output.
   
     Yeah, you will see a lot of stuff appear in the Output window. But the most important thing is that there is "Hello world!" buried in there. Actually, you might see it twice, because a lot of stuff happens in the Output from the start page and on. I'm not entirely sure if it appears twice when you run the game, but it is in there a 2nd time.
     *Cough* Moving on, I'd like you to know that beginning programming by saying "print("Hello world!")" is a programming tradition. If you want to teach someone else programming, start with that code, for it is illegal not to.
     I would explain more about this code, but I can't find a way to give a vivid description without showing you more functions first. This article is getting pretty long, so I think I'll just end right here. See you next time!