Help please!

Kevin Caccamo

Rear Admiral
I've started working on the fourth episode of WC1 Remake (Dakota/Port Hedland) and I've ran into a big problem...

After I kill all the Krants at NAV 1 where the Drayman is supposed to jump out (I call the enemydie function after I kill each Krant) the Drayman does not jump out when it is supposed to!

Here is the mission code, done fully in the mission editor:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<mission name = "" sector="peach" can_eject="false" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="mission.xsd">
  <navpoints number="5">
    <navpoint numships="4" numwaves="1">
      <ship name="claw" pilot="" comm="" video="" autopilot="false" must_survive="true" alignment="friend"  wave="0">
        <location>
          <point  x="-90.81803" y ="0.0" z ="-1586.6444" />
        </location>
        <rotation>
          <point  x="0.0" y ="0.0" z ="0.0" />
        </rotation>
      </ship>
      <ship name="raptor" pilot="player" comm="" video="" autopilot="true" alignment="friend"  wave="0">
        <location>
          <point  x="-64.10684" y ="0.0" z ="-448.7479" />
        </location>
        <rotation>
          <point  x="0.0" y ="0.0" z ="0.0" />
        </rotation>
      </ship>
      <ship name="raptor" pilot="" comm="" video="" autopilot="true" alignment="friend" last_autopilot_nav="4"  wave="0">
        <location>
          <point  x="138.89816" y ="0.0" z ="-454.09015" />
        </location>
        <rotation>
          <point  x="0.0" y ="0.0" z ="0.0" />
        </rotation>
      </ship>
      <ship name="drayman" pilot="" comm="" video="" autopilot="true" must_survive="true" alignment="friend" last_autopilot_nav="2"  wave="0" scriptid="drayone">
        <location>
          <point  x="-261.76962" y ="0.0" z ="-443.40567" />
        </location>
        <rotation>
          <point  x="0.0" y ="0.0" z ="0.0" />
        </rotation>
        <script ondestroy="ifdraydies"  />
      </ship>
    </navpoint>
    <navpoint numships="5" numwaves="1">
      <ship name="salthi" pilot="" comm="kilrathi1" video="" autopilot="false" alignment="enemy"  wave="0">
        <location>
          <point  x="-619.69946" y ="0.0" z ="1137.8965" />
        </location>
        <rotation>
          <point  x="0.0" y ="180.0" z ="0.0" />
        </rotation>
      </ship>
      <ship name="salthi" pilot="" comm="kilrathi1" video="" autopilot="false" alignment="enemy"  wave="0">
        <location>
          <point  x="-341.90317" y ="0.0" z ="1143.2388" />
        </location>
        <rotation>
          <point  x="0.0" y ="180.0" z ="0.0" />
        </rotation>
      </ship>
      <ship name="salthi" pilot="" comm="kilrathi1" video="" autopilot="false" alignment="enemy"  wave="0">
        <location>
          <point  x="-69.44908" y ="0.0" z ="1148.5809" />
        </location>
        <rotation>
          <point  x="0.0" y ="180.0" z ="0.0" />
        </rotation>
      </ship>
      <ship name="salthi" pilot="" comm="kilrathi1" video="" autopilot="false" alignment="enemy"  wave="0">
        <location>
          <point  x="245.7429" y ="0.0" z ="1143.2388" />
        </location>
        <rotation>
          <point  x="0.0" y ="180.0" z ="0.0" />
        </rotation>
      </ship>
      <ship name="salthi" pilot="" comm="kilrathi1" video="" autopilot="false" alignment="enemy"  wave="0">
        <location>
          <point  x="523.53925" y ="0.0" z ="1143.2388" />
        </location>
        <rotation>
          <point  x="0.0" y ="180.0" z ="0.0" />
        </rotation>
      </ship>
    </navpoint>
    <navpoint numships="4" numwaves="1">
      <ship name="jumpbuoy" pilot="" comm="" video="" autopilot="false" alignment="neutral"  wave="0">
        <location>
          <point  x="-37.39566" y ="0.0" z ="886.81134" />
        </location>
        <rotation>
          <point  x="0.0" y ="0.0" z ="0.0" />
        </rotation>
      </ship>
      <ship name="krant" pilot="" comm="kilrathi1" video="" autopilot="false" alignment="enemy"  wave="0">
        <location>
          <point  x="-657.09515" y ="0.0" z ="908.1803" />
        </location>
        <rotation>
          <point  x="0.0" y ="180.0" z ="0.0" />
        </rotation>
        <script ondestroy="enemydie"  />
      </ship>
      <ship name="krant" pilot="" comm="kilrathi1" video="" autopilot="false" alignment="enemy"  wave="0">
        <location>
          <point  x="-1180.6344" y ="0.0" z ="507.5125" />
        </location>
        <rotation>
          <point  x="0.0" y ="138.0" z ="0.0" />
        </rotation>
        <script ondestroy="enemydie"  />
      </ship>
      <ship name="krant" pilot="" comm="kilrathi1" video="" autopilot="false" alignment="enemy"  wave="0">
        <location>
          <point  x="753.25543" y ="0.0" z ="58.764606" />
        </location>
        <rotation>
          <point  x="0.0" y ="260.0" z ="0.0" />
        </rotation>
        <script ondestroy="enemydie"  />
      </ship>
    </navpoint>
    <navpoint numships="5" numwaves="1">
      <ship name="jumpbuoy" pilot="" comm="" video="" autopilot="false" alignment="neutral"  wave="0">
        <location>
          <point  x="-186.9783" y ="0.0" z ="1073.7897" />
        </location>
        <rotation>
          <point  x="0.0" y ="0.0" z ="0.0" />
        </rotation>
      </ship>
      <ship name="drayman" pilot="" comm="" video="" autopilot="true" must_survive="true" alignment="friend" last_autopilot_nav="4"  wave="0" jumpin="true">
        <location>
          <point  x="-165.60934" y ="0.0" z ="876.1269" />
        </location>
        <rotation>
          <point  x="0.0" y ="180.0" z ="0.0" />
        </rotation>
        <script ondestroy="draytwodies"  />
      </ship>
      <ship name="jalthi" pilot="" comm="kilrathi1" video="" autopilot="false" alignment="enemy"  wave="0">
        <location>
          <point  x="-667.7796" y ="0.0" z ="443.40567" />
        </location>
        <rotation>
          <point  x="0.0" y ="121.0" z ="0.0" />
        </rotation>
      </ship>
      <ship name="jalthi" pilot="" comm="kilrathi1" video="" autopilot="false" alignment="enemy"  wave="0">
        <location>
          <point  x="-731.8865" y ="0.0" z ="240.40067" />
        </location>
        <rotation>
          <point  x="0.0" y ="105.0" z ="0.0" />
        </rotation>
      </ship>
      <ship name="jalthi" pilot="" comm="kilrathi1" video="" autopilot="false" alignment="enemy"  wave="0">
        <location>
          <point  x="-758.59766" y ="0.0" z ="80.13355" />
        </location>
        <rotation>
          <point  x="0.0" y ="100.0" z ="0.0" />
        </rotation>
      </ship>
    </navpoint>
    <navpoint numships="1" numwaves="1">
      <ship name="claw" pilot="" comm="tc" video="" autopilot="false" land="true" must_survive="true" alignment="friend"  wave="0">
        <location>
          <point  x="-160.2671" y ="0.0" z ="1442.4039" />
        </location>
        <rotation>
          <point  x="0.0" y ="180.0" z ="0.0" />
        </rotation>
      </ship>
      <script oncreate="haveiwon"  />
    </navpoint>
  </navpoints>
  <objectives>
    <objective name="Escort Hospital ship out of the system" category="primary" default="incomplete" />
    <objective name="Escort Drayman back to the Claw" category="primary" default="incomplete" />
  </objectives>
</mission>

And here is the LUA script.

Code:
dofile "flightcommander.lua"

draydead = 0
cleared = 0
draytwodead = 0
enemykilled = 0

function ifdraydies()
 draydead = draydead + 1;
end

-- Since lua cannot support if statements within if statements
-- I have declared a new variable called "cleared"
-- If you have killed all the enemies, cleared will increase by 1
-- If the drayman is alive, cleared will also be increased by 1
-- If the drayman is dead, cleared will be increased by 2
-- Therefore, if cleared is 2, the drayman will jump out
-- And if cleared is 3, you will fail your first objective
function enemydie()
 enemykilled = enemykilled + 1;
 if enemykilled == 3 then
  cleared = cleared + 1;
 end
 if draydead == 0 then
  cleared = cleared + 1;
 end
 if draydead == 1 then
  cleared = cleared + 2;
 end
 if cleared == 2 then
  Ship_jumpOut("drayone")
  Objective_setState(0, OBJECTIVE_COMPLETE)
 elseif cleared == 3 then
  Objective_setState(0, OBJECTIVE_FAILED)
 end
end

function draytwodies()
 draytwodead = draytwodead + 1;
end

function haveiwon()
 if draytwodead == 1 then
  Objective_setState(1, OBJECTIVE_FAILED)
 elseif draytwodead == 0 then
  Objective_setState(1, OBJECTIVE_COMPLETE)
 end
end
 

eddieb

Vice Admiral
I'm pretty sure Lua does support ifs within ifs. Have you heard of elseif and else?
Also, you do a

a = 0
a = a + 1
a = a + 2

hmm. what's the total for a.... it's 3. But your code looks for a equal to 2, right? I think you can simplify this with if/elseif/else statements.

You really should be coding up a simple state machine to handle this stuff.
 

Sarty

Rear Admiral
I do remember using nested ifs in my Lua course. Either way Eddie is right on both counts, else and else if are more efficient than what you are doing and state machines are pretty handy.
 

Kevin Caccamo

Rear Admiral
The total for a is 3 only if the Drayman gets destroyed.
The total for a should be 2 if the Drayman survives.

I got rid of one extra if statement that was dealing with the same variable and replaced it with elseif.

Code:
 if draydead == 0 then
  cleared = cleared + 1;
 elseif draydead == 1 then
  cleared = cleared + 2;
 end

When I play it, it still does not work, and the first objective is incomplete when I end the mission.

And I never heard of state machines. What are they?

[EDIT] I think I see the problem now. The variable "cleared" adds up every time an enemy dies.
I changed the function code to

Code:
function enemydie()
 enemykilled = enemykilled + 1;
 if enemykilled == 3 then
  cleared = cleared + 1;
 end
 if draydead == 0 then
  cleared = cleared + 1;
 elseif draydead == 1 then
  cleared = cleared + 2;
 end
 if cleared == 4 then
  Ship_jumpOut("drayone")
  Objective_setState(0, OBJECTIVE_COMPLETE)
 elseif cleared > 4 then
  Objective_setState(0, OBJECTIVE_FAILED)
 end
end
 

eddieb

Vice Admiral
Kevin:

I fixed this one for you.

The big bug here was the following.

Whenever you have an destroy or create function associated with a ship, you must assign the ship a script id. That caused a lot of the problems. Here's the working mission.

Note, I also set the objectives to failed by default, that makes the script simpler.

Also note I used boolean variables here, it's much clearer that way.

Code:
dofile "flightcommander.lua"

dray1dead = false
dray2dead = false
enemykilled = 0

function enemydie()
 enemykilled = enemykilled + 1;
 if enemykilled == 3 and dray1dead == false then
  Ship_jumpOut("drayone")
  Objective_setState(0, OBJECTIVE_COMPLETE)
 end
end

function dray1dies()
  dray1dead = true
end

function dray2dies()
  dray2dead = true
end

function haveiwon()
 if(dray2dead == false) then
   Objective_setState(1, OBJECTIVE_COMPLETE)
 end
end

And the mission file

Code:
<?xml version="1.0" encoding="UTF-8"?>
<mission name = "" sector="peach" can_eject="false" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="mission.xsd">
  <navpoints number="5">
    <navpoint numships="4" numwaves="1">
      <ship name="claw" pilot="" comm="" video="" autopilot="false" must_survive="true" alignment="friend"  wave="0">
        <location>
          <point  x="-90.81803" y ="0.0" z ="-1586.6444" />
        </location>
        <rotation>
          <point  x="0.0" y ="0.0" z ="0.0" />
        </rotation>
      </ship>
      <ship name="raptor" pilot="player" comm="" video="" autopilot="true" alignment="friend"  wave="0">
        <location>
          <point  x="-64.10684" y ="0.0" z ="-448.7479" />
        </location>
        <rotation>
          <point  x="0.0" y ="0.0" z ="0.0" />
        </rotation>
      </ship>
      <ship name="raptor" pilot="" comm="" video="" autopilot="true" alignment="friend" last_autopilot_nav="4"  wave="0">
        <location>
          <point  x="138.89816" y ="0.0" z ="-454.09015" />
        </location>
        <rotation>
          <point  x="0.0" y ="0.0" z ="0.0" />
        </rotation>
      </ship>
      <ship name="drayman" pilot="" comm="" video="" autopilot="true" must_survive="true" alignment="friend" last_autopilot_nav="2"  wave="0" scriptid="drayone">
        <location>
          <point  x="-261.76962" y ="0.0" z ="-443.40567" />
        </location>
        <rotation>
          <point  x="0.0" y ="0.0" z ="0.0" />
        </rotation>
        <script  ondestroy="dray1dies"  />
      </ship>
    </navpoint>
    <navpoint numships="5" numwaves="1">
      <ship name="salthi" pilot="" comm="kilrathi1" video="" autopilot="false" alignment="enemy"  wave="0">
        <location>
          <point  x="-619.69946" y ="0.0" z ="1137.8965" />
        </location>
        <rotation>
          <point  x="0.0" y ="180.0" z ="0.0" />
        </rotation>
      </ship>
      <ship name="salthi" pilot="" comm="kilrathi1" video="" autopilot="false" alignment="enemy"  wave="0">
        <location>
          <point  x="-341.90317" y ="0.0" z ="1143.2388" />
        </location>
        <rotation>
          <point  x="0.0" y ="180.0" z ="0.0" />
        </rotation>
      </ship>
      <ship name="salthi" pilot="" comm="kilrathi1" video="" autopilot="false" alignment="enemy"  wave="0">
        <location>
          <point  x="-69.44908" y ="0.0" z ="1148.5809" />
        </location>
        <rotation>
          <point  x="0.0" y ="180.0" z ="0.0" />
        </rotation>
      </ship>
      <ship name="salthi" pilot="" comm="kilrathi1" video="" autopilot="false" alignment="enemy"  wave="0">
        <location>
          <point  x="245.7429" y ="0.0" z ="1143.2388" />
        </location>
        <rotation>
          <point  x="0.0" y ="180.0" z ="0.0" />
        </rotation>
      </ship>
      <ship name="salthi" pilot="" comm="kilrathi1" video="" autopilot="false" alignment="enemy"  wave="0">
        <location>
          <point  x="523.53925" y ="0.0" z ="1143.2388" />
        </location>
        <rotation>
          <point  x="0.0" y ="180.0" z ="0.0" />
        </rotation>
      </ship>
    </navpoint>
    <navpoint numships="4" numwaves="1">
      <ship name="jumpbuoy" pilot="" comm="" video="" autopilot="false" alignment="neutral"  wave="0">
        <location>
          <point  x="-37.39566" y ="0.0" z ="886.81134" />
        </location>
        <rotation>
          <point  x="0.0" y ="0.0" z ="0.0" />
        </rotation>
      </ship>
      <ship name="krant" pilot="" comm="kilrathi1" video="" autopilot="false" alignment="enemy"  wave="0" scriptid="k1">
        <location>
          <point  x="-657.09515" y ="0.0" z ="908.1803" />
        </location>
        <rotation>
          <point  x="0.0" y ="180.0" z ="0.0" />
        </rotation>
        <script ondestroy="enemydie"  />
      </ship>
      <ship name="krant" pilot="" comm="kilrathi1" video="" autopilot="false" alignment="enemy"  wave="0" scriptid="k1">
        <location>
          <point  x="-1180.6344" y ="0.0" z ="507.5125" />
        </location>
        <rotation>
          <point  x="0.0" y ="138.0" z ="0.0" />
        </rotation>
        <script ondestroy="enemydie"  />
      </ship>
      <ship name="krant" pilot="" comm="kilrathi1" video="" autopilot="false" alignment="enemy"  wave="0" scriptid="k1">
        <location>
          <point  x="753.25543" y ="0.0" z ="58.764606" />
        </location>
        <rotation>
          <point  x="0.0" y ="260.0" z ="0.0" />
        </rotation>
        <script ondestroy="enemydie"  />
      </ship>
    </navpoint>
    <navpoint numships="5" numwaves="1">
      <ship name="jumpbuoy" pilot="" comm="" video="" autopilot="false" alignment="neutral"  wave="0">
        <location>
          <point  x="-186.9783" y ="0.0" z ="1073.7897" />
        </location>
        <rotation>
          <point  x="0.0" y ="0.0" z ="0.0" />
        </rotation>
      </ship>
      <ship name="drayman" pilot="" comm="" video="" autopilot="true" must_survive="true" alignment="friend" last_autopilot_nav="4"  wave="0" jumpin="true" scriptid="d2">
        <location>
          <point  x="-165.60934" y ="0.0" z ="876.1269" />
        </location>
        <rotation>
          <point  x="0.0" y ="180.0" z ="0.0" />
        </rotation>
        <script ondestroy="dray2dies"  />
      </ship>
      <ship name="jalthi" pilot="" comm="kilrathi1" video="" autopilot="false" alignment="enemy"  wave="0">
        <location>
          <point  x="-667.7796" y ="0.0" z ="443.40567" />
        </location>
        <rotation>
          <point  x="0.0" y ="121.0" z ="0.0" />
        </rotation>
      </ship>
      <ship name="jalthi" pilot="" comm="kilrathi1" video="" autopilot="false" alignment="enemy"  wave="0">
        <location>
          <point  x="-731.8865" y ="0.0" z ="240.40067" />
        </location>
        <rotation>
          <point  x="0.0" y ="105.0" z ="0.0" />
        </rotation>
      </ship>
      <ship name="jalthi" pilot="" comm="kilrathi1" video="" autopilot="false" alignment="enemy"  wave="0">
        <location>
          <point  x="-758.59766" y ="0.0" z ="80.13355" />
        </location>
        <rotation>
          <point  x="0.0" y ="100.0" z ="0.0" />
        </rotation>
      </ship>
    </navpoint>
    <navpoint numships="1" numwaves="1">
      <ship name="claw" pilot="" comm="tc" video="" autopilot="false" land="true" must_survive="true" alignment="friend"  wave="0">
        <location>
          <point  x="-160.2671" y ="0.0" z ="1442.4039" />
        </location>
        <rotation>
          <point  x="0.0" y ="180.0" z ="0.0" />
        </rotation>
      </ship>
      <script oncreate="haveiwon"  />
    </navpoint>
  </navpoints>
  <objectives>
    <objective name="Escort Hospital ship out of the system" category="primary" default="failed" />
    <objective name="Escort Drayman back to the Claw" category="primary" default="failed" />
  </objectives>
</mission>
 

Kevin Caccamo

Rear Admiral
Whenever you have an destroy or create function associated with a ship, you must assign the ship a script id. That caused a lot of the problems. Here's the working mission.

Note, I also set the objectives to failed by default, that makes the script simpler.

Also note I used boolean variables here, it's much clearer that way.

Ok, I guess I should have gave the Krants script IDs :D.

Thank you so much for fixing my mission. It works just as I wanted it to now.

Also, can you please add this to the scripting documentation?
 
Top