Engine:CCMD lua.lua edit: Difference between revisions
(25 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
{{dev/uiccmd}} | {{dev/uiccmd}} | ||
Runs a Lua statement in a Lua runtime. | Runs a Lua statement in a Lua runtime. | ||
<kua>print("fred")</kua> | <kua>lua print("fred")</kua> | ||
== Arguments == | == Arguments == | ||
* name - (none) | * name - (none) | ||
* value - the verbatim | * value - the verbatim Lua code to be run | ||
== Associations == | == Associations == | ||
Line 13: | Line 13: | ||
== Details == | == Details == | ||
Used to run a Lua text directly in the 'lua' context, if the first statement is not engine command, variable or alias in the 'lua' context. This allows the script in the 'lua' context to act as though it were running directly in a Lua console. | |||
While the 'lua' context has the ability to hold variables and aliases, they cannot be created from script as the 'lua' context has no 'alias', 'set' or 'var' type script commands added to the context. | While the 'lua' context has the ability to hold variables and aliases, they cannot be created from script as the 'lua' context has no 'alias', 'set' or 'var' type script commands added to the context. | ||
; Line by line processing: | |||
The 'lua_edit' default command will only be run when a statement is not another context, or a command, variable, or alias. Script in the 'lua' context is still run statement by statement, as script in the engine. | |||
For example: | |||
<kua> | |||
context lua | |||
print(1+2); exit | |||
</kua> | |||
Is actually: | |||
<kua> | |||
context lua | |||
print(1+2) -- sent to Lua runtime | |||
; // script statement delimiter processed in script | |||
exit // script | |||
</kua> | |||
This however would run ';' delimited statements in one Lua chunk, using the 'lua' context 'run' command. And then still run the script 'exit' command: | |||
<kua> | |||
context lua | |||
run `print(1+2);print(1+2)`;exit | |||
</kua> | |||
And this fails because 'if', 'print', and 'end' are in separate script statements: | |||
<kua> | |||
context lua | |||
if bob then -- Lua errer! | |||
print(1 + 2) | |||
end | |||
</kua> | |||
These work: | |||
<kua> | |||
context lua | |||
run `if bob then | |||
print(1 + 3) | |||
end` | |||
if bob then print(1 + 4) end | |||
</kua> | |||
; Outer return statement: | |||
If the outer scope of the Lua code calls return, the return values will be printed. For example: <kua> | |||
context lua | |||
a = 1 | |||
return a | |||
return 100 + 1000 | |||
</kua> Will write '1' and '1100' to the engine log/console. | |||
This is similar to an interactive Lua console's '=' convenience command like <code>=100</code>, where will output '100' in the console when '=' starts a console line, after the command is run. In the engine real Lua interactive console, and this mechanism is special to only 'lua_edit', and is provided for similar convenience. | |||
== Examples == | == Examples == | ||
Line 52: | Line 98: | ||
> fred | > fred | ||
</kua> | </kua> | ||
=== | === Inline scripting === | ||
Lua and JS from script: | |||
<kua> | <kua> | ||
var bob fred | var bob fred | ||
lua print(1 + 2) | lua print(1 + 2) | ||
js print(1 + 3) | js print(1 + 3) | ||
bob | |||
context lua | context lua | ||
print(1 + 4) | print(1 + 4) | ||
Line 63: | Line 110: | ||
exit | exit | ||
context js | context js | ||
print(1 + 5) | print(1 + 5); | ||
script 'bob alan;bob' | script('bob alan;bob'); | ||
exit | exit | ||
> 3 | > 3 | ||
Line 72: | Line 119: | ||
> james | > james | ||
> 6 | > 6 | ||
> alan | |||
</kua> | |||
With blocks: | |||
<kua> | |||
var bob fred | |||
bob | |||
lua run ` | |||
local s = script | |||
print(1 + 4) | |||
s 'bob james;bob' | |||
s 'js function alan(s) { script("bob "+s+";bob"); }' | |||
` | |||
js alan("alan"); | |||
> fred | |||
> 5 | |||
> james | |||
> alan | > alan | ||
</kua> | </kua> |
Latest revision as of 21:57, 18 October 2023
Runs a Lua statement in a Lua runtime.
lua print("fred")
Arguments[edit]
- name - (none)
- value - the verbatim Lua code to be run
Associations[edit]
- Is by default placed in the 'lua' context.
- Is the default command for the 'lua' context.
- Does not have its own command name in 'lua'.
Details[edit]
Used to run a Lua text directly in the 'lua' context, if the first statement is not engine command, variable or alias in the 'lua' context. This allows the script in the 'lua' context to act as though it were running directly in a Lua console.
While the 'lua' context has the ability to hold variables and aliases, they cannot be created from script as the 'lua' context has no 'alias', 'set' or 'var' type script commands added to the context.
- Line by line processing
The 'lua_edit' default command will only be run when a statement is not another context, or a command, variable, or alias. Script in the 'lua' context is still run statement by statement, as script in the engine.
For example:
context lua
print(1+2); exit
Is actually:
context lua
print(1+2) -- sent to Lua runtime
; // script statement delimiter processed in script
exit // script
This however would run ';' delimited statements in one Lua chunk, using the 'lua' context 'run' command. And then still run the script 'exit' command:
context lua
run `print(1+2);print(1+2)`;exit
And this fails because 'if', 'print', and 'end' are in separate script statements:
context lua
if bob then -- Lua errer!
print(1 + 2)
end
These work:
context lua
run `if bob then
print(1 + 3)
end`
if bob then print(1 + 4) end
- Outer return statement
If the outer scope of the Lua code calls return, the return values will be printed. For example:
context lua
a = 1
return a
return 100 + 1000
Will write '1' and '1100' to the engine log/console.
This is similar to an interactive Lua console's '=' convenience command like =100
, where will output '100' in the console when '=' starts a console line, after the command is run. In the engine real Lua interactive console, and this mechanism is special to only 'lua_edit', and is provided for similar convenience.
Examples[edit]
Default values[edit]
Below, 'exit' is actually a 'lua' context command, which simply switches to the 'global' context. It is not run in a Lua runtime.
context lua
print(1 + 2)
exit
- Prints '3'.
Dual inline scripting[edit]
Run Lua from script:
lua print(1 + 2)
> 3
Run script from lua:
var bob fred
context lua
script 'bob'
> fred
exit
Together fluidly:
lua print(1 + 2)
> 3
bob
> fred
context lua
print(1 + 2)
> 3
script 'bob'
> fred
Inline scripting[edit]
Lua and JS from script:
var bob fred
lua print(1 + 2)
js print(1 + 3)
bob
context lua
print(1 + 4)
script 'bob james;bob'
exit
context js
print(1 + 5);
script('bob alan;bob');
exit
> 3
> 4
> fred
> 5
> james
> 6
> alan
With blocks:
var bob fred
bob
lua run `
local s = script
print(1 + 4)
s 'bob james;bob'
s 'js function alan(s) { script("bob "+s+";bob"); }'
`
js alan("alan");
> fred
> 5
> james
> alan
Notes[edit]
- While 'lua_edit' is intended make console Lua commands easy to run, can allow scripts and other types of macros to avoid lots of situations where complex logic would otherwise be required, by simply switching to the 'lua' context and then running a series of single line lua chunks.