lua:loadfunction
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
lua:loadfunction [2014/07/18 15:40] – vodur | lua:loadfunction [2014/07/18 15:42] (current) – vodur | ||
---|---|---|---|
Line 1: | Line 1: | ||
To be completed, here are some notes: | To be completed, here are some notes: | ||
- | [code] | + | There is a way to do this using loadfunction: |
- | + | ||
- | – mprog 31404 | + | <code lua> |
+ | -- mprog 31404 | ||
function a_function( arg1, arg2 ) | function a_function( arg1, arg2 ) | ||
- | + | | |
- | < | + | say(" |
- | say(" | + | return 3 + 5 |
- | </ | + | |
- | + | ||
- | < | + | |
- | say(" | + | |
- | </ | + | |
- | + | ||
- | < | + | |
- | return 3 + 5 | + | |
- | </ | + | |
end | end | ||
- | + | ||
- | – mprog 31405 | + | |
+ | -- mprog 31405 | ||
target_mob = getmobworld(31424)[1] | target_mob = getmobworld(31424)[1] | ||
- | |||
target_mob: | target_mob: | ||
- | |||
target_mob: | target_mob: | ||
- | + | | |
- | < | + | |
- | a_function( " | + | |
- | </ | + | |
end) | end) | ||
- | + | ||
- | – alternate mprog 31405 | + | |
+ | -- alternate mprog 31405 | ||
target_mob = getmobworld(31424)[1] | target_mob = getmobworld(31424)[1] | ||
- | |||
target_mob: | target_mob: | ||
- | + | | |
- | < | + | -- alternately: |
- | loadprog(31404) | + | -- alternately : mob: |
- | </ | + | a_function( " |
- | + | ||
- | - - alternately: | + | |
- | + | ||
- | - - alternately : mob: | + | |
- | + | ||
- | target_mob because this function runs in target_mob' | + | |
- | + | ||
- | < | + | |
- | a_function( " | + | |
- | </ | + | |
end) | end) | ||
- | + | ||
- | – mprog 31405 with return value | + | -- mprog 31405 with return value |
- | + | -- note, ' | |
- | – note, ' | + | |
environment | environment | ||
- | + | -- uses closure magic to move values between script envs | |
- | – uses closure magic to move values between script envs | + | |
local result | local result | ||
- | |||
target_mob = getmobworld(31424)[1] | target_mob = getmobworld(31424)[1] | ||
- | |||
target_mob: | target_mob: | ||
- | + | | |
- | < | + | result=a_function( " |
- | loadprog(31404) | + | end) |
</ | </ | ||
- | + | ||
- | < | + | |
- | result=a_function( " | + | |
- | </ | + | |
- | + | ||
- | end) | + | |
- | + | ||
- | [/code] | + | |
Not as pretty as something like target_mob: | Not as pretty as something like target_mob: | ||
- | |||
but there is no straightforward way to implement args to loadprog directly. | but there is no straightforward way to implement args to loadprog directly. | ||
- | |||
For instance, if I used the syntax above, by what name would you | For instance, if I used the syntax above, by what name would you | ||
- | |||
refer to those parameters in the 31404 script? | refer to those parameters in the 31404 script? | ||
+ | |||
+ | |||
So loadfunction is the way to do tricky stuff like this, for better or worse. | So loadfunction is the way to do tricky stuff like this, for better or worse. | ||
- | |||
I agree, it's often nice to have all the code in one place. | I agree, it's often nice to have all the code in one place. | ||
- | |||
loadfunction can't directly return a value, but you can use other tricks to | loadfunction can't directly return a value, but you can use other tricks to | ||
- | |||
get the job done: | get the job done: | ||
- | + | ||
- | [code lua] | + | <code lua> |
- | + | -- mprog 31405 with return value | |
- | – mprog 31405 with return value | + | -- uses closure magic to move values between script envs |
- | + | ||
- | – uses closure magic to move values between script envs | + | |
local result | local result | ||
- | |||
target_mob = getmobworld(31424)[1] | target_mob = getmobworld(31424)[1] | ||
- | |||
target_mob: | target_mob: | ||
- | + | | |
- | < | + | result=a_function( " |
- | loadprog(31404) | + | end) |
</ | </ | ||
- | |||
- | < | ||
- | result=a_function( " | ||
- | </ | ||
- | |||
- | end) | ||
- | |||
- | [/code] | ||
lua/loadfunction.1405698042.txt.gz · Last modified: 2014/07/18 15:40 by vodur