Rory Noonan
Posts: 2816
Joined: 12/18/2014 From: Brooklyn, NY Status: offline
|
quote:
scenarioEmitters = { --A table including the names of emitters and whether they're military or civilian { name = 'DA.05 [RAWS-3] (Radar, Target Indicator, 2D Surface-to-Air & Surface-to-Surface)', emitterType = 'Military' }, { name = 'Decca 1229 (Radar, Surface Search & Navigation)', emitterType = 'Commercial' }, --In this example this is the only non-military emitter { name = 'LW.03 [SGR.114] (Radar, Air Search, 2D Medium-Range)', emitterType = 'Military' }, { name = 'M.45 (Radar, FCR, Weapon Director)', emitterType = 'Military' }, } local result = false --Set the initial/default result to false local contact = ScenEdit_UnitX() --Get the details of the detected unit local receiver = ScenEdit_UnitY() --Get the details of the detecting unit local sideContacts = ScenEdit_GetContacts(receiver.unit.side) --Return a list of all contacts for the detecting units side for k,v in ipairs (sideContacts) do --Go through that list and.. contactData = ScenEdit_GetContact({side=receiver.unit.side, guid=v.guid}) --Get the full data for each contact if contactData.actualunitid == contact.guid then --See if this unit is the one that was detected, if it is then... emissions = contactData.emissions --Store a list of the emissions detected break --And break out of the loop (more useful in larger scenarios) end end if emissions ~= nil then --If the list of emissions isn't empty for key,value in ipairs (emissions) do --For each entry in that list for list, emission in ipairs (scenarioEmitters) do --Compare it to each entry in the list of emitters in the scenario if emission.name == value.Name and emission.emitterType == 'Military' then --If the names match, and the type is military emitter then... print (contact.name .. ' determined to be using military type emitter: '..value.Name) --Print out a message to the console result = true --Change the result to true end end end end return result --Return the result (be it true or false) Above is a commented version of the code which steps you through what each line means and does. The first step would be to get a list of the names of the emitters you want to classify as military type emitters; you could do this manually (which risks data entry errors, remember Lua is a case sensitive language) or do what I did and use GetContact.emissions to list emitters and then edit the table to suit your needs (have a look here for an introduction to tables). As mentioned above, the use of UnitX, UnitY and GetContacts was a work-around because I had trouble getting UnitC to work (I'm sure this is user error on my part, but this goes to show that there's nearly always more than one way to approach a problem in Lua). the 'for k,v in ipairs (sideContacts) do' command really confused me when I first started using Lua. In a basic sense, the k and v represent keys and values (refer to the link above for a description of what this means) in a table. The function ipairs goes through the table entered in the brackets (sideContacts here) and performs the relevant code on each entry. As a simple demonstration, type this into your console: quote:
list = { 'A','B','C','D' } for k,v in ipairs(list) do print (k) print (v) end Running this code will get you an output of: quote:
1 A 2 B 3 C 4 D The numbers are the keys, and the letters are the values. You can substitute 'k' and 'v' for whatever you like, and if nesting these functions you absolutely must substitute them for something--having multiple nested functions trying to reference the same variables simply won't work. As always try to use something that is meaningful; a few extra keystrokes here now will save you a lot of confusion when you revisit the code weeks or months down the track. Once we're going through the list, we then compare some values from the tables. Note that the if comparative statement checks to see whether the table values contactData.actualunitid and contact.guid are equal to each other using double equals signs ==. A common mistake (I do this when I'm tired) is to use one equals sign, which will return an error. Essentially == is comparative, and = is declarative, so we use == to see if two things are equal, while we use = to say this variable is 'whatever'. Note also that if the criteria for the if comparative statement are not met, nothing happens. If the if criteria are met, we then store a list of emissions from that contact as the variable emissions and break out of the for loop. This is of little consequence in the demonstration .scen file with only one possible contact, but in large scenarios this will cut down on unnecessary evaluation (it's only possible to have one match for GUID as GUIDs are unique; any further evaluation is pointless after there's a match). Next we check to see if there's anything in the emissions variable. If it returns nil nothing happens and the script ends with the default result of false. Situations where this might occur include the contact being spotted visually or detected via radar where there's no emissions, in which case this script is not relevant. If there's data stored in the emissions variable, it is then evaluated using nested ipairs functions. What this does is take each entry in the emissions table, and check it against each entry in the list of scenarioEmitters. If the names match, and the listed type for that name is 'Military' then we print out a message for the console user (generally invisible to a player in a finished scenario, useful for debugging) and change the result to true. Finally, the result is returned. This will simply state 'Yes' if true in the console, or if used in a condition will allow the event action to be performed. If the result is false, it will state 'No' in the console and block the action from being performed if used in a condition. I hope that explains everything, please feel free to ask questions if there's something I didn't explain clearly I've also attached the .lua file in a zip file, the forum doesn't handle code formatting well so opening this in notepad++ or notepad will allow you to see the indents etc that make reading code much easier.
Attachment (1)
_____________________________
|