Page 1 of 1

Unit Doctrine as String

Posted: Sun Feb 21, 2021 9:06 am
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?
[&:]


RE: Unit Doctrine as String

Posted: Sun Feb 21, 2021 4:46 pm
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)


RE: Unit Doctrine as String

Posted: Mon Feb 22, 2021 9:10 am
by jkgarner
thanks. this is a good start.

RE: Unit Doctrine as String

Posted: Tue Feb 23, 2021 2:12 pm
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 [:)]

RE: Unit Doctrine as String

Posted: Tue Feb 23, 2021 6:49 pm
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.


RE: Unit Doctrine as String

Posted: Tue Feb 23, 2021 6:57 pm
by jkgarner
Thanks.
I'll start with this

[8D]

RE: Unit Doctrine as String

Posted: Wed Feb 24, 2021 7:43 am
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.


RE: Unit Doctrine as String

Posted: Wed Feb 24, 2021 9:21 am
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



RE: Unit Doctrine as String

Posted: Wed Feb 24, 2021 10:15 am
by jkgarner
thanks.

RE: Unit Doctrine as String

Posted: Wed Feb 24, 2021 4:58 pm
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]