Difference between revisions of "Lua:encodeFunction"
Jump to navigation
Jump to search
(Created page with 'Category:Lua '''function''' encodeFunction(''function'', ''protect'' OPTIONAL) ''':''' string Converts a given function into an encoded string that you can pass to [[Lua:fin…') |
m (Added related function template.) |
||
| (7 intermediate revisions by 4 users not shown) | |||
| Line 1: | Line 1: | ||
[[Category:Lua]] | [[Category:Lua]] | ||
| − | '''function''' encodeFunction(''function'' | + | {{CodeBox|'''function''' encodeFunction(''function'') ''':''' string}} |
| − | Converts a given function into an encoded string | + | Converts a given Lua function into an encoded string. |
| + | |||
| + | The encoded string can later be passed to [[Lua:decodeFunction|decodeFunction]] to recreate the function. | ||
===Function Parameters=== | ===Function Parameters=== | ||
| − | {|width="85%" cellpadding="10 | + | {|width="85%" cellpadding="10%" cellspacing="0" border="0" |
!align="left"|Parameter | !align="left"|Parameter | ||
!align="left"|Type | !align="left"|Type | ||
| Line 11: | Line 13: | ||
|- | |- | ||
|function | |function | ||
| − | | | + | |Function |
| − | |The function | + | |The Lua function that should be encoded. |
|} | |} | ||
| − | == Examples == | + | ===Returns=== |
| − | < | + | String — The encoded function string. |
| − | local function | + | |
| − | print(encodeFunction( | + | ===Examples=== |
| − | </ | + | <syntaxhighlight lang="lua" line> |
| − | < | + | local encoded = encodeFunction(function() |
| − | local function | + | print("Hello from an encoded function") |
| − | local | + | end) |
| − | local | + | |
| − | + | print(encoded) | |
| − | </ | + | </syntaxhighlight> |
| + | |||
| + | <syntaxhighlight lang="lua" line> | ||
| + | local encoded = encodeFunction(function(value) | ||
| + | return value * 2 | ||
| + | end) | ||
| + | |||
| + | local decoded = decodeFunction(encoded) | ||
| + | |||
| + | print(decoded(21)) | ||
| + | </syntaxhighlight> | ||
| + | |||
| + | <syntaxhighlight lang="lua" line> | ||
| + | local function testFunction() | ||
| + | showMessage("This function was encoded and decoded") | ||
| + | end | ||
| + | |||
| + | local encoded = encodeFunction(testFunction) | ||
| + | local decoded = decodeFunction(encoded) | ||
| + | |||
| + | decoded() | ||
| + | </syntaxhighlight> | ||
{{LuaSeeAlso}} | {{LuaSeeAlso}} | ||
| − | + | {{EncodeDecode}} | |
| − | |||
Latest revision as of 00:11, 27 June 2026
Converts a given Lua function into an encoded string.
The encoded string can later be passed to decodeFunction to recreate the function.
Function Parameters[edit]
| Parameter | Type | Description |
|---|---|---|
| function | Function | The Lua function that should be encoded. |
Returns[edit]
String — The encoded function string.
Examples[edit]
1 local encoded = encodeFunction(function()
2 print("Hello from an encoded function")
3 end)
4
5 print(encoded)
1 local encoded = encodeFunction(function(value)
2 return value * 2
3 end)
4
5 local decoded = decodeFunction(encoded)
6
7 print(decoded(21))
1 local function testFunction()
2 showMessage("This function was encoded and decoded")
3 end
4
5 local encoded = encodeFunction(testFunction)
6 local decoded = decodeFunction(encoded)
7
8 decoded()