Matrix Games Forums

Forums  Register  Login  Photo Gallery  Member List  Search  Calendars  FAQ 

My Profile  Inbox  Address Book  My Subscription  My Forums  Log Out

Loading A Scenario

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 >> Mods and Scenarios >> Lua Legion >> Loading A Scenario Page: [1]
Message << Older Topic   Newer Topic >>
Loading A Scenario - 4/12/2021 9:22:14 AM   


Posts: 325
Joined: 3/26/2015
Status: offline
Morning Folks,

I was wondering is it possible to load a scenario via Lua? I have a idea where a side has 3 possible force levels, I was thinking of perhaps randomising the force levels when the scenario starts so I was thinking the way to achieve that is have 3 different scenarios one for each level and a fourth "Lobby Scenario" that the player starts, then 1 second in to the scenario a trigger runs the event, "Randomise forces" where the trigger is a time trigger, the event action is a random

"Math Seed 0-10

- If <3 then

(XXXXX Scen_Load "Low Unit Readiness")

If >3 then

(XXXXX Scen_Load "Medium Unit Readiness")


(XXXXX Scen_Load "High Unit Readiness")


Ect Ect

Is this even possible?

As an alternative I could make the groups and save them but am not sure if its possible to load a saved group using Lua

Any thoughts would be appreciated



< Message edited by butch4343 -- 4/12/2021 9:23:01 AM >
Post #: 1
RE: Loading A Scenario - 4/13/2021 8:26:23 AM   


Posts: 992
Joined: 11/15/2018
Status: offline

I was wondering is it possible to load a scenario via Lua?

So to that specifically nope.

BUT... you can execute scripts upon the scene loading.

You can absolutely accomplish the end result you describe 99% of the time using the on load\scenario startup trigger, so that your script(s) for randomization and unit creation run before control is returned to the user, or if you wanted something more manual having the user select options via Special Action menu item(s). Your "lobby" then is the singular baseline scenario, with all\most the units\groups\missions\sides\other customization created by script on scene startup based on whatever the random diceroll(s) results are during that load. End result is upon each load of your original (not their saves-if done right), the player gets one of the possible random variants, the beauty here is you can make just say 5 different total variants (as a whole) or have the randomization granular and as deep as you care to code, all up to you.

So it's very doable, and generally speaking not that "complicated" once you get familiar with the basics of Lua and the CMO api functions, but it can get involved depending on all the things you may want to have be variable and how large or complex the scene. It gets even more advanced if doing that in concert with randomizing of related events you may have in mind that apply to things that may or may not have been selected for generation. Again almost always doable, just can get involved (read more and more code).

There are a lot of scenarios that contain various "Scenario load" randomization of all sort of things you can use as basic examples to learn from if needed. Also lots of previously answered questions here for how to script create units\groups\mission\etc (use search feature with this sub lua forum and the mod forum selected). For example here is a sample I posted some months back (post #6) that touches the subject for a specific thing someone was looking to randomize at load time, including keeping track in any saves if the script\randomization has already been done (so that it doesn't run every time someone reloads a save). Keep in mind it was just a sample for a how-to to something specific and not the entire wider scene as a whole.

the ~1% where it may not work is specific only to when you're trying to create or modify something very specific that can't be done via Lua exactly the way you desire or can in the gui, it's somewhat rare, but it happens sometimes.

Advice - Just dig in, and if new to Lua scripting, bite off one small or medium thing at a time, break scripts and function in them into into logical package of of work where you can. Ask here when you run into trouble or get stumped about how to accomplish a particular thing.

(in reply to butch4343)
Post #: 2
RE: Loading A Scenario - 4/13/2021 9:46:47 AM   


Posts: 152
Joined: 4/30/2020
Status: offline
I create scenarios through Lua all the time...
It can turn into a lot of code, which means you might want to consider organizing the code into re-usable chunks and loading them first. But don't worry about that at first.

1st: Get yourself a tool that does context highlighting for Lua. Many us vscode, I use Notepad++. Both are free. vscode seems to work better with a standard Lua environment. As we are developing Lua scripts in a limited environment, I have found Notepad++ sufficient.

2nd: EVERY time you write something useful, or find something useful on the forums, copy it into a local file with documentation sufficient to remember what it does. Generally this is at minimum, a description of what the function does and a listing of the parameters, parameter types, and parameter meaning/usage. Place all this code in a single location. This can be a single file, or a collection of files in a single directory. The choice here is yours.

3rd: Understand how to set up an event at ScenarioLoad Time. There is help for this on the forum. The first thing you will want to do is load up any lua functionality that you have created during this ScenarioLoad event.

4th. You must determine if this is an initial load (user staring the scenario for play) or a reload of a saved state. In the general case, this can be done by examining the scenario time and comparing it to your start time. if they match it is probably a new load. If it does not match, they have started running the scenario. This fails if the player opens the scenario and closes and saves without pushing the go/run button. The way around this is to store a loaded value with the ScenEdit_SetKeyValue function called during the ScenarioLoad event.

5th. You will need to follow the same steps in Lua that you would normally follow with the GUI...
start with the sides, then postures, then create units, then events and so forth. This is where you create a lot of code.

6th. Test the scripts by running them from the console in a blank scenario. When it all works, tie the code to the LoadScenario event.

I have build a mechanism for creating a library of Lua code and load it. A version of it is available on the Forums at:

As KnightHawk75 said, what you are attempting IS possible, though it may stretch your coding ability. There may be a few times when you may need to adjust your thinking a bit as about 98% of the GUI's capability is implemented as Lua functions. You will get very familiar with the interface and the CommandLua documentation site: And take it one step at a time.

Good Luck!

(in reply to KnightHawk75)
Post #: 3
RE: Loading A Scenario - 4/13/2021 3:10:16 PM   


Posts: 992
Joined: 11/15/2018
Status: offline
All good advice..

But definitely +100 on #1 - VSCode with the free Lua Language Server by sumneko.lua Extension works pretty darn well, has syntax highlighting and can bring up contextual lua documention for keyword\language in split window with a click or two. Especially nice after awhile and you've flagged most the ScenEdit_ and VP_ CMO api function names as globals to avoid them being flagged as undefined vars. It's my personal goto for CMO when trying to do anything more than a couple dozen lines, I've started out using NP++ before moving over to it. That said of course use whatever editor find works best for you\themselves.

(in reply to jkgarner)
Post #: 4
RE: Loading A Scenario - 4/13/2021 5:14:10 PM   


Posts: 385
Joined: 10/10/2019
From: Netherlands
Status: offline
Thx for all the advice
with regards GJ

(in reply to KnightHawk75)
Post #: 5
RE: Loading A Scenario - 4/14/2021 11:54:53 AM   


Posts: 325
Joined: 3/26/2015
Status: offline

Thanks for all the help with this, am starting to delve deep into the U tube videos avaialble, think I really need to get my head around LUA



(in reply to Parel803)
Post #: 6
Page:   [1]
All Forums >> [New Releases from Matrix Games] >> Command: Modern Operations series >> Mods and Scenarios >> Lua Legion >> Loading A Scenario 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