Lua in scenario guidelines

All discussions & material related to Command's Lua interface

Moderators: angster, RoryAndersonCDT, michaelm75au, MOD_Command

Post Reply
User avatar
michaelm75au
Posts: 12455
Joined: Sat May 05, 2001 8:00 am
Location: Melbourne, Australia

Lua in scenario guidelines

Post 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.
Attachments
luaguidelines.txt
(11 KiB) Downloaded 709 times
Michael
mikmykWS
Posts: 7185
Joined: Tue Mar 22, 2005 4:34 pm

RE: Lua in scenario guidelines

Post by mikmykWS »

We think it's awesome!
Rory Noonan
Posts: 2418
Joined: Thu Dec 18, 2014 1:53 am
Location: Brooklyn, NY

RE: Lua in scenario guidelines

Post by Rory Noonan »

That's a great help, thank you.
Image
thewood1
Posts: 9193
Joined: Sun Nov 27, 2005 6:24 pm
Location: Boston

RE: Lua in scenario guidelines

Post by thewood1 »

That is excellent.
User avatar
mikkey
Posts: 3173
Joined: Sun Feb 10, 2008 1:04 pm
Location: Slovakia

RE: Lua in scenario guidelines

Post by mikkey »

Excellent work Michael, thanks!
User avatar
tjhkkr
Posts: 2431
Joined: Wed Jun 02, 2010 11:15 pm
Contact:

RE: Lua in scenario guidelines

Post by tjhkkr »

Thank you very much!
Remember that the evil which is now in the world will become yet more powerful, and that it is not evil which conquers evil, but only love -- Olga Romanov.
User avatar
tjhkkr
Posts: 2431
Joined: Wed Jun 02, 2010 11:15 pm
Contact:

RE: Lua in scenario guidelines

Post by tjhkkr »

Thank you very much!
Remember that the evil which is now in the world will become yet more powerful, and that it is not evil which conquers evil, but only love -- Olga Romanov.
Zaslon
Posts: 292
Joined: Sun Jun 14, 2015 8:52 am

RE: Lua in scenario guidelines

Post by Zaslon »

Very interesting. Thanks Michael.
Image
Kids think about Iran and Amateurs think about Russia, but professionals think about China
User avatar
kevinkins
Posts: 2465
Joined: Wed Mar 08, 2006 11:54 am

RE: Lua in scenario guidelines

Post 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.
“The study of history lies at the foundation of all sound military conclusions and practice.”
Alfred Thayer Mahan
User avatar
michaelm75au
Posts: 12455
Joined: Sat May 05, 2001 8:00 am
Location: Melbourne, Australia

RE: Lua in scenario guidelines

Post 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.
Michael
User avatar
Randomizer
Posts: 1498
Joined: Sat Jun 28, 2008 8:31 pm

RE: Lua in scenario guidelines

Post 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
Post Reply

Return to “Lua Legion”