Reflection Helper

The reflection helper is a powerful part of Lua coding, it helps to access values for GetField, GetValue, IsContainer and ToContainer.

## Comparison

### Without reflection\_helper

```
local function PrintStats()
    local component = EntityService:GetSingletonComponent("CampaignStatsSystemComponent")

    local stats = component:GetField("aggregates"):ToContainer()
    for i=0,stats:GetItemCount()-1 do
        local item = stats:GetItem( i )

        local stat\_index = tonumber(item:GetField("key"):GetValue())
        local stat\_name = GetStatName( stat\_index )

        local stat\_item = item:GetField("value")
        local stat\_count = stat\_item:GetField("total"):GetValue()
        LogService:Log(tostring(stat\_name) .. " = " .. tostring(stat\_count));

        local stat\_values = stat\_item:GetField("values"):ToContainer()

        for j=0,stat\_values:GetItemCount()-1 do
            local stat\_value = stat\_values:GetItem(j)

            local name = stat\_value:GetField("key"):GetValue()
            local value = stat\_value:GetField("value"):GetValue()

            LogService:Log(" - " .. name .. " = " .. value);
        end
    end
end

```

### With reflection\_helper

```
local function PrintStats()
    local component = reflection\_helper( EntityService:GetSingletonComponent("CampaignStatsSystemComponent") )

    local stats = component.aggregates
    for i=1,stats.count do
        local stat = stats[i]
        local stat\_name = GetStatName( stat.key )

        local stat\_item = stat.value
        LogService:Log(tostring(stat\_name) .. " = " .. tostring(stat\_item.total));

        local stat\_values = stat\_item.values
        for j=1,stat\_values.count do
            local stat\_value = stat\_values[j]
            LogService:Log(" - " .. stat\_value.key .. " = " .. tostring(stat\_value.value));
        end
    end
end

```

### Note

Both these examples access the GetStatName function (Just for the sake of completeness)

```
local function GetStatName( stat\_type )
    local names = {
        "PlaytimeElapsed",
        "ResourceProduced",
        "ResourceConsumed",
        "ResourceLooted",
        "BuildingCreated",
        "BuildingUpgraded",
        "BuildingSold",
        "BuildingDestroyed",
        "ObjectiveCompleted",
        "ObjectiveFailed",
        "MissionCompleted",
        "MissionFailed",
        "ItemCrafted",
        "ItemLooted",
        "ConsumableUsed",
        "SkillUsed",
        "ResearchCompleted",
        "PlayerDeath",
        "PlayerDamageTakenByType",
        "PlayerDamageTakenByOwner",
        "PlayerDamageDoneByType",
        "PlayerDamageDoneByTarget",
        "PlayerDamageDoneByCreator",
        "PlayerDamageDoneByOwner",
        "EnemyUnitsKilled",
        "NeutralUnitsKilled",
        "CheatsUsed",
        "DifficultyFinished",
        "CampaignFinished",
        "ItemLost",
        "BuildingsDamageDoneByCreator",
        "BuildingsDamageDoneByType",
        "BuildingsDamageTakenByType",
        "BuildingsDamageTakenByOwner"
    }

    return names[ stat\_type + 1]
end

```

## Log Reflection Helper data

To get an overview of what the reflection\_helper offers, ⁣you can use the code below to view the reflection\_helper data.

### View Reflection Helper data

At the beginning of your lua file should be the require for the reflection element:

```
require("lua/utils/reflection.lua")

```

Inside your code, you add the LogService and combine it with the reflection\_helper, for example:

```
local refl\_MechComponent = reflection\_helper( EntityService:GetComponent(self.entity, "MechComponent"))
LogService:Log( tostring(refl\_MechComponent)  )

```
Now you can see the data inside your exor\_logs.txt, for example:

If you need to go one "layer" deeper into the information, you can use Dot Notation and just append what you need, for example:

```
LogService:Log( tostring( refl\_MechComponent.zoom ) )
LogService:Log( tostring( refl\_MechComponent.velocity ) )
LogService:Log( tostring( refl\_MechComponent.pickup\_radius ) )

```