Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question about looping over player inventory #222

Open
codersparks opened this issue Nov 13, 2023 · 1 comment
Open

Question about looping over player inventory #222

codersparks opened this issue Nov 13, 2023 · 1 comment

Comments

@codersparks
Copy link

codersparks commented Nov 13, 2023

Hi All,

Just wanted to say thanks for the efforts going into this, I've only started looking into creating a mod in the last couple of weeks, I have a quick question if I may, I know it's still early days so if it's the fact that this hasn't been worked on yet, I can go away until it has (I'd love to be able to contribute but have no idea how to get started).

I'm just playing at the moment and I'm trying to loop over my players inventory and have got this far:

auto* player = RE::PlayerCharacter::GetSingleton();

auto name = std::string(player->GetDisplayFullName());
logger::info("Player name: {}", name);
if (player) {

    player->ForEachInventoryItem([&](const RE::BGSInventoryItem& a_item) {
        auto reference = a_item.object;

        if (reference) {
            logger::info("Inventory Item: isWeapon: {}, FormID: {}, Object Type name: {}", reference->IsWeapon(),
                         reference->formID, reference->GetObjectTypeName());
        }
        return RE::BSContainer::ForEachResult::kContinue;
    });

} else {
    logger::info("Player not loaded");
}

My question is two part:

  1. I've found from experimentation, that there is only one entry in the foreach loop for each item even if I hold multiple instances of that item. Is it possible to get the count for each inventory item?
  2. Is there a way to get the name of the item - I've investigated the form object but cannot for the life of me figure out how to get the name.

Any help you can give is much appreciated and I look forwards to being able to use any further updates to the library.

CoderSparks

@typeryougishiki
Copy link

About getting the name of the item, the method I use is:
Convert the type of the item pointer to the pointer of the class corresponding to FormType according to the object's FormType, for example:

case RE::FormType::kALCH: {
    static_assert(RE::FormType::kALCH == RE::AlchemyItem::FORMTYPE);
    return item_form->As<RE::AlchemyItem>();
}break;

If the class corresponding to the object's FormType extends from TESFullName, you can use GetFullName() to get its name from it, but for items with modified names (use workbench to rename or with special names), this can only get the original name of the item.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants