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 and Course Changes

 
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 Air / Naval Operations >> Lua and Course Changes Page: [1]
Login
Message << Older Topic   Newer Topic >>
Lua and Course Changes - 4/24/2019 11:44:16 PM   
DWReese

 

Posts: 1050
Joined: 3/21/2014
Status: offline
I have just played "They Shall Not Pass" and it was lots of fun.

I am curious. In the Event Actions, the scenario designer apparently uses lots of Lua code that (I assume) tells the subs to to plot a different course. Below are the top two entries. Could someone tell me how to decipher this?

transiting = {
{name='K-38', guid='f28e114c-fa58-4ed0-ac10-2da65b2dff94'},
{name='K-317', guid='0164df90-3fee-47e8-8135-585681e97484'},

}

Following a whole bunch of other sub ploys, the long sequence ends with this:

math.randomseed(os.time())

for k,v in pairs(transiting) do
chance = math.random(1,5)
if chance == 1 then
CourseChange(v.guid)
end
end


Could someone also explain what this means?

I really believe that this scenario is excellent, and I would like to employ some of these features (if they do what I suspect that they do) into some of my future creations. But first, I have to be able to understand it. <lol>

Doug
Post #: 1
RE: Lua and Course Changes - 4/25/2019 1:49:24 AM   
Whicker

 

Posts: 495
Joined: 6/20/2018
Status: online
transiting is a table of units - basically a list. I believe it is actually a table of tables - the unit name and guid are a table, and then those are inside another table. { } is an empty table, generally if you see something enclosed is curly brackets it is a table. In other languages you would call this an array.

Tables like the one above have an index, so index #1 is the first item, #2 is the second. You can think of tables as key-value pairs, where the key in this case is the index (which is implied in this context I believe) and the value is the unit which is in the form of another table which has a name and guid.

If you have a table, you can iterate over it by saying "for key and value in the table named 'transiting' do something cool as you loop thru each unit in the list". Key and Value are shortened to k,v. Pairs is just a way to call them, there is also ipairs - they are different slightly but that is another story.

So it is taking the list of units in transiting, iterating through each one - while it is looking at each one it is checking if a random number between 1 and 5 is equal to 1 if so then call a function called CourseChange and in that function use the guid from the Value (v) of the unit that had the random chance equal to 1.

(in reply to DWReese)
Post #: 2
RE: Lua and Course Changes - 4/25/2019 1:58:15 AM   
Whicker

 

Posts: 495
Joined: 6/20/2018
Status: online
the function named CourseChange is a key part of this though - somewhere you should see something like:

function CourseChange(guid)
cool stuff in here, probably starts with ScenEdit_GetUnit(guid=guid)
end

for a long time this line: for k,v in pairs(transiting) do - looked like something I would never understand, when it is really a fairly simple concept once you can wrap your head around the fact that tables are key value pairs, and k and v mean key and value, and some tables like the transiting one have an implied key which is the index.

you could also write it like this and it would make more sense, its just shorter as k,v:

for key,value in pairs(transiting) do
chance = math.random(1,5)
if chance == 1 then
CourseChange(value.guid) -- make sure to change v to value to match top part
end
end

(in reply to Whicker)
Post #: 3
RE: Lua and Course Changes - 4/25/2019 11:30:37 AM   
DWReese

 

Posts: 1050
Joined: 3/21/2014
Status: offline
Thanks for the very detailed explanation. It is a little over my head at this point, but I intend on doing a little more research into it. I'm hoping that it is ultimately as easy as you say.

Thanks again.

Doug

(in reply to Whicker)
Post #: 4
Page:   [1]
All Forums >> [New Releases from Matrix Games] >> Command: Modern Air / Naval Operations >> Lua and Course Changes 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

0.105