[Solved] C++ NPC get race command

Everything about development and the OpenMW source code.
Post Reply
guigui
Posts: 33
Joined: 12 Apr 2020, 12:22

[Solved] C++ NPC get race command

Post by guigui »

Hi,

I want to get the race of a NPC instead of his name, in the context of this mwclass/npc.cpp line (line 1136):
std::string_view name = getName(ptr);
(yes, i want "name" to be the race)

What must i write instead of getName(ptr) ?
std::string_view name = getRace(ptr); ?
something else?
Last edited by guigui on 07 Jun 2023, 10:28, edited 1 time in total.
guigui
Posts: 33
Joined: 12 Apr 2020, 12:22

Re: C++ NPC get race command

Post by guigui »

Nobody knows?...
I tried many things but i didn't find how to see the NPC race instead of his name (in the crosshair tooltip).

PS: this is the context in mwclass/npc.cpp:
[...]

Code: Select all

    MWGui::ToolTipInfo Npc::getToolTipInfo(const MWWorld::ConstPtr& ptr, int count) const
    {

        const MWWorld::LiveCellRef<ESM::NPC>* ref = ptr.get<ESM::NPC>();

        bool fullHelp = MWBase::Environment::get().getWindowManager()->getFullHelp();
        MWGui::ToolTipInfo info;

        std::string_view name = getName(ptr); // <====================================    !!!! THIS LINE !!!!
        info.caption = MyGUI::TextIterator::toTagsString(MWGui::toUString(name));
        if (fullHelp && !ref->mBase->mName.empty() && ptr.getRefData().getCustomData()
            && ptr.getRefData().getCustomData()->asNpcCustomData().mNpcStats.isWerewolf())
        {
            info.caption += " (";
            info.caption += MyGUI::TextIterator::toTagsString(ref->mBase->mName);
            info.caption += ")";
        }

        if (fullHelp)
            info.text = MWGui::ToolTips::getMiscString(ref->mBase->mScript.getRefIdString(), "Script");

        return info;
    }
[...]
Ebonair
Posts: 25
Joined: 04 Dec 2018, 20:08

Re: C++ NPC get race command

Post by Ebonair »

I found this in npc.cpp:

Code: Select all

    void autoCalculateAttributes(const ESM::NPC* npc, MWMechanics::CreatureStats& creatureStats)
    {
        // race bonus
        const ESM::Race* race = MWBase::Environment::get().getWorld()->getStore().get<ESM::Race>().find(npc->mRace);
I know it seems complex, but I sometimes find it useful to picture the code visually. In this case I visualize it as a raw wound, screaming to be wrapped inside an accessor.
guigui
Posts: 33
Joined: 12 Apr 2020, 12:22

Re: C++ NPC get race command

Post by guigui »

Thanks for your reply.
But i don't know anything about C++. I tried some things "on feeling". I can suppress the name with " std::string_view name = ""; ", but that's all, the rest is like chinese for me.
I had seen this line:
const ESM::Race* race = MWBase::Environment::get().getWorld()->getStore().get<ESM::Race>().find(npc->mRace);
but i don't know how to use it. I tried some things, but i always get errors during compilation.
Ebonair
Posts: 25
Joined: 04 Dec 2018, 20:08

Re: C++ NPC get race command

Post by Ebonair »

Well, if you paste that line of code in the function you're modifying, what compiler error do you get? Also, what line are you pasting it to?
guigui
Posts: 33
Joined: 12 Apr 2020, 12:22

Re: C++ NPC get race command

Post by guigui »

For example, i modify the line
std::string_view name = getName(ptr);
to
std::string_view name = MWBase::Environment::get().getWorld()->getStore().get<ESM::Race>().find(npc->mRace);
(line 1137)

and i get:
[ 42%] Building CXX object apps/openmw/CMakeFiles/openmw.dir/mwclass/npc.cpp.o
/home/guigui/builds/openmw-git/src/openmw/apps/openmw/mwclass/npc.cpp: Dans la fonction membre « virtual MWGui::ToolTipInfo MWClass::Npc::getToolTipInfo(const MWWorld::ConstPtr&, int) const »:
/home/guigui/builds/openmw-git/src/openmw/apps/openmw/mwclass/npc.cpp:1137:105: erreur: « npc » n'a pas été déclaré dans cette portée; vouliez-vous employer « Npc » ?
1137 | Environment::get().getWorld()->getStore().get<ESM::Race>().find(npc->mRace);
| ^~~
| Npc
make[2]: *** [apps/openmw/CMakeFiles/openmw.dir/build.make:3716 : apps/openmw/CMakeFiles/openmw.dir/mwclass/npc.cpp.o] Erreur 1
make[1]: *** [CMakeFiles/Makefile2:720 : apps/openmw/CMakeFiles/openmw.dir/all] Erreur 2
make: *** [Makefile:136 : all] Erreur 2
==> ERREUR : Une erreur s’est produite dans build().
Abandon…
Then i add
const ESM::NPC* npc, MWMechanics::CreatureStats& creatureStats
before my modified line.
And i get:
[ 42%] Building CXX object apps/openmw/CMakeFiles/openmw.dir/mwclass/npc.cpp.o
/home/guigui/builds/openmw-git/src/openmw/apps/openmw/mwclass/npc.cpp: Dans la fonction membre « virtual MWGui::ToolTipInfo MWClass::Npc::getToolTipInfo(const MWWorld::ConstPtr&, int) const »:
/home/guigui/builds/openmw-git/src/openmw/apps/openmw/mwclass/npc.cpp:1133:48: erreur: qualified-id in declaration before « & » token
1133 | const ESM::NPC* npc, MWMechanics::CreatureStats& creatureStats
| ^
/home/guigui/builds/openmw-git/src/openmw/apps/openmw/mwclass/npc.cpp:1137:104: erreur: conversion de « const ESM::Race* » vers le type non scalaire « std::string_view » {aka « std::basic_string_view<char> »} demandée
1137 | :Environment::get().getWorld()->getStore().get<ESM::Race>().find(npc->mRace;
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~

make[2]: *** [apps/openmw/CMakeFiles/openmw.dir/build.make:3716 : apps/openmw/CMakeFiles/openmw.dir/mwclass/npc.cpp.o] Erreur 1
make[1]: *** [CMakeFiles/Makefile2:720 : apps/openmw/CMakeFiles/openmw.dir/all] Erreur 2
make: *** [Makefile:136 : all] Erreur 2
==> ERREUR : Une erreur s’est produite dans build().
Abandon…
Chris
Posts: 1625
Joined: 04 Sep 2011, 08:33

Re: C++ NPC get race command

Post by Chris »

What are you trying to do? Where and why do you need the name of a race instead of the NPC name? This feels like the kind of thing that would be better as a mod, but you haven't really said what your ultimate goal is.
guigui
Posts: 33
Joined: 12 Apr 2020, 12:22

Re: C++ NPC get race command

Post by guigui »

In Morrowind, when a NPC is near, and you look at him, his name appears in a tooltip. It's not realistic/immersive. The player should not be a guesser...

I have succeeded to remove these names: in mwclass/npc.cpp i modified:
std::string_view name = getName(ptr);
to
std::string_view name = "";
(line 1137)

Now i want better: i want that the race appears in this tooltip (because the player can know the race of the NPC he sees).

To my knowledge, there is no mod for that for OpenMW. I even don't know if it's possible (perhaps in lua?). I don't know how to do a mod in lua. I can do little mods, but not that.
So it's seem to me easier to modify mwclass/npc.cpp, but if you have a mod, i'm listening... £:^)
guigui
Posts: 33
Joined: 12 Apr 2020, 12:22

Re: C++ NPC get race command

Post by guigui »

So, there is no simple C++ code line for that?...
guigui
Posts: 33
Joined: 12 Apr 2020, 12:22

Re: C++ NPC get race command

Post by guigui »

Kindi give me the solution:

std::string_view name = ptr.get<ESM::NPC>()->mBase->mRace.serializeText();

or

std::string_view name = MWBase::Environment::get().getESMStore()->get<ESM::Race>().search(ref->mBase->mRace)->mName;
(to have the race in the language of Morrowind Data)
Post Reply