EveronLife icon indicating copy to clipboard operation
EveronLife copied to clipboard

Add EL_ComponentFinder<T>

Open Arkensor opened this issue 3 years ago • 0 comments

I propose a small utility class to turn something like this

SCR_CampaignArmoryStorageManagerComponent storageManager = SCR_CampaignArmoryStorageManagerComponent.Cast(storageComponent.GetOwner().FindComponent(SCR_CampaignArmoryStorageManagerComponent));

into

auto storageManager = EL_ComponentFinder<SCR_CampaignArmoryStorageManagerComponent>.Find(storageComponent.GetOwner());

It potentially saves 2 out of 3 component type names in a given line, making it a bit less to type, but especially making the lines shorter. Given how bad the auto-complete is, I often find myself typing out the majority of a component name only to have to autocomplete kick in for the last few characters. Doing the same again for the cast and the find call itself is straight-up annoying.

I think this could be rather useful. Nobody is forced to use it, so I think that should be fine.

Benchmarks

SCRIPT : RunNormal() result: 0.00148 ticks. SCRIPT : RunUtility() result: 0.00159 ticks.

A benchmark shows that as expected, adding one additional function call is slower, but the difference is insignificant. The result shows the average tick count over 100.000 iterations. The additional tick count caused by the usage of the utility is 0.00011 ticks per use.

void RunNormal(IEntity pUserEntity)
{
    auto start = System.GetTickCount(0);
    int runs = 100000;
    for(int i = 0; i < runs; i++)
    {
        auto inventoryManager = SCR_InventoryStorageManagerComponent.Cast(pUserEntity.FindComponent(SCR_InventoryStorageManagerComponent));
    }
    auto end = System.GetTickCount(start);
    PrintFormat("RunNormal() result: %1 ticks.", (end * 1.0) / runs);
}

void RunUtility(IEntity pUserEntity)
{
    auto start = System.GetTickCount(0);
    int runs = 100000;
    for(int i = 0; i < runs; i++)
    {
        auto inventoryManager = EL_ComponentFinder<SCR_InventoryStorageManagerComponent>.Find(pUserEntity);
    }
    auto end = System.GetTickCount(start);
    PrintFormat("RunUtility() result: %1 ticks.", (end * 1.0) / runs);
}

Arkensor avatar Sep 02 '22 13:09 Arkensor