Paying bits for a script teacher
#7
I have almost 1,800 of them already, which is probably more than anyone else. And I really have no use for them, because I generally make my own character bits.

Anyway, back to script stuff.

Being able to change stuff is handy, but if you're changing the same thing more than once, having to write out the full path every time you do so can get unnecessarily tedious. That's where variables come in.

In some languages, you have to say what type of variable you're making when you first write it. In Lua, fortunately, it's simpler than that. You just come up with a name, and then set it.

[lua]
me = game.World.Username
me.MaximumHealth = 1000
me.Health = me.MaximumHealth
[/lua]

Make sure you define your variable before you try to do anything else to it. For example, removing the first line from the code above so that you have this code will give you an error that "me" does not exist:

[lua]
me.MaximumHealth = 1000
me.Health = me.MaximumHealth
[/lua]

There are 3 special types of variables; local, global, and function parameters; but those can come later.

So now you should know how to declare a variable to an existing object, or property of one. There are other uses for variables, though. You can declare variables of data types that originate inside the script:

[lua]
--boolean values: true or false
bool = true

--number values: Lua can do math for you
--(+ is addition, - is subtraction, * is multiplication, / is division,
--and parenthesis and order of operations apply normally)
ten = 6 + 6 - 2

--strings: not yarn or twine, but lines of text
myname = "Qwertygiy"

--two special Buildism data types:
--Colors are r[ed], g[reen], b[lue] and values range from 0 (none) to 255 (full);
--Vec2Ds are x, y
white = Color(255, 255, 255)
origin = Vec2D(0, 0)

--you can read those parts of Colors and Vec2Ds on their own,
--but you can't write them separately; you need to use the constructors and change the whole value,
--like I did above.
redness = white.r
leftright = origin.x

--and of course, objects! You can create most object types like this,
--just make sure to change the Parent after setting all other properties you're changing
circle = create("Circle")
circle.Name = myname
circle.Radius = ten
circle.CharacterCollide = bool
circle.Color = white
circle.Position = origin
circle.Parent = game.World
[/lua]
Reply


Messages In This Thread
Paying bits for a script teacher - by Invader - 09-04-2012, 02:15 AM

Forum Jump:


Users browsing this thread: 7 Guest(s)