My Lua Script-Please Help

Kevin Caccamo

Rear Admiral
Here is my full lua script:

dofile "flightcommander.lua"

killed = 0
identified = 0

function cratedestroy()
killed + 1
end

function begin()
Mission_setScriptControl(true)
Mission_setAutopilotEnabled(true)
end

function nav1()
Mission_setAutopilotEnabled(false)
Ship_setCargo(Ms-c4, "Marijuana");
Ship_setCargo(Ms-c2, "Food");
Ship_setCargo(Ms-c3, "Food");
Ship_setCargo(Ms-c1, "Organic Weapons");
if(Ship_isCargoIdentified(Ms-c1) ) then
Ship_setIff(Ms-c1, IFF_ENEMY);
Mission_setEnemiesAreAlive(true)
identified + 1
end
if(Ship_isCargo_Identified(Ms-c2) ) then
Ship_setIff(Ms-c2, IFF_FRIEND);
identified + 1
end
if(Ship_isCargo_Identified(Ms-c3) ) then
Ship_setIff(Ms-c3, IFF_FRIEND);
identified + 1
end
if(Ship_isCargo_Identified(Ms-c4) ) then
Ship_setIff(Ms-c4, IFF_ENEMY);
identified + 1
end
if(Ship_getNumTurrets(Ms-c4) == 0) then
Ship_setIff(Ms-c4, IFF_NEUTRAL);
killed + 1
end
if killed == 2 then
Mission_setAutopilotEnabled(true)
Mission_setEnemiesAreAlive(false)
Mission_setComplete();
Objective_setState(1, OBJECTIVE_COMPLETE)
end
if identified == 4 then
Objective_setState(0, OBJECTIVE_COMPLETE)
end
end

Can you please help me because I am trying to get the mission to work properly.

There is three cargo containers at nav 1

There is also a cargo ship at nav 1

The cargo ship and also one cargo container have illegal cargo (Marijuana and Organic weapons)

When you get there, you will not be able to autopilot

When you identify the cargo ship, it turns evil and enemies in the navpoint are toggled true

When you disable the cargo ship, it turns neutral

When you destroy the cargo box, nothing happens, except a value should be added to a variable (killed)

When you have destroyed the cargo box and disabled the cargo ship, an objective should be completed.

Each time you identify a cargo box/ship, a value should be added to a variable (identified)

That variable also deals with an objective. When you have identified the cargo of everything, an objective should be completed

The two cargo boxes that contain food should turn friendly when you identify their cargo.

Remember that I am a programmer in training. I don't understand Lua like you do. Please help me out

BTW it does not work in game:eek:
 
You'll always need to send over both your lua file and your mission.xml file. Firstly, did luac successfully compile your program, you need to get that working first. There are lots of bugs I can see just by looking at i. First, all ships names need to be in quotes, like "Ms-c4" instead of just Ms-c4. Also the statement killed + 1 doesn't do anything. You want to do killed = killed + 1. killed + 1 is an expression, you need to store that value somewhere. And you mis-spelled the function name Ship_isCargoIdentified. I can see some more bugs too, but we'll go one step at a time. Post your next attempt. Think carefully about when your function gets called. Read my scripting documention page more carefully, and look carefully at my example scripts.

Good effort attempting to get the script going. As a programming teacher, I never give out the answers; I tell you what you need so you can figure it out for yourself.

Make sure to read at least the first few chapters of the lua programming guides I mentioned on the web page.
 
Thank you for the help, eddieb. Very much appreciated. I did not know that ship names had to be in quotes. Already fixed the math problems on my personal computer. I am surprised I was able just to make a program that compiles! After all, I am a lua programmer in training. (And I just read random locations of the book and tried to understand that crap) I also type fast too and maybe did not recognize the error on Ship_isCargoIdentified, or could it be the extra underscore?
 
All right, here is my new script:

dofile "flightcommander.lua"

killed = 0
identified = 0

function cratedestroy()
killed = killed + 1
end

function begin()
Mission_setScriptControl(true)
Mission_setAutopilotEnabled(true)
end

function nav1()
Mission_setAutopilotEnabled(false)
Ship_setCargo("Ms-c4", "Marijuana");
Ship_setCargo("Ms-c2", "Food");
Ship_setCargo("Ms-c3", "Food");
Ship_setCargo("Ms-c1", "Organic Weapons");
if(Ship_isCargoIdentified("Ms-c1") ) then
Ship_setIff("Ms-c1", IFF_ENEMY);
Mission_setEnemiesAreAlive(true)
identified = identified + 1
end
if(Ship_isCargoIdentified("Ms-c2") ) then
Ship_setIff("Ms-c2", IFF_FRIEND);
identified = identified + 1
end
if(Ship_isCargoIdentified("Ms-c3") ) then
Ship_setIff("Ms-c3", IFF_FRIEND);
identified = identified + 1
end
if(Ship_isCargoIdentified("Ms-c4") ) then
Ship_setIff("Ms-c4", IFF_ENEMY);
Mission_setEnemiesAreAlive(true)
identified = identified + 1
end
if(Ship_getNumTurrets("Ms-c4") == 0) then
Ship_setIff("Ms-c4", IFF_NEUTRAL);
killed = killed + 1
end
if killed == 2 then
Mission_setAutopilotEnabled(true)
Mission_setEnemiesAreAlive(false)
Mission_setComplete();
Objective_setState(1, OBJECTIVE_COMPLETE)
end
if identified == 4 then
Objective_setState(0, OBJECTIVE_COMPLETE)
end
end

function landing()
Mission_setAutopilotEnabled(false)
end

My mission xml:

<?xml version="1.0" encoding="UTF-8"?>
<mission name = "Into the Blackness" sector="blue" can_eject="false" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="mission.xsd">
<navpoints number="3">
<navpoint numships="2" numwaves="1">
<ship name="outpost" pilot="Tarma Station" comm="midway" video="midway" autopilot="false" must_survive="true" alignment="friend" wave="0" scriptid="Ms-1">
<location>
<point x="-55.555553" y ="0.0" z ="1444.4443" />
</location>
<rotation>
<point x="0.0" y ="0.0" z ="0.0" />
</rotation>
</ship>
<ship name="panther" pilot="player" comm="casey" video="" autopilot="true" alignment="friend" wave="0" scriptid="Ms-player">
<location>
<point x="-77.77777" y ="0.0" z ="94.44444" />
</location>
<rotation>
<point x="0.0" y ="0.0" z ="0.0" />
</rotation>
</ship>
</navpoint>
<navpoint numships="4" numwaves="1">
<ship name="cargo" pilot="" comm="" video="" autopilot="false" alignment="neutral" wave="0" scriptid="Ms-c1">
<location>
<point x="944.4444" y ="0.0" z ="888.88885" />
</location>
<rotation>
<point x="0.0" y ="0.0" z ="0.0" />
</rotation>
<script ondestroy="cratedestroy" />
</ship>
<ship name="cargo" pilot="" comm="" video="" autopilot="false" alignment="neutral" wave="0" scriptid="Ms-c2">
<location>
<point x="177.77777" y ="0.0" z ="1155.5555" />
</location>
<rotation>
<point x="0.0" y ="0.0" z ="0.0" />
</rotation>
</ship>
<ship name="cargo" pilot="" comm="" video="" autopilot="false" alignment="neutral" wave="0" scriptid="Ms-c3">
<location>
<point x="-566.6666" y ="0.0" z ="966.6666" />
</location>
<rotation>
<point x="0.0" y ="0.0" z ="0.0" />
</rotation>
</ship>
<ship name="pelican" pilot="TCS Monologue" comm="midway" video="midway" autopilot="false" alignment="neutral" wave="0" scriptid="Ms-c4">
<location>
<point x="144.44444" y ="0.0" z ="805.55554" />
</location>
<rotation>
<point x="0.0" y ="92.0" z ="0.0" />
</rotation>
</ship>
<script oncreate="nav1" />
</navpoint>
<navpoint numships="1" numwaves="1">
<ship name="outpost" pilot="Tarma Station" comm="midway" video="midway" autopilot="false" land="true" must_survive="true" alignment="friend" wave="0" scriptid="Ms-land">
<location>
<point x="33.333332" y ="0.0" z ="1455.5555" />
</location>
<rotation>
<point x="0.0" y ="188.0" z ="0.0" />
</rotation>
</ship>
<script oncreate="landing" />
</navpoint>
</navpoints>
<objectives>
<objective name="Identify all the cargo at Nav 1" category="Primary" default="Incomplete" />
<objective name="Destroy any spies" category="Primary" default="Incomplete" />
</objectives>
</mission>

And, as a bonus, my campaign xml

<campaign nummissions="1" name="Into the Blackness">
<mission file="blk.mission.xml></mission>
</campaign>

See what you can make out of all that. I misread your first message. Sorry, I do not have luac. I did not even think about such a thing. Although I will try today.

I just found out I have one already in my flight commander directory. I did not get a result and I do not know how to even get into the directory in the MS-DOS Prompt!
 
Well google around and learn how to use the MS dos prompt then. Also, your campaign file is invalid XML. You can check your xml with something like XML Spy first, and validate the file against the schemas that come with Flight Commander. Use an XML editor like xml spy, it catches these things for you. Look at it carefully.
 
eddieb said:
Well google around and learn how to use the MS dos prompt then. Also, your campaign file is invalid XML. You can check your xml with something like XML Spy first, and validate the file against the schemas that come with Flight Commander. Use an XML editor like xml spy, it catches these things for you. Look at it carefully.

Already done. The mission works now. It's just when I play the mission, the objects are not changing their IFF code, so the script is not quite working yet.
 
Right. Did you read my scripting documentation page carefully. Do you know the difference between an on create function, and one that is called once a second? Think about what will happen if your script is only called when you enter the nav. Once you figure out what needs to happen at the start of the nav versus what needs to happen repeatedly, that will tell you how to reorganize your script. Look at my cargo run script for an example. Its in scriptexample2.lua and the associated xml.
 
eddieb said:
Right. Did you read my scripting documentation page carefully. Do you know the difference between an on create function, and one that is called once a second? Think about what will happen if your script is only called when you enter the nav. Once you figure out what needs to happen at the start of the nav versus what needs to happen repeatedly, that will tell you how to reorganize your script. Look at my cargo run script for an example. Its in scriptexample2.lua and the associated xml.

Yah I read through the scripting documentation even while I was editing the script. It seems that whenever I complete nav 1, I am not allowed to autopilot. I am going to test the mission again now.
 
I still cannot get out of nav 1! Eddieb, can you please tell me what is stopping me because I really have no clue what is going on here. I will send my three files to you in a zip format. You should be able to tell me if you played it yourself. I will probably have to e-mail the file to you. I wish I could figure out what was wrong
 
Look, I'm not going to write your script for you. It's for your own good, making mistakes is a part of learning to write scripts. The problem is you put everything into the entry function of the nav point. It only gets called once. Again think about what happens if your function is only called once at the beginning of the mission before anything happens. You need to create a on time advance function as I describe in the docs. You need to look at my example script, it does almost exactly what you're doing. Go ahead and post your changes after you're created the time advance function, and put the appropriate code in there. Here's another hint. Put in print calls, or use the Mission_setDebugText(string text) to see if what you think is happening is actually happening.
 
Eddieb, do I have to call the random.math command in order for some of the math functions in this script to work properly. I've modified the script a bit with lua IDE. Eddieb, I do not know if I am making an error with Mission_setAccomplished();, but whatever it is, I just cannot leave nav 1. Here is the script, if it interests you:

-- Script by Kevin Caccamo
-- Some bugs found with brain and LuaIDE
-- Warning: Lua Programmer in Training
-- Reminder: Put semicolons after all lines of code
-- except after functions, if/then statements
-- and after dofile
dofile "flightcommander.lua"

killed = 0;
identified = 0;
box1 = 1;
box1dead = 0;
box2 = 1;
box3 = 1;
box4 = 1;
box4dead = 0;
bvar = 1;
cvar = 1;
dvar = 1;
activatendofnavone = 0;

function cratedestroy()
killed = killed + 1;
box1dead = 1;
end

function begin()
Mission_setScriptControl(true);
Mission_setAutopilotEnabled(true);
Mission_setFriendlyEjectPickup(true);
end

function cbox1(shipid)
if box1 ~= 0 and box1dead == 0 then
if(Ship_isCargoIdentified(shipid) ) then
Ship_setIff(shipid, IFF_ENEMY);
identified = identified + 1;
box1 = 0;
end
end
end
function cbox2(shipid)
if box2 ~= 0 then
if(Ship_isCargoIdentified(shipid) ) then
Ship_setIff(shipid, IFF_FRIEND);
identified = identified + 1;
box2 = 0;
end
end
end
function cbox3(shipid)
if box3 ~= 0 then
if(Ship_isCargoIdentified(shipid) ) then
Ship_setIff(shipid, IFF_FRIEND);
identified = identified + 1;
box3 = 0;
end
end
end
function ctransport(shipid)
if box4 ~= 0 and box4dead == 0 then
if(Ship_isCargoIdentified(shipid) ) then
Ship_setIff(shipid, IFF_ENEMY);
identified = identified + 1;
end
if(Ship_getNumTurrets(shipid) == 0) then
Ship_setIff(shipid, IFF_NEUTRAL);
killed = killed + 1;
box4dead = 1;
box4 = 0;
end
end
end

function nav1()
Mission_setAutopilotEnabled(false);
Ship_setCargo("Ms-c4", "Marijuana");
Ship_setCargo("Ms-c2", "Food");
Ship_setCargo("Ms-c3", "Food");
Ship_setCargo("Ms-c1", "Organic Weapons");
Mission_setEnemiesAreAlive(true);
end

function nav1events()
if killed == 2 and bvar ~= 0 then
Objective_setState(1, OBJECTIVE_COMPLETE);
bvar = 0;
-- Do a check to see if the code is working
-- Text under print will appear on console
print "Killed all spies"
end
if identified == 4 and cvar ~= 0 then
Objective_setState(0, OBJECTIVE_COMPLETE);
cvar = 0;
-- Do a check to see if the code is working
print "Identified all vessels"
end
if identified == 4 and killed == 2 then
activatendofnavone = activatendofnavone + 1;
end
if activatendofnavone == 1 and dvar ~= 0 then
Mission_setAutopilotEnabled(true);
Mission_setAccomplished();
Mission_setEnemiesAreAlive(false);
-- Do a check to see if the code is working
print "Allowed to leave navpoint"
-- Cancel the code that is currently being run
dvar = 0;
end
end

function landing()
Mission_setAutopilotEnabled(false);
if (Ship_isWithinRange("Ms-player", "Ms-land", 8000) ) then
Mission_doLanding();
end
end

I think it should be working fine now, but it's not, and I feel like I am in deep trouble.
 
This looks a lot better, but I'd need your new mission.xml also to try it out. You shouldn't need to call random unless you want a random number.
 
Ok. Here goes nothing.

Actually, here goes the script:

-- Script by Kevin Caccamo
-- Some bugs found with brain and LuaIDE
-- Warning: Lua Programmer in Training
-- Reminder: Put semicolons after all lines of code
-- except after functions, if/then statements
-- and after dofile
dofile "flightcommander.lua"

killed = 0;
identified = 0;
box1 = 1;
box1dead = 0;
box2 = 1;
box3 = 1;
box4 = 1;
box4dead = 0;
bvar = 1;
cvar = 1;
dvar = 1;
activatendofnavone = 0;

function cratedestroy()
killed = killed + 1;
box1dead = 1;
end

function begin()
Mission_setScriptControl(true);
Mission_setAutopilotEnabled(true);
Mission_setFriendlyEjectPickup(true);
end

function cbox1(shipid)
if box1 ~= 0 and box1dead == 0 then
if(Ship_isCargoIdentified(shipid) ) then
Ship_setIff(shipid, IFF_ENEMY);
identified = identified + 1;
box1 = 0;
end
end
end
function cbox2(shipid)
if box2 ~= 0 then
if(Ship_isCargoIdentified(shipid) ) then
Ship_setIff(shipid, IFF_FRIEND);
identified = identified + 1;
box2 = 0;
end
end
end
function cbox3(shipid)
if box3 ~= 0 then
if(Ship_isCargoIdentified(shipid) ) then
Ship_setIff(shipid, IFF_FRIEND);
identified = identified + 1;
box3 = 0;
end
end
end
function ctransport(shipid)
if box4 ~= 0 and box4dead == 0 then
if(Ship_isCargoIdentified(shipid) ) then
Ship_setIff(shipid, IFF_ENEMY);
identified = identified + 1;
end
if(Ship_getNumTurrets(shipid) == 0) then
Ship_setIff(shipid, IFF_NEUTRAL);
killed = killed + 1;
box4dead = 1;
box4 = 0;
end
end
end

function nav1()
Mission_setAutopilotEnabled(false);
Ship_setCargo("Ms-c4", "Marijuana");
Ship_setCargo("Ms-c2", "Food");
Ship_setCargo("Ms-c3", "Food");
Ship_setCargo("Ms-c1", "Organic Weapons");
Mission_setEnemiesAreAlive(true);
end

function nav1events()
if killed == 2 and bvar ~= 0 then
Objective_setState(1, OBJECTIVE_COMPLETE);
bvar = 0;
-- Do a check to see if the code is working
-- Text under print will appear on console
print "Killed all spies"
end
if identified == 4 and cvar ~= 0 then
Objective_setState(0, OBJECTIVE_COMPLETE);
cvar = 0;
-- Do a check to see if the code is working
print "Identified all vessels"
end
if identified == 4 and killed == 2 then
activatendofnavone = activatendofnavone + 1;
print "called value change on activatendofnavone"
end
if activatendofnavone == 1 and dvar ~= 0 then
print "activatendofnavone read"
Mission_setAutopilotEnabled(true);
Mission_setAccomplished();
Mission_setEnemiesAreAlive(false);
-- Do a check to see if the code is working
print "Allowed to leave navpoint"
-- Cancel the code that is currently being run
dvar = 0;
end
end

function landing()
Mission_setAutopilotEnabled(false);
if (Ship_isWithinRange("Ms-player", "Ms-land", 2500) ) then
Mission_doLanding();
end
end

And here is the Mission XML, like you asked:

<?xml version="1.0" encoding="UTF-8"?>
<mission name = "Into the Blackness" sector="blue" can_eject="false" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="mission.xsd">
<navpoints number="3">
<navpoint numships="2" numwaves="1">
<ship name="outpost" pilot="Tarma Station" comm="midway" video="midway" autopilot="false" must_survive="true" alignment="friend" wave="0" scriptid="Ms-1">
<location>
<point x="-55.555553" y ="0.0" z ="1444.4443" />
</location>
<rotation>
<point x="0.0" y ="0.0" z ="0.0" />
</rotation>
</ship>
<ship name="panther" pilot="player" comm="casey" video="" autopilot="true" alignment="friend" wave="0" scriptid="Ms-player">
<location>
<point x="-77.77777" y ="0.0" z ="94.44444" />
</location>
<rotation>
<point x="0.0" y ="0.0" z ="0.0" />
</rotation>
</ship>
<script oncreate="begin" />
</navpoint>
<navpoint numships="4" numwaves="1">
<ship name="cargo" pilot="" comm="" video="" autopilot="false" alignment="neutral" wave="0" scriptid="Ms-c1">
<location>
<point x="944.4444" y ="0.0" z ="888.88885" />
</location>
<rotation>
<point x="0.0" y ="0.0" z ="0.0" />
</rotation>
<script during="cbox1" ondestroy="cratedestroy" />
</ship>
<ship name="cargo" pilot="" comm="" video="" autopilot="false" alignment="neutral" wave="0" scriptid="Ms-c2">
<location>
<point x="177.77777" y ="0.0" z ="1155.5555" />
</location>
<rotation>
<point x="0.0" y ="0.0" z ="0.0" />
</rotation>
<script during="cbox2" />
</ship>
<ship name="cargo" pilot="" comm="" video="" autopilot="false" alignment="neutral" wave="0" scriptid="Ms-c3">
<location>
<point x="-566.6666" y ="0.0" z ="966.6666" />
</location>
<rotation>
<point x="0.0" y ="0.0" z ="0.0" />
</rotation>
<script during="cbox3" />
</ship>
<ship name="pelican" pilot="TCS Monologue" comm="midway" video="midway" autopilot="false" alignment="neutral" wave="0" scriptid="Ms-c4">
<location>
<point x="144.44444" y ="0.0" z ="805.55554" />
</location>
<rotation>
<point x="0.0" y ="92.0" z ="0.0" />
</rotation>
<script during="ctransport" />
</ship>
<script oncreate="nav1" during="nav1events" />
</navpoint>
<navpoint numships="1" numwaves="1">
<ship name="outpost" pilot="Tarma Station" comm="midway" video="midway" autopilot="false" land="true" must_survive="true" alignment="friend" wave="0" scriptid="Ms-land">
<location>
<point x="33.333332" y ="0.0" z ="1455.5555" />
</location>
<rotation>
<point x="0.0" y ="188.0" z ="0.0" />
</rotation>
</ship>
<script during="landing" />
</navpoint>
</navpoints>
<objectives>
<objective name="Identify all the cargo at Nav 1" category="Primary" default="Incomplete" />
<objective name="Destroy any spies" category="Primary" default="Incomplete" />
</objectives>
</mission>

The objectives are being completed, but there is a big bug that you are not able to leave nav 1. See what results you get from that. You have my permission to copy the script and the Mission XML directly from this message
 
Firstly, its easier for me if you just send me a zip of your files.

Now, try putting a print like this

print("identified =" .. identified .. " killed = " .. killed) ;

At the very beginning of function nav1events. You'll see one of your variables is not what you expect it to be.

Also, if your variable can only have two values, you really should use a boolean and true or false instead of an integer. Also, try to use more descriptive variable names than bvar, cvar, and dvar.

You also need to consider the case of someone killing the transport without destroying all its turrets first.

You're almost there, keep trying.
 
Ever since I have read the section 3.2 in the online version of the book "Programming in Lua," I thought that this "~=" operator meant "not equal." Is this true?
 
Yes, according to your own citation:
The operator == tests for equality; the operator ~= is the negation of equality.
 
Eddieb, the script is done now, except for one thing. At the end of my mission, the ship keeps continually landing nonstop.

Also, enemies alive mode won't disable at the end of nav 1
 
We found the problem with the continually landing. You're calling Mission_doLanding() multiple times. Make sure you only call it once.
 
Back
Top