Random Lua Elements-How, When, and Why

Take command of air and naval assets from post-WW2 to the near future in tactical and operational scale, complete with historical and hypothetical scenarios and an integrated scenario editor.

Moderator: MOD_Command

Post Reply
RoryAndersonCDT
Posts: 1828
Joined: Mon Jun 15, 2009 11:45 pm

Random Lua Elements-How, When, and Why

Post by RoryAndersonCDT »

Coiler12 examines using Lua to provide random elements in Command scenarios, and how to manage how much of a scenario should be set up randomly.

Random Lua Elements-How, When, and Why by Coiler12


When I saw the Lua tutorial video showing how it could be used to make random elements in Command, I was excited. My excitement diminished as I started making an overambitious scenario involving random Lua and recognized both the limitations and difficulty. But with that lesson in mind, I continued using random Lua.

While still frustrating at times, in part due to me not being a programmer, I found the benefits to be worth it. (Ckfinite, who is a programmer, has been a great help in getting my Lua efforts to work, and has provided many necessary technical corrections to this post. )

Event Editor vs. Lua.
So, in layman’s terms, the existing event editor allowed for an “either or” option. Operation Square Peg: Syrian MiG-29s are sitting at Damascus International, and a probability event fires to see if they’re dumb enough to mess with F-22s. The “attempted intercept” mission switches from inactive to active, and that’s all it can do.

Lua would allow that same group of MiG-29s to either all take off, none of them take off, have a portion of them take off, or have them take off to fly to another airbase rather than engaging the attackers.



Read more on baloogancampaign.com
Command Dev Team
Technical Lead
User avatar
tjhkkr
Posts: 2431
Joined: Wed Jun 02, 2010 11:15 pm
Contact:

RE: Random Lua Elements-How, When, and Why

Post by tjhkkr »

It is very good, but there are ways to streamline the code such that if you had 30 aircraft you wanted to add, it would not take 30 if/then/else platforms to make it work...[8D]
Remember that the evil which is now in the world will become yet more powerful, and that it is not evil which conquers evil, but only love -- Olga Romanov.
User avatar
snowburn
Posts: 188
Joined: Mon Sep 23, 2013 9:10 pm
Location: Bovril, Argentina

RE: Random Lua Elements-How, When, and Why

Post by snowburn »

Thanks for the article!

Can a lua script LUA set random skill values to units? i need something like:


function SetSkill(side_name,unit_name, min, max)
local unit = ScenEdit_GetUnit({side=side_name,name=unit_name})
if unit ~= nil then
unit.skill= math.random(min,max)
end
end

math.randomseed( os.time() )
SetSkill('Soviet Union', 'Jet #1', '1', '3')
SetSkill('Soviet Union', 'Jet #2', '1', '3')
SetSkill('Soviet Union', 'Jet #3', '3', '4')
ckfinite
Posts: 208
Joined: Fri Jul 19, 2013 10:33 pm

RE: Random Lua Elements-How, When, and Why

Post by ckfinite »

Yes, you can use Lua to set proficiency. See my documentation here. The field is called "proficiency", and you can access it through ScenEdit_GetUnit.
User avatar
snowburn
Posts: 188
Joined: Mon Sep 23, 2013 9:10 pm
Location: Bovril, Argentina

RE: Random Lua Elements-How, When, and Why

Post by snowburn »

Thanks ckfinite!
User avatar
snowburn
Posts: 188
Joined: Mon Sep 23, 2013 9:10 pm
Location: Bovril, Argentina

RE: Random Lua Elements-How, When, and Why

Post by snowburn »

How to set random skill levels for units:

Event: Scenario Start

Trigger: Scenario is loaded

Action: Lua Script:

function SetSkill(side_name,unit_name, min, max)
local unit = ScenEdit_GetUnit({side=side_name,name=unit_name})
if unit ~= nil then
local unit_id=unit.guid
ScenEdit_SetUnit({guid=unit_id, proficiency = math.random(min,max) })
end
end

math.randomseed(os.time())
SetSkill('Irak', 'Fishbed #1', 2, 4)
SetSkill('Irak', 'Fishbed #2', 2, 3)
SetSkill('Irak', 'Fishbed #3', 1, 3)
SetSkill('Irak', 'Fishbed #4', 0, 2)
SetSkill('Irak', 'Fishbed #5', 0, 2)
SetSkill('Irak', 'Fishbed #6', 0, 2)
solidgeoff
Posts: 11
Joined: Thu Feb 19, 2015 1:19 pm

RE: Random Lua Elements-How, When, and Why

Post by solidgeoff »

Thanks for posting. Randomizing elements in a scenario is a great way to improve replayability.
Post Reply

Return to “Command: Modern Operations series”