Qwertygiy's Scripting Tutorials #1: The Very Basic
#1
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:
  • If you first reference a variable by putting "local" in front of it, if it is inside a function or other object requiring an "end" line, it is only usable until that "end" line. If you first refer to it with just the name, it can be used in the entire script.
  • If you delete an object using :remove(), you can't bring it back into the game if you only referenced it, but if you referred to it as a variable, you can bring it back.
    [lua]
    local box = game.World.Model.Box
    box:remove()
    sleep(1)
    box.Parent = game.World.Model
    [/lua]
  • It's not a good idea at all to name a variable with a global term, like "string" or "math" or "table" or "Color" or "Vec2D". If you do this, your script will either error or not be able to use the original term.
  • If you want to be able to refer to one variable from more than one script, instead of local variablename, say _G.variablename.
    [lua]
    _G.box = game.World.Model.Box
    [/lua]
    Then, in another script, you can refer to _G.box. This is a global variable.

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.
Reply
#2
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
Reply
#3
Oh, right, I'll add that into it.

Edit: Done.
Reply
#4
i cant script cause i just started this few days ago!
Reply
#5
That was the greatest Scripting Tutorial I've seen!
Good work Qwertygiy!
Reply
#6
I didnt understand this at all. LOL. I dont script
Reply
#7
looks clean.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)