Unit Doctrine as String

All discussions & material related to Command's Lua interface

Moderators: RoryAndersonCDT, michaelm75au, angster, MOD_Command

Post Reply
jkgarner
Posts: 175
Joined: Thu Apr 30, 2020 12:42 pm

Unit Doctrine as String

Post by jkgarner »

OK, Does anyone have an idea how to get a unit's doctrine as a string?

ScenEdit_SetDoctrine takes a table as a parameter.
ScenEdit_GetDoctrine returns a table.

FYI: an empty table is the default doctrige for all units, this corespondes to all doctrine values are inherited from the parent.

To Set the doctrine of a unit in an event to inherit all values from the parent, I must create a script for the action.

This appears as:

Code: Select all

script = "ScenEdit_SetDoctrine({})"
Or I could go in with guns blazing with:

Code: Select all

script = "ScenEdit_SetDoctrine({ "..
     "weapon_control_status_land = '0',"..
     "engage_opportunity_targets = '1' })"
Now it gets interesting. I could set up an event where the script was implemented as:

Code: Select all

script="ScenEdit_SetDoctrine("..doctrineStr..")"
This allows me to pass in any doctrine and have it set. I can even grab the various strings to pass in from the GUI, by setting various doctrine values and doing

Code: Select all

print(ScenEdit_GetDoctrine({side="???", name="???",})
)filling in the ???.

This is all fine and dandy, but what if in a later event, I want to return the doctrine to whatever it was before I mucked with it? TO do this using my odd event, I need the doctrine table as a string. Something equivalent to what is printed to the screen/console when I execute a print on the ScenEdit_GetDoctrine call.

So one might say, just grab the table, and call tostring on it. It doesn not work. I ran the following code:

Code: Select all

docTable = ScenEdit_GetDoctrine({side='USA', name='73rd Batallion 2nd Company 1st Platoon (M2A3 Bradley AFV x 4)'})
 docStr = tostring(docTable)
 print(docStr)
 print(docTable)
and got the following results:

Code: Select all

table: 2BB97C78
 { ignore_plotted_course = '1', engage_opportunity_targets = '1', weapon_control_status_land = '0' }
The tostring gives the Table and an address, while the print gets me what I want.

So the short of all this is: does anyone know how to get the equivalent of a print(ScenEdit_GetDoctrine(???)) call into a string value without calling print?
[&:]

KnightHawk75
Posts: 1850
Joined: Thu Nov 15, 2018 7:24 pm

RE: Unit Doctrine as String

Post by KnightHawk75 »

So you just want a tbl as a pure string not convertible back to a table, for me I just implement my own.

Code: Select all

 function gKH.base:dumptable(o)
     if o == nil then return "nil";end
     if type(o) == 'table' then
        local s = '{ '
        local maxcount = 0;
        local count = 1;
        for k,v in pairs(o) do maxcount=maxcount+1; end
        for k,v in pairs(o) do
          if count == maxcount then
           s = s .. k ..' = ' .. self:dumptable(v) --no trailing ","
          else
           s = s .. k ..' = ' .. self:dumptable(v) .. ', '
          end
          count = count + 1
        end
        return s .. ' }'
     else
        return tostring(o)
     end
   end
 
You might have to tweak the above it if you want string type values wrapped in ' ' and numbers not, though to completely match 'print', and obviously rename it to whatever you want.

docTable = ScenEdit_GetDoctrine({side='USA', name='73rd Batallion 2nd Company 1st Platoon (M2A3 Bradley AFV x 4)'})
docStr = gKH.base:dumptable(docTable);
print(docStr)
print(docTable)

jkgarner
Posts: 175
Joined: Thu Apr 30, 2020 12:42 pm

RE: Unit Doctrine as String

Post by jkgarner »

thanks. this is a good start.
jkgarner
Posts: 175
Joined: Thu Apr 30, 2020 12:42 pm

RE: Unit Doctrine as String

Post by jkgarner »

So, based on your snippet, I wrote the following:

Code: Select all

--function: table2string. Converts a table to a string that can reset the table.
 function table2string(o)
     if o == nil then return 'nil';end
     if type(o) == 'table' then
        local s = '{ '
        local maxcount = 0;
        local count = 1;
        for k,v in pairs(o) do maxcount=maxcount+1; end
        for k,v in pairs(o) do
          if count == maxcount then
           s = s .. k ..' = ' .. table2string(v) --no trailing ","
          else
           s = s .. k ..' = ' .. table2string(v) .. ', '
          end
          count = count + 1
        end
        return s .. ' }'
     else
         if type(o)=='string' then
             return '\''..tostring(o)..'\''
         else
             return tostring(o)
         end
     end
 end
Now I am contemplating the inverse function: string2table.

Yeah, I know that this is serialization of tables.

I welcome all inputs ([&o]KnightHawk75)

In the meantime, I will be looking over http://lua-users.org/wiki/TableSerialization where there are some ideas.

Cheers [:)]
KnightHawk75
Posts: 1850
Joined: Thu Nov 15, 2018 7:24 pm

RE: Unit Doctrine as String

Post by KnightHawk75 »

If you want serialize\de-serialize a table I've been using the following for a year with no issues when serializing\deserializing to storagekeys, including large tables within tables etc too the tune of several MB of data.

Code: Select all

 --------------------------------------------------------------------------------------------------------------------------------
 -- JSON Library - Credit: https://gist.github.com/tylerneylon/59f4bcf316be525b30ab
 --
 --              - Only changes were the function names to fit the namespace
 --              - kh note to self: namespace change may slow it down slightly > ~100k of data. 
 --------------------------------------------------------------------------------------------------------------------------------
 
Snipped... just see attached textfile

call .stringify to serialize, .parse to de-serialize back to table.

Attachments
gKH_State_json.txt
(5.56 KiB) Downloaded 24 times
jkgarner
Posts: 175
Joined: Thu Apr 30, 2020 12:42 pm

RE: Unit Doctrine as String

Post by jkgarner »

Thanks.
I'll start with this

[8D]
jkgarner
Posts: 175
Joined: Thu Apr 30, 2020 12:42 pm

RE: Unit Doctrine as String

Post by jkgarner »

KnightHawk75,
Can you show usage?

I passed a non-empty table into stringify, and it returned '{}'
I am sure that's not correct.

Code: Select all

local currentDoctrine = ScenEdit_GetDoctrine({side= 'USA', name = '73rd Batallion 2nd Company 1st Platoon (M2A3 Bradley AFV x 4)'})
 print(currentDoctrine)
 
 local doctrienStr = json.stringify(currentDoctrine,false)
 print('stringify called...')
 print(doctrineStr)
Results:

Code: Select all

{ weapon_control_status_air = '0', weapon_control_status_subsurface = '0', weapon_control_status_surface = '0', weapon_control_status_land = '0', engage_opportunity_targets = '1', automatic_evasion = '1' }
 stringify called...
 {}
Thanks.

KnightHawk75
Posts: 1850
Joined: Thu Nov 15, 2018 7:24 pm

RE: Unit Doctrine as String

Post by KnightHawk75 »

if you modified the namespace... did you modify it everywhere in every function?

Anyway based on what I previously posted:

Code: Select all

 local theTbl = {testentry1=1,testentry2=2,somesubtable={entry1="one",entrytwo="two"}}; --the table object
 local retval = gKH.json.stringify(theTbl); --serialize
 print(retval); --show it serialized.
 theTbl = nil; --wipe out the var.
 theTbl = gKH.json.parse(retval); -- de-serialize
 print(theTbl); -- show it reconstituted.
 
Image
Image


jkgarner
Posts: 175
Joined: Thu Apr 30, 2020 12:42 pm

RE: Unit Doctrine as String

Post by jkgarner »

thanks.
jkgarner
Posts: 175
Joined: Thu Apr 30, 2020 12:42 pm

RE: Unit Doctrine as String

Post by jkgarner »

Got it working:

Code: Select all

local currentDoctrine = ScenEdit_GetDoctrine({side= 'USA', name = '73rd Batallion 2nd Company 1st Platoon (M2A3 Bradley AFV x 4)'})
 print(currentDoctrine)
 local doctrineStr = json.stringify(currentDoctrine,false)
 print('stringify called...')
 print(doctrineStr)
 print('parse called')
 local postDoctrine = json.parse(doctrineStr)
 print(postDoctrine)
Results:

Code: Select all

{ weapon_control_status_air = '0', weapon_control_status_subsurface = '0', weapon_control_status_surface = '0', weapon_control_status_land = '0', engage_opportunity_targets = '1', automatic_evasion = '1' }
 stringify called...
 {"weapon_control_status_air":"0", "weapon_control_status_subsurface":"0", "weapon_control_status_surface":"0", "weapon_control_status_land":"0", "engage_opportunity_targets":"1", "automatic_evasion":"1"}
 parse called
 { weapon_control_status_air = '0', weapon_control_status_subsurface = '0', weapon_control_status_surface = '0', weapon_control_status_land = '0', engage_opportunity_targets = '1', automatic_evasion = '1' }
[:)][:)][:)]

Now to get it to happen from withing my event!

Thanks for the help.
Cheers[8D]
Post Reply

Return to “Lua Legion”