Matrix Games Forums

Forums  Register  Login  Photo Gallery  Member List  Search  Calendars  FAQ 

My Profile  Inbox  Address Book  My Subscription  My Forums  Log Out

Lua Condition Failing

 
View related threads: (in this forum | in all forums)

Logged in as: Guest
Users viewing this topic: none
  Printable Version
All Forums >> [New Releases from Matrix Games] >> Command: Modern Operations series >> Lua Condition Failing Page: [1]
Login
Message << Older Topic   Newer Topic >>
Lua Condition Failing - 12/8/2016 1:35:20 PM   
ColonelMolerat

 

Posts: 479
Joined: 9/23/2015
Status: offline
I'm trying to set up an event which only fires if both of two radars are destroyed.

There are two triggers (each firing if one radar is destroyed). These are passed onto a LUA condition.

The LUA script itself works (in that I've set up 'answer' to return 'true' only if both radars are destroyed).

However, the trigger still fails because the 'conditions aren't met'.

How do I pass the 'true' of the condition onto the action? Are there any scenarios using it I could dissect? Most of the LUA info I can find is from before the patch introducing LUA conditions.

The script is below. It's messy as hell - I'm new to programming and I was up until far far too late pulling my hair out writing it - but it works...

The scenario is attached too.

local function Cond1(abc)
unit=ScenEdit_GetUnit({side="Pitiful Capitalist Pigs", name="Primary Radar 1"});print(unit)
if unit==nil
then
return false
else
return true
end
end

local function Cond2(def)
unit=ScenEdit_GetUnit({side="Pitiful Capitalist Pigs", name="Primary Radar 2"});print(unit)
if unit==nil
then
return false
else
return true
end
end

answer1=Cond1("abc"); print(answer1)
answer2=Cond2("def"); print(answer2)

if answer1==false
and answer2==false
then answer=true
elseif
answer1==true
and answer2==true
then answer=false
elseif answer1==true
and answer2==false
then answer=false
elseif answer1==false
and answer1==true
then answer=false
end

return answer

Edit:

I've changed 'return answer' at the end to:
if answer==true
then
ScenEdit_RunScript('Secondary Radar Activate.lua')

That works if I delete the radars manually, but not if they are destroyed.

Also I have to store the Trigger's Lua file in my CMANO directory. I don't seem to be able to point to a different location. Is there any way round this? I've also set the script as a scenario attachment, but that doesn't seem to work. (I'm looking into it now, will edit if I work it out)

Attachment (1)

< Message edited by ColonelMolerat -- 12/8/2016 2:58:51 PM >
Post #: 1
RE: Lua Condition Failing - 12/8/2016 2:46:45 PM   
stilesw


Posts: 1497
Joined: 6/26/2014
From: Hansville, WA, USA
Status: offline
Hi,

Perhaps there is a simpler solution without using Lua. Suppose you set up a dummy side called Admin or Counters. Then when "Pitiful Capitalist Pigs", "Primary Radar 1" is destroyed add 1 to the points for the Admin side. Same when "Primary Radar 2" is destroyed. Have a trigger check for the Admin side count getting to 2 and then fire the event you want. I've used this sort if thing a number of times and have had good results. BTW, I also use Lua for things but sometimes normal CMANO functions are easier to use. Anyway, hope this helps.

-Wayne Stiles

(in reply to ColonelMolerat)
Post #: 2
RE: Lua Condition Failing - 12/8/2016 2:59:46 PM   
ColonelMolerat

 

Posts: 479
Joined: 9/23/2015
Status: offline
Hey - thanks! That helps a lot!

I'd still like to know how to do this with LUA (I've got to learn eventually!), but for this particular purpose, that will save me a lot of heartache. Thanks for thinking of it!

Edit:

That's beautiful! It works so well!

I was working on that damn LUA from 1am-7am... Your solution took me 5 minutes...

< Message edited by ColonelMolerat -- 12/8/2016 3:08:13 PM >

(in reply to stilesw)
Post #: 3
RE: Lua Condition Failing - 12/8/2016 3:26:41 PM   
stilesw


Posts: 1497
Joined: 6/26/2014
From: Hansville, WA, USA
Status: offline
Sometimes the simpler solution works best. I programmed professionally for 20+ years but never used (or heard of) Lua until CMANO included it as a tool. I don't consider myself more than a novice with Lua but have found Baloogan's site and michaelm's more recent https://commandlua.github.io/beta/index.html to be invaluable in using the scripting language. You can also find several Lua reference books on Amazon. Two are "Programming in Lua" and "Lua Reference Manual", both by Roberto Ierusalimschey.

Glad I could be of help.

-Wayne Stiles

(in reply to ColonelMolerat)
Post #: 4
RE: Lua Condition Failing - 12/8/2016 3:41:49 PM   
ColonelMolerat

 

Posts: 479
Joined: 9/23/2015
Status: offline
Cheers! I had a look at MichaelM's list - I managed to get the script itself working, but couldn't see how to pass the result on, so to speak - it would work if I manually deleted the targets and ran the script manually, but when the targets were destroyed in-game, it would say the conditions had failed.

I've got a friend visiting this weekend who codes for a living, so he's offered to help me with the basics! He's seen my attempts before, "Oh god, no..." was his first response to hearing the details of my coding!

Edit:

Woah, having gone through a little bit of it with him via text, I can see quite how horrific that code I was using was!

< Message edited by ColonelMolerat -- 12/8/2016 4:15:37 PM >

(in reply to stilesw)
Post #: 5
RE: Lua Condition Failing - 12/8/2016 4:19:59 PM   
stilesw


Posts: 1497
Joined: 6/26/2014
From: Hansville, WA, USA
Status: offline
You might check https://commandlua.github.io/beta/index.html#ScenEdit_SetKeyValue. It should allow you to save and get a value for later use.

-Wayne

< Message edited by stilesw -- 12/8/2016 4:22:32 PM >

(in reply to ColonelMolerat)
Post #: 6
RE: Lua Condition Failing - 12/8/2016 4:39:23 PM   
ColonelMolerat

 

Posts: 479
Joined: 9/23/2015
Status: offline
Nice, that'll come in handy thank you!

With some advice from my friend, I've simplified my above script to:

local function Cond1(abc)
unit=ScenEdit_GetUnit({side="Pitiful Capitalist Pigs", name="Primary Radar 1"});print(unit)
if unit==nil
then
return false
else
return true
end
end

local function Cond2(def)
unit=ScenEdit_GetUnit({side="Pitiful Capitalist Pigs", name="Primary Radar 2"});print(unit)
if unit==nil
then
return false
else
return true
end
end

answer1=Cond1("abc"); print(answer1)
answer2=Cond2("def"); print(answer2)

if not (answer1 or answer2)
then
ScenEdit_RunScript('Secondary Radar Activate.lua')
end

It still fails in-game though - it works if I wait until the radars are destroyed, then press 'run script' on the condition (which in turn runs the action script). If I don't do that, though, when the radar is destroyed it says the conditions failed (even though that's the same script that passes if I run it myself).

(in reply to stilesw)
Post #: 7
RE: Lua Condition Failing - 12/8/2016 4:59:04 PM   
stilesw


Posts: 1497
Joined: 6/26/2014
From: Hansville, WA, USA
Status: offline
Just a brief note on programming style. I've always found (and it's generally considered good programming practice) that thoughtful use of structure will greatly improve readability and future debugging of code. I've modified your code sample to demonstrate this through indentation.

As for the code not working as desired within the scenario I suspect that that is caused by the specific way that Lua is implemented in CMANO. It seems to be a subset of "normal" Lua in that things that work in a pure Lua environment do not necessarily work the same way within CMANO. I could be wrong though.

Anyway, here is your code sample modified:

OK, that didn't show up correctly - my reply slammed everything to the left side. I've attached a document to show the sample. -WS

local function Cond1(abc)
unit=ScenEdit_GetUnit({side="Pitiful Capitalist Pigs", name="Primary Radar 1"});print(unit)
if unit==nil then
return false
else
return true
end
end

local function Cond2(def)
unit=ScenEdit_GetUnit({side="Pitiful Capitalist Pigs", name="Primary Radar 2"});print(unit)
if unit==nil then
return false
else
return true
end
end

answer1=Cond1("abc"); print(answer1)
answer2=Cond2("def"); print(answer2)

if not (answer1 or answer2) then
ScenEdit_RunScript('Secondary Radar Activate.lua')
end


-Wayne

Attachment (1)

< Message edited by stilesw -- 12/8/2016 5:03:55 PM >

(in reply to ColonelMolerat)
Post #: 8
RE: Lua Condition Failing - 12/8/2016 5:13:00 PM   
ColonelMolerat

 

Posts: 479
Joined: 9/23/2015
Status: offline
Haha, I copied that part of the script from a forum - it must have moved theirs to the left too!

Judging from my experience being taught with my friend, left to my own devices I make working scripts - but it takes me 100x as much time, they're 100x as long, and 100x messier!

---

I've found the problem with my script

I put all of this into 'actions' (no conditions, same triggers):

local function Cond1(abc)
unit=ScenEdit_GetUnit({side="Pitiful Capitalist Pigs", name="Primary Radar 1"});print(unit)
if unit==nil
then
return true
else
return false
end
end

local function Cond2(def)
unit=ScenEdit_GetUnit({side="Pitiful Capitalist Pigs", name="Primary Radar 2"});print(unit)
if unit==nil
then
return true
else
return false
end
end

answer1=Cond1("abc"); print(answer1)
answer2=Cond2("def"); print(answer2)

if (answer1 and answer2)
then
ScenEdit_SetEMCON('Unit', 'Secondary Radar 1', 'Radar=Active')
ScenEdit_SetEMCON('Unit', 'Secondary Radar 2', 'Radar=Active')
ScenEdit_SetEMCON('Unit', 'Secondary Radar 3', 'Radar=Active')
ScenEdit_SetEMCON('Unit', 'Secondary Radar 4', 'Radar=Active')
ScenEdit_SetEMCON('Unit', 'Secondary Radar 5', 'Radar=Active')
ScenEdit_SetEMCON('Unit', 'Secondary Radar 6', 'Radar=Active')
end


BUT - when the 'Unit destroyed' trigger is activated, my script is checking if the unit=nil

The unit doesn't report as nil at this point. It must happen a fraction of a second later. So the first time a unit is destroyed, both are listed as existing. When the second unit is destroyed, the first (already dead) one shows as destroyed, but the second still alive. If I then run the script manually, both are reported as destroyed ('nil').

I'm not there yet, but I'm making progress. Thanks for helping me through this!

EDIT!!!!
SUCCESS!!!! SLIGHTLY CHEATY SUCCESS!!!!
I made it so that if either one of the radars is reporting as destroyed ('nil') then the trigger activates as if both are destroyed.

Since the trigger always shows the currently destroyed radar as active, it is always 'one behind' - so if I want it to activated when two are destroyed, I tell it to activate when it thinks only one is. When only one radar is destroyed, it won't activate since it doesn't think any are.

I have a feeling this workaround will fail me one day, but it works for now. I've no idea how to make a unit report as 'nil' immediately when the 'destroyed' trigger is activated.

Of course, the non-Lua method worked fine several hours ago!

< Message edited by ColonelMolerat -- 12/8/2016 5:22:14 PM >

(in reply to stilesw)
Post #: 9
RE: Lua Condition Failing - 12/9/2016 12:14:26 AM   
kevinkins


Posts: 2257
Joined: 3/8/2006
Status: offline
Great discussion.

Couple of things from my end. I have been building up a small file of useful lua code snippets that should help speed up scenario design. It is a lot better to have the code in one place instead of going back into individual scenario files. The code above is worthy of safe keeping.

I also use "sides". To me they are like old fashion global variables since triggered lua scripts are local in scope - I think that's the term. Not sure if there is another way around that.

I really applaud ColonelMolerat trying to push the limits even though a solution was at hand. Lua and its Command API are documented. But how that translates into a game play experience is an art gained with a lot of trials.

And also to stilesw's point. Readable and reusable open source lua code is important even though each scenario is as unique as a fingerprint. Example code that runs without error is so valuable even if the specifics need adjustment. At least you know it's your errors when you make changes.

Kevin


< Message edited by kevinkin -- 12/9/2016 12:16:38 AM >

(in reply to ColonelMolerat)
Post #: 10
RE: Lua Condition Failing - 12/9/2016 1:13:02 AM   
stilesw


Posts: 1497
Joined: 6/26/2014
From: Hansville, WA, USA
Status: offline
Here is another bit of Lua code that I have found most useful.

Back when I first started to create scenarios there were many times when I wanted to create reference points in a circle around a unit. Defining a rectangular area just didn't look/feel right. Along the way someone posted a way to do this (ckfinite I believe) that worked but was a little cumbersome to use. Later I read another post about a script originally created by Baloogan and modified by Yauntay. I further added some changes to make it a little more usable for me. I allows you to select a unit by side and name and then create reference points around that unit by running it in the script console. In the attached code just change the items in red to what you want. Hope folks find this useful. Thanks.

-Wayne Stiles

Attachment (1)

(in reply to kevinkins)
Post #: 11
RE: Lua Condition Failing - 12/9/2016 8:01:03 AM   
michaelm75au


Posts: 13500
Joined: 5/5/2001
From: Melbourne, Australia
Status: offline

quote:

ORIGINAL: stilesw

You might check https://commandlua.github.io/beta/index.html#ScenEdit_SetKeyValue. It should allow you to save and get a value for later use.

-Wayne


Just a note. The 'beta' Command Lua is a working copy so it may have things in it that are not yet available. My intend is to post the new index to the normal Command Lua once they are actually available in the game.

_____________________________

Michael

(in reply to stilesw)
Post #: 12
RE: Lua Condition Failing - 12/9/2016 8:04:47 AM   
michaelm75au


Posts: 13500
Joined: 5/5/2001
From: Melbourne, Australia
Status: offline
You can use ScenEdit_UnitX() to get the unit that triggered the event. In this case the unit destroyed
local unit = ScenEdit_UnitX()

_____________________________

Michael

(in reply to ColonelMolerat)
Post #: 13
RE: Lua Condition Failing - 12/9/2016 11:12:48 AM   
ColonelMolerat

 

Posts: 479
Joined: 9/23/2015
Status: offline
quote:

You can use ScenEdit_UnitX() to get the unit that triggered the event. In this case the unit destroyed
local unit = ScenEdit_UnitX()


I tried running this as the first line in the event action, then setting that unit to nil. It didn't change anything, but I'm not sure whether that's because of the problem I'm facing (units being set to nil only after the rest of the code has run) or because I just coded it incorrectly!

When my friend visits tomorrow I'm going to have a look at that section of the code with him (he helped me tidy up the second section yesterday afternoon) and have a little tinker, so I'll try it out again.

I've got it all working fine checking for '-1 units destroyed' - ie, a trigger I want activated at 4 units destroyed I set to check for 3 units being destroyed. But I figured I might as well see if it's possible without such a workaround in case it comes in useful in the future.

Thanks for all your help with this, everybody! I'm finding the coding a real struggle, but gradually making progress.
One of the things I do really love about CMANO is the open nature of so much of it - from continuing missions after they've finished, to playing them in the editor, to having such power to create editor events.

(in reply to michaelm75au)
Post #: 14
RE: Lua Condition Failing - 12/12/2016 7:24:45 PM   
ColonelMolerat

 

Posts: 479
Joined: 9/23/2015
Status: offline
Right! I've realised, there's no point making the destroyed unit report 'nil' immediately - if it's activating the 'unit is killed' trigger, then it must have been destroyed, so the action only needs to check that the other units are destroyed anyway.

As for the rest of my code, my friend's taught me how to simplify it as such:

*****
--This bit checks if each radar is alive or not - if dead, they report as 'nil' and 'true' is returned.

local function Cond1()
unit=ScenEdit_GetUnit({side="Pitiful Capitalist Pigs", name="Primary Radar 1"});print(unit)
if unit==nil
then
return true
else
return false
end
end

local function Cond2()
unit=ScenEdit_GetUnit({side="Pitiful Capitalist Pigs", name="Primary Radar 2"});print(unit)
if unit==nil
then
return true
else
return false
end
end

*****
--This bit creates an array containing all of my secondary radars. Basically, a single entity containing a list of all the secondary radars I want. Easy enough to add more to.

radars = {'Secondary Radar 1','Secondary Radar 2','Secondary Radar 3','Secondary Radar 4','Secondary Radar 5','Secondary Radar 6'}

*****
--This bit sets 'answer1' and 'answer2' to whatever Cond1 and Cond2 returned earlier ('true' or 'false' depending on if their Primary Radar is destroyed). The 'print' command isn't needed, but displays the response - useful when bugchecking.

answer1=Cond1(); print(answer1)
answer2=Cond2(); print(answer2)

*****
-- This is the fun bit. 'if (answer1 and answer2)' checks to see if both answer1 and answer2 are TRUE - earlier in the script, you can see they are set to TRUE if the primary radars are destroyed.

-- The next bit's a little more complicated - the "1, 6, 1" means that the script will run from the first to the sixth item in the list that will follow, going up by one each time - ie, 1-2-3-4-5-6.

-- Finally, the three parts of SetEmcon are what it applies to - in this case 'Unit' (so a single unit), the name of the unit (normally eg "Secondary Radar 1"), and what sensor to set (in this case radar becomes active). The 'name' part in this case is a bit different - by writing 'radars[variable]', we are telling it to choose an item from the array 'radars' (mentioned earlier, containing Secondary Radar 1-6). The [variable] then tells it to run through from the first to the sixth item in that array, increasing by one each time - so first the code will apply to Secondary Radar One, then to Secondary Radar 2, then 3, 4, 5 up to Secondary Radar 6 - as set by the earlier 'for variable=1,6,1' part of code.

if (answer1 and answer2)
then
for variable=1,6,1 do
ScenEdit_SetEMCON('Unit',radars[variable],'Radar=Active')
end
*****

I hope that explanation is clear enough, let me know if anyone wants anything clarifying.

To add more radars to turn on, add them to the 'radars' array (the one containing 'Secondary Radars 1-6') then increase the 'for variable=1,6,1 do' to list how many radars you now have - eg 'for variable=1,9,1 do' if you have nine radars.

I have that code set as an action (Lua Script type) that is triggered in the mission editor when one of my two 'Primary Radars' are destroyed.

All together it's:

local function Cond1()
unit=ScenEdit_GetUnit({side="Pitiful Capitalist Pigs", name="Primary Radar 1"});print(unit)
if unit==nil
then
return true
else
return false
end
end

local function Cond2()
unit=ScenEdit_GetUnit({side="Pitiful Capitalist Pigs", name="Primary Radar 2"});print(unit)
if unit==nil
then
return true
else
return false
end
end

radars = {'Secondary Radar 1','Secondary Radar 2','Secondary Radar 3','Secondary Radar 4','Secondary Radar 5','Secondary Radar 6'}

answer1=Cond1(); print(answer1)
answer2=Cond2(); print(answer2)

if (answer1 and answer2)
then
for variable=1,6,1 do
ScenEdit_SetEMCON('Unit',radars[variable],'Radar=Active')
end
end

< Message edited by ColonelMolerat -- 12/12/2016 7:26:09 PM >

(in reply to ColonelMolerat)
Post #: 15
Page:   [1]
All Forums >> [New Releases from Matrix Games] >> Command: Modern Operations series >> Lua Condition Failing Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts


Forum Software © ASPPlayground.NET Advanced Edition 2.4.5 ANSI

1.094