Загрузка страницы

Roblox How to Script for Beginners | #3 | Functions, Returning, Methods

In this video, we are going to cover functions, returning, and methods.
What is function?
A function is a block of reusable code.
Functions in Lua
In Lua, functions are defined using the keyword function and the keyword end. Just like with variables, you will typically want to use the local keyword when defining functions. This is related to scoping, and I’ll make a video on that later on in this series.
There are three main parts in a function.
Name- Not all functions have names- functions that don’t are called anonymous functions. Technically no functions in Lua have names, but that's a weird technicality that I will talk about at the end of the video.
Body- this contains the code. The body is indented so there is a tab before each line in the body. That isn’t necessary but it is useful because it makes the code easier to read. You could actually write a whole function in a single line, but I usually wouldn’t recommend that because that is harder to read.
Parameters/Arguments- Not all functions have parameters. You can have as many or few as you want. Parameters go between the parentheses, and are used to pass additional information to the function. Parameters work s local variables within the function.
Function Definition
Defining a function only tells the computer that we want to be able to call the function with the given name to execute the code inside.
Function Execution
The function does not execute until you tell it to. By that I mean the code doesn’t run just by typing out a definition. In order to execute your function, you need to type its name with parentheses afterward- this is known as calling the function.
Something I didn’t really cover before is that scripts just execute once, and they do so from the beginning to the end. Because of this, we can’t call the function until it is defined. So if you write the function call above the function definition, you will have an error which is “attempt to call a nil value.”
Why would you want to use a function?
Say I want to make a series of changes to 3 different parts. I want to make each part a random size and a random color. You could repeat this process three times; however, it is more efficient if you write a function that takes a part as a parameter then makes the changes.
Returns
If you want to get information back from your function, a return would be useful. Functions can return or send back data to the calling command. If you return information, you will want to capture the returned information in a variable or use it in place where you call the function. Also, it is important to note that the keyword return needs to be at the end of your function if you use it. Anything after "return" will not be executed because your function will terminate immediately as it returns.
Methods
Methods are functions stored within an object. For example the Destroy() method is a function stored within Parts. Because methods are stored within objects, you need to use the dot syntax or colon syntax to access them. Typically the colon syntax is used, i.e. part:Destroy(). Another way to do this is part.Destroy(part). That is because the colon automatically passes the object before to the function as the first parameter.
Anonymous Functions
Anonymous functions are just functions without names. You will often see them when people use event triggered functions like when using the Touched event. I have a full tutorial on Touched events on my channel if you are interested.
As I hinted early in the video, technically all functions in Lua are anonymous. When we are naming a function, we are really just naming a variable and setting it equal to an anonymous function.

Intro: 0:00
Overview: 0:06
What is a Function?: 0:09
Function in Lua: 0:13
3 Parts of Function: 0:53
Function Name: 01:04
Function Body: 01:32
Function Parameters: 01:43
Function Definition: 02:18
Function Call: 02:33
Multiple Parameters: 03:15
Script Execution Order: 03:53
Why use a function?: 04:22
Function Demo: 06:42
How to Return: 08:29
Methods: 12:18
Anonymous Functions: 13:18
Outro: 14:32

Видео Roblox How to Script for Beginners | #3 | Functions, Returning, Methods канала CyberCreator
Показать
Комментарии отсутствуют
Введите заголовок:

Введите адрес ссылки:

Введите адрес видео с YouTube:

Зарегистрируйтесь или войдите с
Информация о видео
11 октября 2020 г. 7:15:37
00:14:43
Яндекс.Метрика