Page 1 of 1

Lua in scenario guidelines

Posted: Sat Sep 10, 2016 1:59 am
by michaelm75au
I have put together a list of items to be aware of when using Lua with Command scenarios.

I hope it is helpful.

Release 1.11.SR7
Note:
Due to how errors are now handle from SR7, if a command fails in the console window, it will show an error. If the command runs outside the console (as in an event script), it will not fail with a visible error but return a nil or "" or some other value.
One issue with commands running in an event is that sometimes they fail with an in-game message that actually stops the rest of the script from running. Now, these event scripts will run without any in-game message showing, and the designer should check the result of the command and handle any error condition, and let the remaining script run as needed.

My intent is have all command errors behave in the same fashion in the console window; and the command errors outside a console behave without stopping the script. Which requires the designer to cater for the specific error conditions. Benefit of showing the error regardless, is that sometimes a real error is hidden by just assuming the command works. A good example is this: assume you have a unit 'My Unit' in game. Running the command 'ScenEdit_GetUnit({name='My Unit '})' would return a nil in the earlier releases. Obviously this is wrong; there is no unit 'My Unit '. Now when you try this in the console, it will show as an error.

To emulate the expected outcome from an event, put 'Tool_EmulateNoConsole(true)' at the start of the script to be tested; it is not required in the event code as the script is already not running in a console.

Note also, that the Lua history log should also record the event script errors.

Hope this makes sense.

RE: Lua in scenario guidelines

Posted: Sat Sep 10, 2016 2:21 am
by mikmykWS
We think it's awesome!

RE: Lua in scenario guidelines

Posted: Sat Sep 10, 2016 7:57 am
by Rory Noonan
That's a great help, thank you.

RE: Lua in scenario guidelines

Posted: Sat Sep 10, 2016 11:02 am
by thewood1
That is excellent.

RE: Lua in scenario guidelines

Posted: Sat Sep 10, 2016 5:55 pm
by mikkey
Excellent work Michael, thanks!

RE: Lua in scenario guidelines

Posted: Sat Sep 10, 2016 11:05 pm
by tjhkkr
Thank you very much!

RE: Lua in scenario guidelines

Posted: Sat Sep 10, 2016 11:05 pm
by tjhkkr
Thank you very much!

RE: Lua in scenario guidelines

Posted: Sun Sep 11, 2016 7:30 am
by Zaslon
Very interesting. Thanks Michael.

RE: Lua in scenario guidelines

Posted: Sun Sep 11, 2016 10:29 am
by kevinkins
Welcome addition to the resources. Reminds of the old days when there were sites that contained code snipets and examples in different languages. I'm the type that needs a working example for each element of the code. Thanks for that.

RE: Lua in scenario guidelines

Posted: Thu Feb 16, 2017 6:40 am
by michaelm75au
Note:
Due to how errors are now handle from SR7, if a command fails in the console window, it will show an error. If the command runs outside the console (as in an event script), it will not fail with a visible error but return a nil or "" or some other value.
One issue with commands running in an event is that sometimes they fail with an in-game message that actually stops the rest of the script from running. Now, these event scripts will run without any in-game message showing, and the designer should check the result of the command and handle any error condition, and let the remaining script run as needed.

My intent is have all command errors behave in the same fashion in the console window; and the command errors outside a console behave without stopping the script. Which requires the designer to cater for the specific error conditions.

To emulate the expected outcome from an event, put 'Tool_EmulateNoConsole(true)' at the start of the script to be tested; it is not required in the event code as the script is already not running in a console.

Note also, that the Lua history log should also record the event script errors.

Hope this makes sense.

RE: Lua in scenario guidelines

Posted: Thu Feb 16, 2017 12:51 pm
by Randomizer
How have I missed this thread for months? D'oh! And by the way Michaelm, many thanks for all you do in ridding the community of fear and ignorance wherever Lua meets CMANO.

-C