This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
lua:loadfunction [2014/07/18 15:40] vodur |
— (current) | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | To be completed, here are some notes: | ||
- | |||
- | [code] | ||
- | |||
- | – mprog 31404 | ||
- | |||
- | function a_function( arg1, arg2 ) | ||
- | |||
- | <code> | ||
- | say("Arg 1: "..arg1) | ||
- | </code> | ||
- | |||
- | <code> | ||
- | say("Arg 2: "..arg2) | ||
- | </code> | ||
- | |||
- | <code> | ||
- | return 3 + 5 | ||
- | </code> | ||
- | |||
- | end | ||
- | |||
- | – mprog 31405 | ||
- | |||
- | target_mob = getmobworld(31424)[1] | ||
- | |||
- | target_mob:loadprog(31404) | ||
- | |||
- | target_mob:loadfunction( function() | ||
- | |||
- | <code> | ||
- | a_function( "test1", "test2" ) | ||
- | </code> | ||
- | |||
- | end) | ||
- | |||
- | – alternate mprog 31405 | ||
- | |||
- | target_mob = getmobworld(31424)[1] | ||
- | |||
- | target_mob:loadfunction( function () | ||
- | |||
- | <code> | ||
- | loadprog(31404) | ||
- | </code> | ||
- | |||
- | - - alternately: target_mob:loadprog(31404) | ||
- | |||
- | - - alternately : mob:loadprog(31404) – 'mob' in this case would be | ||
- | |||
- | target_mob because this function runs in target_mob's env | ||
- | |||
- | <code> | ||
- | a_function( "test1", "test2" ) | ||
- | </code> | ||
- | |||
- | end) | ||
- | |||
- | – mprog 31405 with return value | ||
- | |||
- | – note, 'mob' is not a function variable, but a global variable to the | ||
- | |||
- | environment | ||
- | |||
- | – uses closure magic to move values between script envs | ||
- | |||
- | local result | ||
- | |||
- | target_mob = getmobworld(31424)[1] | ||
- | |||
- | target_mob:loadfunction( function () | ||
- | |||
- | <code> | ||
- | loadprog(31404) | ||
- | </code> | ||
- | |||
- | <code> | ||
- | result=a_function( "test1", "test2" ) | ||
- | </code> | ||
- | |||
- | end) | ||
- | |||
- | [/code] | ||
- | |||
- | Not as pretty as something like target_mob:loadprog( 31404, "test1", "test2" ), | ||
- | |||
- | 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 | ||
- | |||
- | refer to those parameters in the 31404 script? | ||
- | |||
- | 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. | ||
- | |||
- | loadfunction can't directly return a value, but you can use other tricks to | ||
- | |||
- | get the job done: | ||
- | |||
- | [code lua] | ||
- | |||
- | – mprog 31405 with return value | ||
- | |||
- | – uses closure magic to move values between script envs | ||
- | |||
- | local result | ||
- | |||
- | target_mob = getmobworld(31424)[1] | ||
- | |||
- | target_mob:loadfunction( function () | ||
- | |||
- | <code> | ||
- | loadprog(31404) | ||
- | </code> | ||
- | |||
- | <code> | ||
- | result=a_function( "test1", "test2" ) | ||
- | </code> | ||
- | |||
- | end) | ||
- | |||
- | [/code] | ||