使用函數可以寫出非常漂亮的程式結構,使程式簡單化,偵錯/更改容易
這個是 function 的用法
function FUNCTION_NAME (Variable you want to put in)
...
end
|
轉換例子
本來 | 後來 | Output |
---|---|---|
print("Hi")
print("Hi")
print("Hi")
|
function printhi()
print("Hi")
end
printhi()
printhi()
printhi()
|
Hi Hi Hi |
print("Helo")
wait(2)
script.Parent.TextLabel.Text = "Helo"
print("Ko")
wait(2)
script.Parent.TextLabel.Text = "Ko"
print("Hey")
wait(2)
script.Parent.TextLabel.Text = "Hey"
print("Lol")
wait(2)
script.Parent.TextLabel.Text = "Lol"
|
function add(Name)
print(Name)
wait(2)
script.Parent.TextLabel.Text = Name
end
add(Helo)
add(Ko)
add(Hey)
add(Lol)
|
Helo
Ko Hey Lol |
print(2+3)
print(5+7)
print(6+8)
|
function add(a,b)
print(a+b)
end
add(2,3)
add(5,7)
add(6,8)
|
5 12 14 |
Return 返回
有時候你想在 function 中取得數字,就要使用return
當 return 用完後,該 function 會立刻停止運作
本來 | 後來 | output |
---|---|---|
x = 5 + 2
print(x)
|
function add(a,b)
return a + b
end
x = add(5,2)
print(x)
|
7 |
x = 5 + 2
print(x)
|
function add(a,b)
return a + b
end
print(add(5,2))
|
7 |
Recursion 遞迴
遞迴(即循環) 能令一個 function重復
例子:{| class="article-table"
|
function lockitup(location)
for i, v in pairs(location:GetChildren())do
if(v:IsA("BasePart"))then
v.Locked = true
elseif(v.ClassName=="Model")then
lockitup(v) --Scan model 裏面的 part
end
end
end
lockitup(workspace)
script:Destroy()
|這一個script可以令 workspace 的所有 part "locked" 但因為 GetDescendants () 的出現,現在已經沒有人用這個方法
詳看 GetChildren() 及 GetDescendants () |}
Variable Function
function 也可以當作一般用語
local myPrint = print
myPrint("test")
|
Output: test |
沒有名字的 function
英文:Anonymous function
詳看 事件 Event
小遊戲
1. 以下script的output是?
function hey(times)
for i=1,times do
print("Hey")
end
print("End")
end
hey(5)
hey(1)
|
答案 |
---|
Hey
Hey Hey Hey Hey End Hey End |
2. 以下script的output是?
function print_hi(a,b)
return a + b
for i=0, a do
print("Hi")
end
for i=0, b do
print("Bye")
end
end
x = print_hi(2,3)
|
答案 | 原因 |
---|---|
ERROR | return 等於 function 的最後一行, 如果沒有 end 的話有機會造成錯誤 |
3. 以下script的output是?
function sum(a)
wait()
local x = 7
x = x + sum(x)
return x
end
sum(5)
|
答案 | 原因 |
---|---|
INFINITY RECURSION | 與 while true do 效果一樣 |
4. 這是巴士目的地牌轉變程式,其根據"route.Value"來轉變目的地牌的文字,試將如下程式簡化
while true do
wait()
if script.Parent.Route.Value == 1 then
script.Parent.Front.SurfaceGui.TextLabel = "Tam Jackson"
script.Parent.Rare.SurfaceGui.TextLabel = "GD"
script.Parent.Back.SurfaceGui.TextLabel = "GD"
elseif script.Parent.Route.Value == 2 then
script.Parent.Front.SurfaceGui.TextLabel = "Ryan Law"
script.Parent.Rare.SurfaceGui.TextLabel = "DD"
script.Parent.Back.SurfaceGui.TextLabel = "DD"
elseif script.Parent.Route.Value == 3 then
script.Parent.Front.SurfaceGui.TextLabel = "Matthewcatsey"
script.Parent.Rare.SurfaceGui.TextLabel = "RMB"
script.Parent.Back.SurfaceGui.TextLabel = "RMB"
end
end
|
答案 |
---|
function changedest(Num,Front,Rare)
if script.Parent.Route.Value == Num then
script.Parent.Front.SurfaceGui.TextLabel = Front
script.Parent.Rare.SurfaceGui.TextLabel = Rare
script.Parent.Back.SurfaceGui.TextLabel = Rare
end
end
while true do
wait()
changedest(1,"Tam Jackson","GD")
changedest(2,"Ryan Law","DD")
changedest(3,"matthewcatsey","RMB")
end
|
5. 試寫 swap function, 將兩個 variable 的數值調換
local a = 5
local b = 7
a,b = swap(a,b)
print(a,b)
|
答案 |
---|
function swap(a,b)
local temp = a
a = b
b = temp
return a,b
end
local a = 5
local b = 7
a,b = swap(a,b)
print(a,b)
|
或
function swap(a,b)
return b,a
end
local a = 5
local b = 7
a,b = swap(a,b)
print(a,b)
|
學習更多
社区内容除另有注明外,均在CC-BY-SA许可协议下提供。