Ryan-rsm-McKenzie / CommonLibSSE

A reverse engineered library for hacking Skyrim Special Edition
MIT License
208 stars 153 forks source link

RE::TESObjectCELL::ForEachReferenceInRange #110

Open Elephant42 opened 3 years ago

Elephant42 commented 3 years ago

Hello,

I've been hammering away at an SKSE plugin for a couple of weeks using your excellent library and have managed to get most of what I need working but have struck a brick wall on one feature. Would you have any suggestions on how to use the method RE::TESObjectCELL::ForEachReferenceInRange. I'm trying to get a list of RE::TESObjectREFR* that are within a certain radius of a target Actor.

TIA for any help you care to offer.

SteveTownsend commented 2 years ago

I think this should be _return distance <= (a_radius * aradius) ? since it's comparing to the square of the distance.

    void TESObjectCELL::ForEachReferenceInRange(const NiPoint3& a_origin, float a_radius, std::function<bool(TESObjectREFR&)> a_callback) const
    {
        ForEachReference([&](TESObjectREFR& ref) {
            const auto distance = a_origin.GetSquaredDistance(ref.GetPosition());
            return distance <= a_radius ?
                         a_callback(ref) :
                         true;
        });
    }

It's also possible to optimize by not doing any floating point mult if any of dx/dy/dz are > radius from ref