Page 1 of 1

Functions - How to?

Posted: Sun Jan 28, 2018 2:18 pm
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

RE: Functions - How to?

Posted: Sun Jan 28, 2018 3:16 pm
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()

RE: Functions - How to?

Posted: Mon Mar 05, 2018 12:52 pm
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.