Functions - How to?

All discussions & material related to Command's Lua interface

Moderators: RoryAndersonCDT, michaelm75au, angster, MOD_Command

Post Reply
User avatar
vettim89
Posts: 3723
Joined: Fri Jul 13, 2007 11:38 pm
Location: Toledo, Ohio

Functions - How to?

Post by vettim89 »

Ok, so I think I figured out my question about resetting cap code. Here is what I came up with

local acount=0
for i=1,12 do
ScenEdit_AssignUnitToMission('Sundowner #'..i, 'America CAP')
if acount<4 then
unit=ScenEdit_GetUnit({side='NATO', name="Sundowner #'..i})
if unit.condition_v=='Airborne' then
acount = acount+1
end
else
unit=ScenEdit_GetUnit({side='NATO', name='Sundowner #'..i})
if unit.condition_v=='Airborne' then
ScenEdit_SetUnitSide({side='NATO', name='Sundowner #'..i, RTB='true'} )
end
end
end

No I need to run this code on five different missions and reasoned that it would be best to set this up as a function. The problem is that I am unsure how to execute the creation, saving and loading of a function. I know the function needs to be saved to the lua library in the CMNAO folder. Then an "include" line needs to be added to a scenario start event

So my questions are:
1. How do I save the function to the lua library?
2. how do I add that library to the scenario?
3. If I publish this scenario, how do I add the needed function to the export/import so that other players have access to this function

As always, thank you in advance for the help
"We have met the enemy and they are ours" - Commodore O.H. Perry
angster
Posts: 89
Joined: Mon Oct 12, 2015 10:25 am

RE: Functions - How to?

Post by angster »

Normally, I would wrap it in a function and load it on scenario start (with repeat on). You can create a LUA action and import the below code:

function myFunctionName()
myCode
end

You will need another LUA action to trigger the following:

myFunctionName()
User avatar
kevinkins
Posts: 2465
Joined: Wed Mar 08, 2006 11:54 am

RE: Functions - How to?

Post by kevinkins »

I keep this basic code in my lua code DB:

function add(first_number, second_number)
return first_number + second_number
end
print(add(5, 5))

The number 10 is printed in the lua script console.

But I think the PO wanted to known about this technique: https://forums.coronalabs.com/topic/381 ... -lua-file/

It does work - I tested something similar a long while back. But it means posting you own code file with each scenario. Since the lua code is so unique to each scenario, I could not find a big reason to develop what amounts to an "include" file. I do think it's a real good idea to develop your own lua code DB where you have code syntax you know works. Copy, paste and modified as needed.

Edit: Here is what I tested -
ScenEdit_RunScript('myModule1.lua')

n1 = 1
n2 = 100
x = sample.add(n1,n2)
print(x)

The tiny function was defined in myModule1.lua.




“The study of history lies at the foundation of all sound military conclusions and practice.”
Alfred Thayer Mahan
Post Reply

Return to “Lua Legion”