LUA - Use of KeyValue to track settings in scenario

Post new mods and scenarios here.

Moderator: MOD_Command

Post Reply
User avatar
michaelm75au
Posts: 12457
Joined: Sat May 05, 2001 8:00 am
Location: Melbourne, Australia

LUA - Use of KeyValue to track settings in scenario

Post by michaelm75au »

Another useful LUA feature is the ability to keep track of external information which is kept as part of the save.
For example, say you have a condition that is triggered, and you need to check how often the condition occurs at some later time. Or you need to 'teleport' units.

The command set is "ScenEdit_SetKeyValue()/ScenEdit_GetKeyValue()".

In a recent case, I used this to keep track of how many times I created a particular unit. The things I needed to keep track of were the name of the unit, the database type, maximum number to create and how many have already been created. So when the scenario is first started, I ran the following script to initialize it:

Code: Select all

ScenEdit_SetKeyValue("CanberraAir", "dbid=673, maxNum=10, curNum=0");
When an event is triggered that needs to create a unit, I recall the key and use the values. If I have not yet added the maximum number of units, I create a new unit with the call sign and current number. Update the key with a new curNum and save it.

Code: Select all

 -- get the current settings and decode the values
 local drop = ScenEdit_GetKeyValue("CanberraAir");print(drop);
 local t = {}
 for k, v in string.gmatch(drop, "(%w+)=(%w+)") 
 do
  t[k] = v
 end
 -- deploy one unit
 local lastN = tonumber(t['curNum']);
 if  lastN == nil then
   lastN = 0;
 end
  lastN = lastN +1;
 if lastN > tonumber(t['maxNum']) then
  ScenEdit_MsgBox('All troops landed',0)
  return
 end
 
 local veh =ScenEdit_AddUnit({type = 'Facility', name = 'Assault #' .. lastN, 
   heading = 0, dbid = t['dbid'], side = 'RAN', Lat=new.lat, Lon=new.lon});print(veh)
 -- create string with new value to store
 local b="curNum="..tostring(lastN)
 -- replace the existing string
 local a=string.gsub(drop,"curNum=%d", b)
 ScenEdit_SetKeyValue("CanberraAir", a);
 

I have a Steam Workshop scenario under michaelm75 that demonstrates this.

Hope this is helpful.
Michael
mikmykWS
Posts: 7185
Joined: Tue Mar 22, 2005 4:34 pm

RE: LUA - Use of KeyValue to track settings in scenario

Post by mikmykWS »

It is. Thanks for this Mike!

Mike
ckfinite
Posts: 208
Joined: Fri Jul 19, 2013 10:33 pm

RE: LUA - Use of KeyValue to track settings in scenario

Post by ckfinite »

If anyone is interested, I wrote a library that abstracts over all of the stringy business, available here.

It can handle everything except functions. To give an example using the above scenario, it would allow the above initialization code to be simplified to

KeyStore.CanberraAir = {dbid=673, maxNum=10, curNum=0}

And the usage to be simplified to

Code: Select all

 local t = KeyStore.CanberraAir
 -- deploy one unit
 local lastN = t.curNum;
 if  lastN == nil then
   lastN = 0;
 end
  lastN = lastN +1;
 if lastN > t.maxNum then
  ScenEdit_MsgBox('All troops landed',0)
  return
 end
 
 local veh =ScenEdit_AddUnit({type = 'Facility', name = 'Assault #' .. lastN, 
   heading = 0, dbid = t.dbid, side = 'RAN', Lat=new.lat, Lon=new.lon});print(veh)
 
 t.curNum = lastN
 KeyStore.CanberraAir = t
 
User avatar
Gunner98
Posts: 5966
Joined: Fri Apr 29, 2005 12:49 am
Location: The Great White North!
Contact:

RE: LUA - Use of KeyValue to track settings in scenario

Post by Gunner98 »

Guys

Am showing my programing numpty-ism here but I think you're solving a problem that I currently have.

Requirement:

Deploying a USMC Regiment in an amphibious operation

Current solution:

Naming and placing all the platoon sized elements on a different 'blind' side
Teleport linked to specific helo or boat names to specific named units
change the side of that unit
Sluff off all the return trips the carrier units would do a supply runs

I did this for two companies in 'NF 10.5 Sweep up' and it is awkward. Doing it for an RCT is daunting

I think the script your explaining here will gradually build the units as the carriers trigger the base event. Is that so?

Thanks

B
Check out our novel, Northern Fury: H-Hour!: http://northernfury.us/
And our blog: http://northernfury.us/blog/post2/
Twitter: @NorthernFury94 or Facebook https://www.facebook.com/northernfury/
User avatar
michaelm75au
Posts: 12457
Joined: Sat May 05, 2001 8:00 am
Location: Melbourne, Australia

RE: LUA - Use of KeyValue to track settings in scenario

Post by michaelm75au »

Correct. Here is my sandbox.
It also shows how to handle one of the delivering units being killed and possibly also killing the unit being delivered.
Attachments
Sandbox..gforces.zip
(22.32 KiB) Downloaded 13 times
Michael
User avatar
michaelm75au
Posts: 12457
Joined: Sat May 05, 2001 8:00 am
Location: Melbourne, Australia

RE: LUA - Use of KeyValue to track settings in scenario

Post by michaelm75au »

ORIGINAL: ckfinite

If anyone is interested, I wrote a library that abstracts over all of the stringy business, available here.

It can handle everything except functions. To give an example using the above scenario, it would allow the above initialization code to be simplified to

KeyStore.CanberraAir = {dbid=673, maxNum=10, curNum=0}

And the usage to be simplified to

Code: Select all

 local t = KeyStore.CanberraAir
 -- deploy one unit
 local lastN = t.curNum;
 if  lastN == nil then
   lastN = 0;
 end
  lastN = lastN +1;
 if lastN > t.maxNum then
  ScenEdit_MsgBox('All troops landed',0)
  return
 end
 
 local veh =ScenEdit_AddUnit({type = 'Facility', name = 'Assault #' .. lastN, 
   heading = 0, dbid = t.dbid, side = 'RAN', Lat=new.lat, Lon=new.lon});print(veh)
 
 t.curNum = lastN
 KeyStore.CanberraAir = t
 
My initial goal was to demonstrate the ease of using LUA to perform the old 'teleporting' action.

I forgot about your libraries. Which is strange as I recall looking them up a few weeks ago.[:o]
Michael
ckfinite
Posts: 208
Joined: Fri Jul 19, 2013 10:33 pm

RE: LUA - Use of KeyValue to track settings in scenario

Post by ckfinite »

I also wrote one that lets you store values directly on the unit object, though it's a bit hacky. It's here, lines 233 through 282

For example, if you have an amphibious unit called (for the sake of argument) HMAS Canberra on the side named RAN, then you could (to initialize it) write

Code: Select all

 local unit = ScenEdit_GetUnit({side="RAN", name="HMAS Canberra"})
 unit.embarked = {2396, 2396}
 

Then, when the unit entered the AOE, you can have

Code: Select all

 local unit = ScenEdit_UnitX()
 if unit.embarked ~= nil and #(unit.embarked) > 0 then
  for _,embarked in pairs(unit.embarked) do
    ScenEdit_AddUnit({side="RAN", name="Something or other", dbid=embarked, latitude = unit.latitude, longitude=unit.longitude})
  end
 end
 

At the moment, I'm trying to make this more modular with a behaviour system that's still kind of half-formed.
User avatar
michaelm75au
Posts: 12457
Joined: Sat May 05, 2001 8:00 am
Location: Melbourne, Australia

RE: LUA - Use of KeyValue to track settings in scenario

Post by michaelm75au »

Nice.
This would make the carrying unit being destroyed much cleaner as you know what unit it is carrying, to 'kill' off.[:D]
At the moment, I have same types being carried so I just up the current count to represent it has been 'killed' so it wont be deployed.

Interesting. I'll definitely will be looking at this!![;)]
Michael
Post Reply

Return to “Mods and Scenarios”