API Basic Callbacks ko - LIHACHTETAN/ClassicUO-BadNewbie-BasicIDE GitHub Wiki
AddressOf / callbacks
ClassicUO โข Basic
AddressOf๋ ์คํฌ๋ฆฝํธ์ Sub ๋๋ Function ์ฐธ์กฐ๋ฅผ ๊ฐ์ ธ์ค๋ฉฐ ์ฆ์ ํธ์ถํ์ง ์์ต๋๋ค. ์ฐธ์กฐ๋ฅผ ์ธ์๋ก ์ ๋ฌํ๊ฑฐ๋ ํจ์์์ ๋ฐํํ๊ณ ์ปฌ๋ ์ ์ ์ ์ฅํ์ฌ ์ฒ๋ฆฌ ๊ท์น์ ์ ํํ ์ ์์ต๋๋ค.
์ ํํ ๊ตฌ๋ฌธ
Dim callback = AddressOf ProcedureName
Dim callback As Object = AddressOf Tools.FunctionName
callback(arguments)
callback.Invoke(arguments)
Process(values, AddressOf Predicate)
๋งค๊ฐ๋ณ์
ProcedureNameโ ์ ์ธ๋ ํ๋ก์์ ์ด๋ฆ์ด๋ฉฐ ํ์ํ๋ฉด Module ์ด๋ฆ์ ๋ถ์ ๋๋ค. ์ผ์นํ๋ ์ ์ธ์ ํ๋์ฌ์ผ ํฉ๋๋ค. ์ ์ ์๋ ์ด๋ฆ, ์ ๊ทผํ ์ ์๋ Private, ์ค๋ฒ๋ก๋ ์ด๋ฆ์ ์คํ ์ ์ ๊ฑฐ๋ถํฉ๋๋ค. Basic ๋ด์ฅ ํจ์๋ UO ๋ช ๋ น์ ๊ณ ์ ํ ์ด๋ฆ์ ์คํฌ๋ฆฝํธ ๋ํผ๋ฅผ ๋ง๋ ๋ค ๊ทธ AddressOf๋ฅผ ์ฌ์ฉํฉ๋๋ค. ์ด๋ฆ ๋ค์ ๊ดํธ๋ฅผ ๋ถ์ด์ง ์์ต๋๋ค.callback / argumentsโ callback์ Object ๊ฐ์ ๋๋ค. callback(...)๊ณผ callback.Invoke(...)๋ ํ์ฌ ์คํฌ๋ฆฝํธ ์ค๋ ๋์์ ๋๊ธฐ ํธ์ถํฉ๋๋ค. ๋ช ๋ช ๋ ์ธ์๋ ์ค์ ๋งค๊ฐ๋ณ์ ์ด๋ฆ์ ์ฌ์ฉํฉ๋๋ค. ์ปฌ๋ ์ ์์๋ฅผ ๋จผ์ ๋ณ์์ ์ ์ฅํ ๋ค ํธ์ถํ์ธ์. ์ธ์ ๊ณ์ฐ ์ ์ ์ฐธ์กฐ๋ฅผ ๊ณ ์ ํ๋ฏ๋ก ์ธ์๊ฐ callback์ ๋ฐ๊พธ๋๋ผ๋ ์ด๋ฒ ํธ์ถ ๋์์ ์ ์ง๋ฉ๋๋ค.ByRef / ByVal / Optional / ParamArrayโ ByVal์ ๊ฐ์ด๋ ์ฐธ์กฐ๋ฅผ ๋ณต์ฌํ๊ณ ByRef๋ ๋ณ๊ฒฝ์ ๋ค์ ๊ธฐ๋กํฉ๋๋ค. Optional์ ์๋ตํ ๊ธฐ๋ณธ๊ฐ์ ๊ณ์ฐํ๊ณ ParamArray๋ ์์น ์ธ์๋ฅผ ๋ชจ์๋๋ค. ๋ช ๋ช ๋ ์ธ์๋ ์์น ์ธ์ ๋ค์ ์๋๋ค. ParamArray ๊ฐ์ ์์น ์ธ์๋ง ์ฌ์ฉํ๋ ํธ์ถ์ด ํ์ํฉ๋๋ค. ์๋ชป๋ ์ด๋ฆ์ด๋ ๊ฐ์๋ ์ธ์ ๋ถ์์ฉ ์ ์ ์ค๋ฅ๊ฐ ๋ฉ๋๋ค.
๋ฐํ๊ฐ
AddressOf๋ Object ์ฐธ์กฐ๋ฅผ ๋ฐํํฉ๋๋ค. ID, ๋ฉ๋ชจ๋ฆฌ ์ฃผ์, Boolean ๋๋ ํจ์ ์คํ ๊ฒฐ๊ณผ๊ฐ ์๋๋๋ค. Function ํธ์ถ์ ํจ์ ๊ฒฐ๊ณผ๋ฅผ ๋ฐํํ๋ฉฐ Return ์์ด ์๋ Sub๋ ๊ฐ์ ๋ฐํํ์ง ์์ต๋๋ค(Unit). IsPositive๋ 1/True ๋๋ 0/False๋ฅผ ๋ฐํํฉ๋๋ค. ์์ 1, 3์ Main์ String, ์์ 2๋ Integer 15๋ฅผ ๋ฐํํฉ๋๋ค.
๋์
- ์ฐธ์กฐ๋ ์์ฑ ํจ์์ ์ง์ญ ๋ณ์๋ฅผ ์บก์ฒํ์ง ์์ต๋๋ค. ๋๋ค๋ ํด๋ก์ ๊ฐ ์๋๋๋ค. ๋ก๋๋ ์คํฌ๋ฆฝํธ์ ์ํ๋ฏ๋ก ๋ค๋ฅธ ๋ฐํ์์ด๋ ๋ค์ ๋ก๋ํ ์คํฌ๋ฆฝํธ์์๋ ์ด์ ์ฐธ์กฐ๋ฅผ ํธ์ถํ ์ ์์ต๋๋ค. Public ํฉํฐ๋ฆฌ๋ ์์ ์ Private ๋์ฐ๋ฏธ ์ฐธ์กฐ๋ฅผ ์๋์ ์ผ๋ก ๋ฐํํ ์ ์์ต๋๋ค.
- ์ค๋น ๋จ๊ณ์์ ์ด๋ฆ๊ณผ ์ ๊ทผ ๊ถํ์ ๊ฒ์ฌํฉ๋๋ค. ์ธํฐํ๋ฆฌํฐ๋ AddressOf ์์น๋ง๋ค ๋ถ๋ณ ์ฐธ์กฐ๋ฅผ ์บ์ํฉ๋๋ค. ํธ์ถ๋ง๋ค ํ์ฌ ๋ณ์๋ฅผ ์ฝ๊ณ ์๊ทธ๋์ฒ๋ฅผ ํ์ธํ ๋ค ์ธ์๋ฅผ ์์ฑ ์์๋๋ก ํ ๋ฒ์ฉ ๊ณ์ฐํ์ฌ ์ผ๋ฐ ํ๋ก์์ ํ๋ ์์ผ๋ก ๋ค์ด๊ฐ๋๋ค. ByRef์ ์์ธ ์ฒ๋ฆฌ๋ ์ง์ ํธ์ถ๊ณผ ๊ฐ์ต๋๋ค.
- ์ค๋ ๋๋ ํ์ด๋จธ๋ฅผ ๋ง๋ค์ง ์์ต๋๋ค. ์ผ์ ์ ์ง์ ์ทจ์๋ ์ผ๋ฐ ๊ฒ์ฌ ์ง์ ์ ์ฌ์ฉํ๋ฉฐ ์ฝ๋ฐฑ ์์ ๋ฐ๋ณต๋ฌธ์๋ ์ ์ฉ๋ฉ๋๋ค. ์ค๋ฅ๋ ํธ์ถ์์ Catch/Finally๋ก ์ ๋ฌ๋ฉ๋๋ค. Catch๊ฐ ๋น์ ์ ์ง๋ฅผ ์ผํค์ง ์์ต๋๋ค. ์ฐจ๋จ๋๋ ๋ค์ดํฐ๋ธ ํธ์ถ์๋ ๊ณ ์ ํ ์ทจ์ ์ ํ์ด ๋จ์ต๋๋ค.
- ์ด ๊ธฐ๋ฅ์ Delegate ํ์ ์ ์ธ, ๋๋ค, DLL ํจ์ ํฌ์ธํฐ, ์ค๋ฒ๋ก๋ ์ฐธ์กฐ๋ฅผ ์ง์ํ์ง ์์ต๋๋ค. AddressOf์ UO.๋ฅผ ๋ถ์ด์ง ์์ผ๋ฉฐ ๋ํผ ์์ ๊ฒ์ ๋ช ๋ น์๋ UO.๋ฅผ ์ ์งํฉ๋๋ค.
- ์ค์ฒฉ ์คํฌ๋ฆฝํธ ํ๋ก์์ ํธ์ถ์ ์ฝ๋ฐฑ๊ณผ ์ด๋ฒคํธ ์ฒ๋ฆฌ๊ธฐ๋ฅผ ํฌํจํด 32ํ๋ ์์ผ๋ก ์ ํ๋ฉ๋๋ค. ์ด๊ณผํ๋ฉด ์ก์ ์ ์๋ ์ค๋ฅ๊ฐ ๋ฐ์ํ๋ฏ๋ก ๊น์ ์ฒ๋ฆฌ๋ ๋ฐ๋ณต๋ฌธ์ ์ฌ์ฉํ์ธ์. ๋ฐํ์ด๋ ์ค๋ฅ ์ ํ๋ ์์ด ํด์ ๋์ด ์ดํ ํธ์ถ์ด ๊ฐ๋ฅํฉ๋๋ค.
์์
1. ์กฐ๊ฑด ํจ์๋ก ๋ชฉ๋ก ํํฐ๋ง
# values๋ ์
๋ ฅ List์ด๊ณ predicate๋ AddressOf IsPositive์
๋๋ค. FilterValues๋ ์๋ง๋ค predicate(number)๋ฅผ ํ ๋ฒ ํธ์ถํฉ๋๋ค. ์์ 4์ 7๋ง ๋จ์ selected.Count()=2, selected[0]=4์ด๋ฏ๋ก Main์ "2:4"๋ฅผ ๋ฐํํฉ๋๋ค. ๋ ๋์ฐ๋ฏธ๋ ์คํฌ๋ฆฝํธ์ ์ ์ฒด ์ฝ๋๊ฐ ์์ผ๋ฉฐ ์ถ๊ฐ API ๋ช
๋ น์ด ์๋๋๋ค.
Option Explicit On
Function IsPositive(ByVal number) As Boolean
Return number > 0
End Function
Function FilterValues(ByVal values, ByVal predicate)
Dim result = List()
For Each number In values
If predicate(number) Then
result.Add(number)
End If
Next
Return result
End Function
Sub Main()
Dim numbers = List()
numbers.Add(-2)
numbers.Add(4)
numbers.Add(7)
Dim selected = FilterValues(numbers, AddressOf IsPositive)
Return CStr(selected.Count()) & ":" & CStr(selected[0])
End Sub
๋งค๊ฐ๋ณ์ ๋ฐ ์คํ ์ค๋ช :
values๋ ์ ๋ ฅ List์ด๊ณ predicate๋ AddressOf IsPositive์ ๋๋ค. FilterValues๋ ์๋ง๋ค predicate(number)๋ฅผ ํ ๋ฒ ํธ์ถํฉ๋๋ค. ์์ 4์ 7๋ง ๋จ์ selected.Count()=2, selected[0]=4์ด๋ฏ๋ก Main์ "2:4"๋ฅผ ๋ฐํํฉ๋๋ค. ๋ ๋์ฐ๋ฏธ๋ ์คํฌ๋ฆฝํธ์ ์ ์ฒด ์ฝ๋๊ฐ ์์ผ๋ฉฐ ์ถ๊ฐ API ๋ช ๋ น์ด ์๋๋๋ค.
2. ํธ์ถ์์ ๋ณ์ ๋ณ๊ฒฝ
# AddAmount์ total์ ByRef, amount๋ ByVal์ด๋ฉฐ ๊ธฐ๋ณธ๊ฐ์ 1์
๋๋ค. update(total)์ 10์ 11๋ก ๋ฐ๊ฟ๋๋ค. Invoke(amount:=4, total:=total)๋ ์ด๋ฆ์ผ๋ก ์ฐ๊ฒฐํ์ฌ 11์ 15๋ก ๋ฐ๊ฟ๋๋ค. ByRef๊ฐ ์๋ ๋ณ์์ ๋ค์ ๊ธฐ๋กํฉ๋๋ค. Sub๋ ๊ฐ์ ๋ฐํํ์ง ์์ผ๋ฉฐ Main์ Boolean์ด ์๋ Integer 15๋ฅผ ๋ฐํํฉ๋๋ค.
Option Explicit On
Sub AddAmount(ByRef total As Integer, Optional ByVal amount = 1)
total += amount
End Sub
Sub Main()
Dim update = AddressOf AddAmount
Dim total = 10
update(total)
update.Invoke(amount:=4, total:=total)
Return total
End Sub
๋งค๊ฐ๋ณ์ ๋ฐ ์คํ ์ค๋ช :
AddAmount์ total์ ByRef, amount๋ ByVal์ด๋ฉฐ ๊ธฐ๋ณธ๊ฐ์ 1์ ๋๋ค. update(total)์ 10์ 11๋ก ๋ฐ๊ฟ๋๋ค. Invoke(amount:=4, total:=total)๋ ์ด๋ฆ์ผ๋ก ์ฐ๊ฒฐํ์ฌ 11์ 15๋ก ๋ฐ๊ฟ๋๋ค. ByRef๊ฐ ์๋ ๋ณ์์ ๋ค์ ๊ธฐ๋กํฉ๋๋ค. Sub๋ ๊ฐ์ ๋ฐํํ์ง ์์ผ๋ฉฐ Main์ Boolean์ด ์๋ Integer 15๋ฅผ ๋ฐํํฉ๋๋ค.
3. ๋น๊ณต๊ฐ ๊ท์น๊ณผ ์ค๋ฅ ์ฒ๋ฆฌ
# Rules.Create๋ Private CheckedDouble ์ฐธ์กฐ๋ฅผ ๋ฐํํฉ๋๋ค. ์ธ๋ถ์์ AddressOf Rules.CheckedDouble์ ์ง์ ๊ฐ์ ธ์ฌ ์ ์์ต๋๋ค. operation(6)์ 12๋ฅผ ๋ฐํํฉ๋๋ค. operation(-1)์ ๋์
์ ์ "negative"๋ฅผ ๋ฐ์์์ผ result๋ 12๋ก ์ ์ง๋ฉ๋๋ค. Catch๋ ๋ฉ์์ง๋ฅผ ๋ฐ๊ณ Finally๋ ":done"์ ๋ถ์
๋๋ค. Main์ "12:negative:done"๋ฅผ ๋ฐํํฉ๋๋ค. IDE๋ฅผ ๋ซ๋ ๊ฒ๋ง์ผ๋ก ์คํฌ๋ฆฝํธ๋ ๋ฉ์ถ์ง ์์ต๋๋ค.
Option Explicit On
Module Rules
Private Function CheckedDouble(ByVal number) As Integer
If number < 0 Then
Throw "negative"
End If
Return number * 2
End Function
Public Function Create()
Return AddressOf CheckedDouble
End Function
End Module
Sub Main()
Dim operation = Rules.Create()
Dim result = operation(6)
Dim message = ""
Try
result = operation(-1)
Catch problem
message = problem
Finally
message = message & ":done"
End Try
Return CStr(result) & ":" & message
End Sub
๋งค๊ฐ๋ณ์ ๋ฐ ์คํ ์ค๋ช :
Rules.Create๋ Private CheckedDouble ์ฐธ์กฐ๋ฅผ ๋ฐํํฉ๋๋ค. ์ธ๋ถ์์ AddressOf Rules.CheckedDouble์ ์ง์ ๊ฐ์ ธ์ฌ ์ ์์ต๋๋ค. operation(6)์ 12๋ฅผ ๋ฐํํฉ๋๋ค. operation(-1)์ ๋์ ์ ์ "negative"๋ฅผ ๋ฐ์์์ผ result๋ 12๋ก ์ ์ง๋ฉ๋๋ค. Catch๋ ๋ฉ์์ง๋ฅผ ๋ฐ๊ณ Finally๋ ":done"์ ๋ถ์ ๋๋ค. Main์ "12:negative:done"๋ฅผ ๋ฐํํฉ๋๋ค. IDE๋ฅผ ๋ซ๋ ๊ฒ๋ง์ผ๋ก ์คํฌ๋ฆฝํธ๋ ๋ฉ์ถ์ง ์์ต๋๋ค.