![]() |
Qwertygiy's Scripting Tutorials #1: The Very Basic - Printable Version +- 2DWorlds Forums (http://2dworlds.buildism.net/forum) +-- Forum: 2DWorlds (http://2dworlds.buildism.net/forum/forumdisplay.php?fid=4) +--- Forum: Scripting (http://2dworlds.buildism.net/forum/forumdisplay.php?fid=13) +--- Thread: Qwertygiy's Scripting Tutorials #1: The Very Basic (/showthread.php?tid=6961) |
Qwertygiy's Scripting Tutorials #1: The Very Basic - Qwertygiy - 07-22-2011 I know that there are many Buildists out there who want to make cool games, but have trouble finding their way through the Lua language. This is the first in hopefully several tutorials to help you work your way through Buildism scripting. Part One: Referencing In order to make even the most basic scripts, you need to know how to access game parts. Let's say you have a Box inside a Model, and a script inside the World. You want to be able to do something to the box from the script. To do that, you'll need to reference it. There are two ways to do it in this instance. [lua] script.Parent.Model.Box --or game.World.Model.Box [/lua] The first line works by finding what the script is inside (in this case, World), then finding the object named Model inside the script's parent, then finding the object named Box inside the Model. The second line works no matter where the script is located; it doesn't have to be inside World. It first looks in the Root (where everything in the game is located), then World, then finds the Model inside World, and then the Box inside Model. So, if we wanted to make this box red, wait a moment, then turn blue, we could use these lines: [lua] game.World.Model.Box.Color = Color(255,0,0) sleep(2) game.World.Model.Box.Color = Color(0, 0, 255) [/lua] Part Two: Variables But say we have not just one or two lines where we need to reference this Box, but many more. It gets tedious having to type in all that, especially if the Box is not located in just one Model, but in a Model inside a Model inside a Model! Fortunately, there is an easy shortcut -- local variables. With local variables, we only need to type in the path to the object once. We use a shorter term -- like box -- to refer to it. We can do this two ways. [lua] local box = game.World.Model.Box box.Color = Color(255, 0, 0) [/lua] or [lua] box = game.World.Model.Box box.Color = Color(255, 0, 0) [/lua] Here's a few simple things about variables to remember:
Part Three: Simple Functions Now, say we have a bit of code that we want to run several times. [lua] local box = game.World.Model.Box sleep(1) box.Color = Color(255, 0, 0) sleep(1) box.Color = Color(0, 255, 0) sleep(1) box.Color = Color(0, 0, 255) [/lua] We could just repeat these lines, but that takes up lots of space and is quite tedious. So, we use a function. All functions start with "function name()" and end with "end". All the stuff we want to do goes in between. To "use" the function, we use the line "name()". [lua] local box = game.World.Model.Box function ColorBox() sleep(1) box.Color = Color(255, 0, 0) sleep(1) box.Color = Color(0, 255, 0) sleep(1) box.Color = Color(0, 0, 255) end ColorBox() ColorBox() ColorBox() [/lua] That is the simplest way to do a function. But there is another way, using a function variable. Part Four: Functions with Variables "But wait", you might be thinking, "didn't we already go over variables?" Function variables are only used inside a function, not a whole script. They allow you to do the same function to different objects, or in different ways. So say we have two boxes, and we want to change their colors a bit differently, but we don't want to code in all that exactly. We can still script it into one function and a few call lines. [lua] local box = game.World.Model.Box1 local otherbox = game.World.Model.Box2 function ColorBox(part, partcolor) part.Color = partcolor end ColorBox(box, Color(255, 0, 0)) ColorBox(otherbox, Color(0, 255, 0)) ColorBox(box, Color(0, 0, 255)) [/lua] First, the part function variable refers to box, and the partcolor refers to Color(255, 0, 0) [or red]. Then, part refers to otherbox and partcolor to (0, 255, 0) [green.]. And that is how you create your own functions. Congratulations! You've completed this scripting tutorial. Now you should be able to make your own basic working Buildism scripts. RE: Qwertygiy's Scripting Tutorials #1: The Very Basic - Duck - 07-22-2011 That was a good tutorial, although I thought it was a bit weird that you only used local variables. You should explain that you can have local box = game.World.Box or box = game.World.Box RE: Qwertygiy's Scripting Tutorials #1: The Very Basic - Qwertygiy - 07-22-2011 Oh, right, I'll add that into it. Edit: Done. RE: Qwertygiy's Scripting Tutorials #1: The Very Basic - tomi123456789 - 08-03-2011 i cant script cause i just started this few days ago! RE: Qwertygiy's Scripting Tutorials #1: The Very Basic - Nice - 08-03-2011 That was the greatest Scripting Tutorial I've seen! Good work Qwertygiy! RE: Qwertygiy's Scripting Tutorials #1: The Very Basic - Anthony - 08-04-2011 I didnt understand this at all. LOL. I dont script RE: Qwertygiy's Scripting Tutorials #1: The Very Basic - Derpy-Hooves - 11-25-2011 looks clean. |