[NFP] LUA: Need help accessing combat object attributes

SourceCodeSamurai

Chieftain
Joined
Jul 9, 2021
Messages
2
I am trying to write a small mod that targets the issue with the combat experience of aircraft carriers. I got a mod set up, got the event hook for the combat initialized but the object that the event provides is not really helpful at the moment.

Thanks to this event documentation I have an idea of what to expect.

I hoped to access the object attributes like object.COMBAT_TYPE or object["COMBAT_TYPE"] but these do not exist (nil). So I looped over the table to checked what keys and values are being provided and instead of proper key-names the table only provides some strange integer numbers. I assume they are ids for some kind of reference table (though negative numbers are usually never used for reference ids...). But for the death of me, I can't find the connection.

Is there a method (something like object[getRefIndexFor("COMBAT_TYPE")] or similar) to translate these numbers into proper key names? Does someone know about a mod where the source code is available that had solved that issue already so I could learn from it?

Thank you in advance!


Combat Object example:
Code:
{
    954462304 = false,
    865706498 = true,
    432821635 = true,
    1431908133 = {
        126272016 = 0,
        -746205821 = 100,
        853003252 = -1,
        -958805242 = 34,
        -1522214634 = 0,
        1930175143 = 34,
        -1263136305 = 0,
        236175385 = -1,
        1472654640 = {
            type = 1,
            player = 0,
            id = 131073,
        },
        -1351684353 = 5,
        -1779249298 = {
            y = 16,
            x = 9,
        },
        -1306278355 = 20,
        1838009406 = 0,
        1116922831 = 5,
    },
    -1480090105 = -1,
    -1632097141 = {
        126272016 = 0,
        -746205821 = 100,
        853003252 = -1,
        -958805242 = 21,
        -1522214634 = 34,
        1930175143 = 21,
        -1263136305 = 0,
        236175385 = -1,
        1472654640 = {
            type = 1,
            player = 63,
            id = 196610,
        },
        -1351684353 = 3,
        -1779249298 = {
            y = 17,
            x = 9,
        },
        -1306278355 = 25,
        1838009406 = 0,
        1116922831 = 9,
    },
    -278393875 = false,
    -1779249298 = {
        y = 17,
        x = 9,
    },
    469286896 = 0,
    433239030 = 0,
    598597112 = true,
    493520281 = {
        126272016 = 0,
        -746205821 = 0,
        853003252 = -1,
        -958805242 = 0,
        -1522214634 = 0,
        1930175143 = 0,
        -1263136305 = 0,
        236175385 = -1,
        1472654640 = {
            type = 0,
            player = -1,
            id = -1,
        },
        -1351684353 = 0,
        -1779249298 = {
            y = -9999,
            x = -9999,
        },
        -1306278355 = 0,
        1838009406 = 0,
        1116922831 = 0,
    },
    -5828522 = true,
    -77002069 = {
        y = -9999,
        x = -9999,
    },
    -2102924904 = 748940753,
    -247326915 = -1,
    705738904 = -1507201801,
    787125023 = {
        126272016 = 0,
        -746205821 = 0,
        853003252 = -1,
        -958805242 = 0,
        -1522214634 = 0,
        1930175143 = 0,
        -1263136305 = 0,
        236175385 = -1,
        1472654640 = {
            type = 0,
            player = -1,
            id = -1,
        },
        -1351684353 = 0,
        -1779249298 = {
            y = -9999,
            x = -9999,
        },
        -1306278355 = 0,
        1838009406 = 0,
        1116922831 = 0,
    },
}
 
you need CombatResultParameters.COMBAT_TYPE

CombatResultParameters is a table containing the ashes for the keys of the CombatResult table

for example to get the attacker ID
Code:
function OnCombat( combatResult )
  print(combatResult[CombatResultParameters.ATTACKER][CombatResultParameters.ID].player)
end
Events.Combat.Add( OnCombat )

not the simplest mod for an example, but I don't know if other are using this event:

function OnCombat in
https://github.com/Gedemon/Civ6-GCO/blob/master/Scripts/GCO_UnitScript.lua

better use the base game example, function OnShowCombat in
"\Steam\steamapps\common\Sid Meier's Civilization VI\Base\Assets\UI\Panels\UnitPanel.lua"


for your other question there is DB.MakeHash, but I don't think it works for an entry of a table like CombatResultParameters.
 
That was really helpful! I have to learn so much, still!

After going through all the files I could now easily access the attributes and work with them. But I ran into another roadblock. *sad*

Apparently, the CombatType is also stored as some kind of hash. I looked into the UnitPanel.lua file and found out that there is a table that stores these hashes for comparisons (for example CombatTypes.MELEE). The way it is writen it suggested that this CombatTypes table is a valriable on root level of the script context. Since they were never defined I assume they are passed through via the includes. Adding them only returned errors and no combination enabled me to get access to the CombatTypes object/table (always nil).

Then I looked up your mod and found that you used ExposedMembers to get access to the CombatTypes. But no luck for me, since there was no CombatTypes object part of the ExposedMembers object in my mod. As far as I understand it, the ExposedMembers is an object that is shared across different script context areas, right? So you did push the real CombatTypes table (as a reference ?) into that cross-context object, yes? Yet, I could not find the event and code line that did so.

By any chance, do you remember where you set up that reference? Is there a special event that provides that context I could hook into?

Many thanks for your patience with me! Your help is very much appreciated!
 
Those kind of tables (CombatResultParameters, CombatTypes, ...) are exposed to Lua in GameCore I suppose, in the UI context, the Script context or both.

In the case of CombatTypes, as it's only accessible in UI context, yes, I add it to ExposedMembers in a file loaded in UI context, in my mod it's this file (fuction Initialize at the end of the file):
https://github.com/Gedemon/Civ6-GCO/blob/master/InGame/GCO_ContextFunctions.lua
 
Top Bottom